php-parser/lib/PhpParser/Builder/Class_.php

141 lines
3.6 KiB
PHP
Raw Normal View History

2017-08-18 22:57:27 +02:00
<?php declare(strict_types=1);
2012-03-10 17:56:56 +01:00
namespace PhpParser\Builder;
use PhpParser;
use PhpParser\BuilderHelpers;
use PhpParser\Node;
use PhpParser\Node\Name;
use PhpParser\Node\Stmt;
class Class_ extends Declaration
2012-03-10 17:56:56 +01:00
{
protected $name;
protected $extends = null;
protected $implements = [];
protected $flags = 0;
2012-03-10 17:56:56 +01:00
protected $uses = [];
protected $constants = [];
protected $properties = [];
protected $methods = [];
/** @var Node\AttributeGroup[] */
protected $attributeGroups = [];
2012-03-10 17:56:56 +01:00
/**
* Creates a class builder.
*
* @param string $name Name of the class
*/
2017-04-28 21:40:59 +02:00
public function __construct(string $name) {
2012-03-10 17:56:56 +01:00
$this->name = $name;
}
/**
* Extends a class.
*
* @param Name|string $class Name of class to extend
2012-03-10 17:56:56 +01:00
*
* @return $this The builder instance (for fluid interface)
2012-03-10 17:56:56 +01:00
*/
public function extend($class) {
$this->extends = BuilderHelpers::normalizeName($class);
2012-03-10 17:56:56 +01:00
return $this;
}
/**
* Implements one or more interfaces.
*
2016-02-20 17:06:09 +01:00
* @param Name|string ...$interfaces Names of interfaces to implement
2012-03-10 17:56:56 +01:00
*
* @return $this The builder instance (for fluid interface)
2012-03-10 17:56:56 +01:00
*/
public function implement(...$interfaces) {
foreach ($interfaces as $interface) {
$this->implements[] = BuilderHelpers::normalizeName($interface);
2012-03-10 17:56:56 +01:00
}
return $this;
}
/**
* Makes the class abstract.
*
* @return $this The builder instance (for fluid interface)
2012-03-10 17:56:56 +01:00
*/
public function makeAbstract() {
$this->flags = BuilderHelpers::addModifier($this->flags, Stmt\Class_::MODIFIER_ABSTRACT);
2012-03-10 17:56:56 +01:00
return $this;
}
/**
* Makes the class final.
*
* @return $this The builder instance (for fluid interface)
2012-03-10 17:56:56 +01:00
*/
public function makeFinal() {
$this->flags = BuilderHelpers::addModifier($this->flags, Stmt\Class_::MODIFIER_FINAL);
2012-03-10 17:56:56 +01:00
return $this;
}
/**
* Adds a statement.
*
* @param Stmt|PhpParser\Builder $stmt The statement to add
2012-03-10 17:56:56 +01:00
*
* @return $this The builder instance (for fluid interface)
2012-03-10 17:56:56 +01:00
*/
public function addStmt($stmt) {
$stmt = BuilderHelpers::normalizeNode($stmt);
2012-03-10 17:56:56 +01:00
$targets = [
Stmt\TraitUse::class => &$this->uses,
Stmt\ClassConst::class => &$this->constants,
Stmt\Property::class => &$this->properties,
Stmt\ClassMethod::class => &$this->methods,
];
2012-03-10 17:56:56 +01:00
$class = \get_class($stmt);
if (!isset($targets[$class])) {
throw new \LogicException(sprintf('Unexpected node of type "%s"', $stmt->getType()));
2012-03-10 17:56:56 +01:00
}
$targets[$class][] = $stmt;
2012-03-10 17:56:56 +01:00
return $this;
}
/**
* Adds an attribute group.
*
* @param Node\Attribute|Node\AttributeGroup $attribute
*
* @return $this The builder instance (for fluid interface)
*/
public function addAttribute($attribute) {
$this->attributeGroups[] = BuilderHelpers::normalizeAttribute($attribute);
return $this;
}
2012-03-10 17:56:56 +01:00
/**
* Returns the built class node.
*
* @return Stmt\Class_ The built class node
2012-03-10 17:56:56 +01:00
*/
2017-04-28 21:40:59 +02:00
public function getNode() : PhpParser\Node {
return new Stmt\Class_($this->name, [
'flags' => $this->flags,
2012-03-10 17:56:56 +01:00
'extends' => $this->extends,
'implements' => $this->implements,
'stmts' => array_merge($this->uses, $this->constants, $this->properties, $this->methods),
'attrGroups' => $this->attributeGroups,
], $this->attributes);
2012-03-10 17:56:56 +01:00
}
2018-01-10 18:04:06 +01:00
}