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 Interface_ extends Declaration
13 protected $extends = [];
14 protected $constants = [];
15 protected $methods = [];
18 * Creates an interface builder.
20 * @param string $name Name of the interface
22 public function __construct(string $name) {
27 * Extends one or more interfaces.
29 * @param Name|string ...$interfaces Names of interfaces to extend
31 * @return $this The builder instance (for fluid interface)
33 public function extend(...$interfaces) {
34 foreach ($interfaces as $interface) {
35 $this->extends[] = BuilderHelpers::normalizeName($interface);
44 * @param Stmt|PhpParser\Builder $stmt The statement to add
46 * @return $this The builder instance (for fluid interface)
48 public function addStmt($stmt) {
49 $stmt = BuilderHelpers::normalizeNode($stmt);
51 if ($stmt instanceof Stmt\ClassConst) {
52 $this->constants[] = $stmt;
53 } elseif ($stmt instanceof Stmt\ClassMethod) {
54 // we erase all statements in the body of an interface method
56 $this->methods[] = $stmt;
58 throw new \LogicException(sprintf('Unexpected node of type "%s"', $stmt->getType()));
65 * Returns the built interface node.
67 * @return Stmt\Interface_ The built interface node
69 public function getNode() : PhpParser\Node {
70 return new Stmt\Interface_($this->name, [
71 'extends' => $this->extends,
72 'stmts' => array_merge($this->constants, $this->methods),
73 ], $this->attributes);