1<?php declare(strict_types=1);
2
3namespace PhpParser\Builder;
4
5use PhpParser;
6use PhpParser\BuilderHelpers;
7use PhpParser\Node\Name;
8use PhpParser\Node\Stmt;
9
10class Interface_ extends Declaration
11{
12    protected $name;
13    protected $extends = [];
14    protected $constants = [];
15    protected $methods = [];
16
17    /**
18     * Creates an interface builder.
19     *
20     * @param string $name Name of the interface
21     */
22    public function __construct(string $name) {
23        $this->name = $name;
24    }
25
26    /**
27     * Extends one or more interfaces.
28     *
29     * @param Name|string ...$interfaces Names of interfaces to extend
30     *
31     * @return $this The builder instance (for fluid interface)
32     */
33    public function extend(...$interfaces) {
34        foreach ($interfaces as $interface) {
35            $this->extends[] = BuilderHelpers::normalizeName($interface);
36        }
37
38        return $this;
39    }
40
41    /**
42     * Adds a statement.
43     *
44     * @param Stmt|PhpParser\Builder $stmt The statement to add
45     *
46     * @return $this The builder instance (for fluid interface)
47     */
48    public function addStmt($stmt) {
49        $stmt = BuilderHelpers::normalizeNode($stmt);
50
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
55            $stmt->stmts = null;
56            $this->methods[] = $stmt;
57        } else {
58            throw new \LogicException(sprintf('Unexpected node of type "%s"', $stmt->getType()));
59        }
60
61        return $this;
62    }
63
64    /**
65     * Returns the built interface node.
66     *
67     * @return Stmt\Interface_ The built interface node
68     */
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);
74    }
75}
76