-<?php
+<?php declare(strict_types=1);
namespace PhpParser\Builder;
use PhpParser;
+use PhpParser\BuilderHelpers;
use PhpParser\Node\Name;
use PhpParser\Node\Stmt;
protected $name;
protected $extends = null;
- protected $implements = array();
+ protected $implements = [];
protected $flags = 0;
- protected $uses = array();
- protected $constants = array();
- protected $properties = array();
- protected $methods = array();
+ protected $uses = [];
+ protected $constants = [];
+ protected $properties = [];
+ protected $methods = [];
/**
* Creates a class builder.
*
* @param string $name Name of the class
*/
- public function __construct($name) {
+ public function __construct(string $name) {
$this->name = $name;
}
* @return $this The builder instance (for fluid interface)
*/
public function extend($class) {
- $this->extends = $this->normalizeName($class);
+ $this->extends = BuilderHelpers::normalizeName($class);
return $this;
}
*
* @return $this The builder instance (for fluid interface)
*/
- public function implement() {
- foreach (func_get_args() as $interface) {
- $this->implements[] = $this->normalizeName($interface);
+ public function implement(...$interfaces) {
+ foreach ($interfaces as $interface) {
+ $this->implements[] = BuilderHelpers::normalizeName($interface);
}
return $this;
* @return $this The builder instance (for fluid interface)
*/
public function makeAbstract() {
- $this->setModifier(Stmt\Class_::MODIFIER_ABSTRACT);
+ $this->flags = BuilderHelpers::addModifier($this->flags, Stmt\Class_::MODIFIER_ABSTRACT);
return $this;
}
* @return $this The builder instance (for fluid interface)
*/
public function makeFinal() {
- $this->setModifier(Stmt\Class_::MODIFIER_FINAL);
+ $this->flags = BuilderHelpers::addModifier($this->flags, Stmt\Class_::MODIFIER_FINAL);
return $this;
}
* @return $this The builder instance (for fluid interface)
*/
public function addStmt($stmt) {
- $stmt = $this->normalizeNode($stmt);
-
- $targets = array(
- 'Stmt_TraitUse' => &$this->uses,
- 'Stmt_ClassConst' => &$this->constants,
- 'Stmt_Property' => &$this->properties,
- 'Stmt_ClassMethod' => &$this->methods,
- );
-
- $type = $stmt->getType();
- if (!isset($targets[$type])) {
- throw new \LogicException(sprintf('Unexpected node of type "%s"', $type));
+ $stmt = BuilderHelpers::normalizeNode($stmt);
+
+ $targets = [
+ Stmt\TraitUse::class => &$this->uses,
+ Stmt\ClassConst::class => &$this->constants,
+ Stmt\Property::class => &$this->properties,
+ Stmt\ClassMethod::class => &$this->methods,
+ ];
+
+ $class = \get_class($stmt);
+ if (!isset($targets[$class])) {
+ throw new \LogicException(sprintf('Unexpected node of type "%s"', $stmt->getType()));
}
- $targets[$type][] = $stmt;
+ $targets[$class][] = $stmt;
return $this;
}
*
* @return Stmt\Class_ The built class node
*/
- public function getNode() {
- return new Stmt\Class_($this->name, array(
+ public function getNode() : PhpParser\Node {
+ return new Stmt\Class_($this->name, [
'flags' => $this->flags,
'extends' => $this->extends,
'implements' => $this->implements,
'stmts' => array_merge($this->uses, $this->constants, $this->properties, $this->methods),
- ), $this->attributes);
+ ], $this->attributes);
}
-}
\ No newline at end of file
+}