1 <?php declare(strict_types=1);
3 namespace PhpParser\Builder;
6 use PhpParser\BuilderHelpers;
7 use PhpParser\Node\Name;
8 use PhpParser\Node\Stmt;
10 class Class_ extends Declaration
14 protected $extends = null;
15 protected $implements = [];
19 protected $constants = [];
20 protected $properties = [];
21 protected $methods = [];
24 * Creates a class builder.
26 * @param string $name Name of the class
28 public function __construct(string $name) {
35 * @param Name|string $class Name of class to extend
37 * @return $this The builder instance (for fluid interface)
39 public function extend($class) {
40 $this->extends = BuilderHelpers::normalizeName($class);
46 * Implements one or more interfaces.
48 * @param Name|string ...$interfaces Names of interfaces to implement
50 * @return $this The builder instance (for fluid interface)
52 public function implement(...$interfaces) {
53 foreach ($interfaces as $interface) {
54 $this->implements[] = BuilderHelpers::normalizeName($interface);
61 * Makes the class abstract.
63 * @return $this The builder instance (for fluid interface)
65 public function makeAbstract() {
66 $this->flags = BuilderHelpers::addModifier($this->flags, Stmt\Class_::MODIFIER_ABSTRACT);
72 * Makes the class final.
74 * @return $this The builder instance (for fluid interface)
76 public function makeFinal() {
77 $this->flags = BuilderHelpers::addModifier($this->flags, Stmt\Class_::MODIFIER_FINAL);
85 * @param Stmt|PhpParser\Builder $stmt The statement to add
87 * @return $this The builder instance (for fluid interface)
89 public function addStmt($stmt) {
90 $stmt = BuilderHelpers::normalizeNode($stmt);
93 Stmt\TraitUse::class => &$this->uses,
94 Stmt\ClassConst::class => &$this->constants,
95 Stmt\Property::class => &$this->properties,
96 Stmt\ClassMethod::class => &$this->methods,
99 $class = \get_class($stmt);
100 if (!isset($targets[$class])) {
101 throw new \LogicException(sprintf('Unexpected node of type "%s"', $stmt->getType()));
104 $targets[$class][] = $stmt;
110 * Returns the built class node.
112 * @return Stmt\Class_ The built class node
114 public function getNode() : PhpParser\Node {
115 return new Stmt\Class_($this->name, [
116 'flags' => $this->flags,
117 'extends' => $this->extends,
118 'implements' => $this->implements,
119 'stmts' => array_merge($this->uses, $this->constants, $this->properties, $this->methods),
120 ], $this->attributes);