3 namespace PhpParser\Builder;
6 use PhpParser\Node\Name;
7 use PhpParser\Node\Stmt;
9 class Class_ extends Declaration
13 protected $extends = null;
14 protected $implements = array();
17 protected $uses = array();
18 protected $constants = array();
19 protected $properties = array();
20 protected $methods = array();
23 * Creates a class builder.
25 * @param string $name Name of the class
27 public function __construct($name) {
34 * @param Name|string $class Name of class to extend
36 * @return $this The builder instance (for fluid interface)
38 public function extend($class) {
39 $this->extends = $this->normalizeName($class);
45 * Implements one or more interfaces.
47 * @param Name|string ...$interfaces Names of interfaces to implement
49 * @return $this The builder instance (for fluid interface)
51 public function implement() {
52 foreach (func_get_args() as $interface) {
53 $this->implements[] = $this->normalizeName($interface);
60 * Makes the class abstract.
62 * @return $this The builder instance (for fluid interface)
64 public function makeAbstract() {
65 $this->setModifier(Stmt\Class_::MODIFIER_ABSTRACT);
71 * Makes the class final.
73 * @return $this The builder instance (for fluid interface)
75 public function makeFinal() {
76 $this->setModifier(Stmt\Class_::MODIFIER_FINAL);
84 * @param Stmt|PhpParser\Builder $stmt The statement to add
86 * @return $this The builder instance (for fluid interface)
88 public function addStmt($stmt) {
89 $stmt = $this->normalizeNode($stmt);
92 'Stmt_TraitUse' => &$this->uses,
93 'Stmt_ClassConst' => &$this->constants,
94 'Stmt_Property' => &$this->properties,
95 'Stmt_ClassMethod' => &$this->methods,
98 $type = $stmt->getType();
99 if (!isset($targets[$type])) {
100 throw new \LogicException(sprintf('Unexpected node of type "%s"', $type));
103 $targets[$type][] = $stmt;
109 * Returns the built class node.
111 * @return Stmt\Class_ The built class node
113 public function getNode() {
114 return new Stmt\Class_($this->name, array(
115 'flags' => $this->flags,
116 'extends' => $this->extends,
117 'implements' => $this->implements,
118 'stmts' => array_merge($this->uses, $this->constants, $this->properties, $this->methods),
119 ), $this->attributes);