Chris@13: name = $name; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Extends a class. Chris@0: * Chris@0: * @param Name|string $class Name of class to extend Chris@0: * Chris@0: * @return $this The builder instance (for fluid interface) Chris@0: */ Chris@0: public function extend($class) { Chris@13: $this->extends = BuilderHelpers::normalizeName($class); Chris@0: Chris@0: return $this; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Implements one or more interfaces. Chris@0: * Chris@0: * @param Name|string ...$interfaces Names of interfaces to implement Chris@0: * Chris@0: * @return $this The builder instance (for fluid interface) Chris@0: */ Chris@13: public function implement(...$interfaces) { Chris@13: foreach ($interfaces as $interface) { Chris@13: $this->implements[] = BuilderHelpers::normalizeName($interface); Chris@0: } Chris@0: Chris@0: return $this; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Makes the class abstract. Chris@0: * Chris@0: * @return $this The builder instance (for fluid interface) Chris@0: */ Chris@0: public function makeAbstract() { Chris@13: $this->flags = BuilderHelpers::addModifier($this->flags, Stmt\Class_::MODIFIER_ABSTRACT); Chris@0: Chris@0: return $this; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Makes the class final. Chris@0: * Chris@0: * @return $this The builder instance (for fluid interface) Chris@0: */ Chris@0: public function makeFinal() { Chris@13: $this->flags = BuilderHelpers::addModifier($this->flags, Stmt\Class_::MODIFIER_FINAL); Chris@0: Chris@0: return $this; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Adds a statement. Chris@0: * Chris@0: * @param Stmt|PhpParser\Builder $stmt The statement to add Chris@0: * Chris@0: * @return $this The builder instance (for fluid interface) Chris@0: */ Chris@0: public function addStmt($stmt) { Chris@13: $stmt = BuilderHelpers::normalizeNode($stmt); Chris@0: Chris@13: $targets = [ Chris@13: Stmt\TraitUse::class => &$this->uses, Chris@13: Stmt\ClassConst::class => &$this->constants, Chris@13: Stmt\Property::class => &$this->properties, Chris@13: Stmt\ClassMethod::class => &$this->methods, Chris@13: ]; Chris@0: Chris@13: $class = \get_class($stmt); Chris@13: if (!isset($targets[$class])) { Chris@13: throw new \LogicException(sprintf('Unexpected node of type "%s"', $stmt->getType())); Chris@0: } Chris@0: Chris@13: $targets[$class][] = $stmt; Chris@0: Chris@0: return $this; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns the built class node. Chris@0: * Chris@0: * @return Stmt\Class_ The built class node Chris@0: */ Chris@13: public function getNode() : PhpParser\Node { Chris@13: return new Stmt\Class_($this->name, [ Chris@0: 'flags' => $this->flags, Chris@0: 'extends' => $this->extends, Chris@0: 'implements' => $this->implements, Chris@0: 'stmts' => array_merge($this->uses, $this->constants, $this->properties, $this->methods), Chris@13: ], $this->attributes); Chris@0: } Chris@13: }