1<?php declare(strict_types=1);
2
3namespace PhpParser\Builder;
4
5use PhpParser;
6use PhpParser\BuilderHelpers;
7use PhpParser\Node;
8use PhpParser\Node\Stmt;
9
10class Function_ extends FunctionLike {
11    protected string $name;
12    /** @var list<Stmt> */
13    protected array $stmts = [];
14
15    /** @var list<Node\AttributeGroup> */
16    protected array $attributeGroups = [];
17
18    /**
19     * Creates a function builder.
20     *
21     * @param string $name Name of the function
22     */
23    public function __construct(string $name) {
24        $this->name = $name;
25    }
26
27    /**
28     * Adds a statement.
29     *
30     * @param Node|PhpParser\Builder $stmt The statement to add
31     *
32     * @return $this The builder instance (for fluid interface)
33     */
34    public function addStmt($stmt) {
35        $this->stmts[] = BuilderHelpers::normalizeStmt($stmt);
36
37        return $this;
38    }
39
40    /**
41     * Adds an attribute group.
42     *
43     * @param Node\Attribute|Node\AttributeGroup $attribute
44     *
45     * @return $this The builder instance (for fluid interface)
46     */
47    public function addAttribute($attribute) {
48        $this->attributeGroups[] = BuilderHelpers::normalizeAttribute($attribute);
49
50        return $this;
51    }
52
53    /**
54     * Returns the built function node.
55     *
56     * @return Stmt\Function_ The built function node
57     */
58    public function getNode(): Node {
59        return new Stmt\Function_($this->name, [
60            'byRef'      => $this->returnByRef,
61            'params'     => $this->params,
62            'returnType' => $this->returnType,
63            'stmts'      => $this->stmts,
64            'attrGroups' => $this->attributeGroups,
65        ], $this->attributes);
66    }
67}
68