1<?php declare(strict_types=1);
2
3namespace PhpParser\Builder;
4
5use PhpParser;
6use PhpParser\BuilderHelpers;
7use PhpParser\Node;
8use PhpParser\Node\Name;
9use PhpParser\Node\Stmt;
10
11class Interface_ extends Declaration
12{
13    protected $name;
14    protected $extends = [];
15    protected $constants = [];
16    protected $methods = [];
17
18    /** @var Node\AttributeGroup[] */
19    protected $attributeGroups = [];
20
21    /**
22     * Creates an interface builder.
23     *
24     * @param string $name Name of the interface
25     */
26    public function __construct(string $name) {
27        $this->name = $name;
28    }
29
30    /**
31     * Extends one or more interfaces.
32     *
33     * @param Name|string ...$interfaces Names of interfaces to extend
34     *
35     * @return $this The builder instance (for fluid interface)
36     */
37    public function extend(...$interfaces) {
38        foreach ($interfaces as $interface) {
39            $this->extends[] = BuilderHelpers::normalizeName($interface);
40        }
41
42        return $this;
43    }
44
45    /**
46     * Adds a statement.
47     *
48     * @param Stmt|PhpParser\Builder $stmt The statement to add
49     *
50     * @return $this The builder instance (for fluid interface)
51     */
52    public function addStmt($stmt) {
53        $stmt = BuilderHelpers::normalizeNode($stmt);
54
55        if ($stmt instanceof Stmt\ClassConst) {
56            $this->constants[] = $stmt;
57        } elseif ($stmt instanceof Stmt\ClassMethod) {
58            // we erase all statements in the body of an interface method
59            $stmt->stmts = null;
60            $this->methods[] = $stmt;
61        } else {
62            throw new \LogicException(sprintf('Unexpected node of type "%s"', $stmt->getType()));
63        }
64
65        return $this;
66    }
67
68    /**
69     * Adds an attribute group.
70     *
71     * @param Node\Attribute|Node\AttributeGroup $attribute
72     *
73     * @return $this The builder instance (for fluid interface)
74     */
75    public function addAttribute($attribute) {
76        $this->attributeGroups[] = BuilderHelpers::normalizeAttribute($attribute);
77
78        return $this;
79    }
80
81    /**
82     * Returns the built interface node.
83     *
84     * @return Stmt\Interface_ The built interface node
85     */
86    public function getNode() : PhpParser\Node {
87        return new Stmt\Interface_($this->name, [
88            'extends' => $this->extends,
89            'stmts' => array_merge($this->constants, $this->methods),
90            'attrGroups' => $this->attributeGroups,
91        ], $this->attributes);
92    }
93}
94