xref: /PHP-Parser/lib/PhpParser/Builder/Param.php (revision b493c51c)
1<?php declare(strict_types=1);
2
3namespace PhpParser\Builder;
4
5use PhpParser;
6use PhpParser\BuilderHelpers;
7use PhpParser\Modifiers;
8use PhpParser\Node;
9
10class Param implements PhpParser\Builder {
11    protected string $name;
12    protected ?Node\Expr $default = null;
13    /** @var Node\Identifier|Node\Name|Node\ComplexType|null */
14    protected ?Node $type = null;
15    protected bool $byRef = false;
16    protected int $flags = 0;
17    protected bool $variadic = false;
18    /** @var list<Node\AttributeGroup> */
19    protected array $attributeGroups = [];
20
21    /**
22     * Creates a parameter builder.
23     *
24     * @param string $name Name of the parameter
25     */
26    public function __construct(string $name) {
27        $this->name = $name;
28    }
29
30    /**
31     * Sets default value for the parameter.
32     *
33     * @param mixed $value Default value to use
34     *
35     * @return $this The builder instance (for fluid interface)
36     */
37    public function setDefault($value) {
38        $this->default = BuilderHelpers::normalizeValue($value);
39
40        return $this;
41    }
42
43    /**
44     * Sets type for the parameter.
45     *
46     * @param string|Node\Name|Node\Identifier|Node\ComplexType $type Parameter type
47     *
48     * @return $this The builder instance (for fluid interface)
49     */
50    public function setType($type) {
51        $this->type = BuilderHelpers::normalizeType($type);
52        if ($this->type == 'void') {
53            throw new \LogicException('Parameter type cannot be void');
54        }
55
56        return $this;
57    }
58
59    /**
60     * Make the parameter accept the value by reference.
61     *
62     * @return $this The builder instance (for fluid interface)
63     */
64    public function makeByRef() {
65        $this->byRef = true;
66
67        return $this;
68    }
69
70    /**
71     * Make the parameter variadic
72     *
73     * @return $this The builder instance (for fluid interface)
74     */
75    public function makeVariadic() {
76        $this->variadic = true;
77
78        return $this;
79    }
80
81    /**
82     * Makes the (promoted) parameter public.
83     *
84     * @return $this The builder instance (for fluid interface)
85     */
86    public function makePublic() {
87        $this->flags = BuilderHelpers::addModifier($this->flags, Modifiers::PUBLIC);
88
89        return $this;
90    }
91
92    /**
93     * Makes the (promoted) parameter protected.
94     *
95     * @return $this The builder instance (for fluid interface)
96     */
97    public function makeProtected() {
98        $this->flags = BuilderHelpers::addModifier($this->flags, Modifiers::PROTECTED);
99
100        return $this;
101    }
102
103    /**
104     * Makes the (promoted) parameter private.
105     *
106     * @return $this The builder instance (for fluid interface)
107     */
108    public function makePrivate() {
109        $this->flags = BuilderHelpers::addModifier($this->flags, Modifiers::PRIVATE);
110
111        return $this;
112    }
113
114    /**
115     * Makes the (promoted) parameter readonly.
116     *
117     * @return $this The builder instance (for fluid interface)
118     */
119    public function makeReadonly() {
120        $this->flags = BuilderHelpers::addModifier($this->flags, Modifiers::READONLY);
121
122        return $this;
123    }
124
125    /**
126     * Gives the promoted property private(set) visibility.
127     *
128     * @return $this The builder instance (for fluid interface)
129     */
130    public function makePrivateSet() {
131        $this->flags = BuilderHelpers::addModifier($this->flags, Modifiers::PRIVATE_SET);
132
133        return $this;
134    }
135
136    /**
137     * Gives the promoted property protected(set) visibility.
138     *
139     * @return $this The builder instance (for fluid interface)
140     */
141    public function makeProtectedSet() {
142        $this->flags = BuilderHelpers::addModifier($this->flags, Modifiers::PROTECTED_SET);
143
144        return $this;
145    }
146
147    /**
148     * Adds an attribute group.
149     *
150     * @param Node\Attribute|Node\AttributeGroup $attribute
151     *
152     * @return $this The builder instance (for fluid interface)
153     */
154    public function addAttribute($attribute) {
155        $this->attributeGroups[] = BuilderHelpers::normalizeAttribute($attribute);
156
157        return $this;
158    }
159
160    /**
161     * Returns the built parameter node.
162     *
163     * @return Node\Param The built parameter node
164     */
165    public function getNode(): Node {
166        return new Node\Param(
167            new Node\Expr\Variable($this->name),
168            $this->default, $this->type, $this->byRef, $this->variadic, [], $this->flags, $this->attributeGroups
169        );
170    }
171}
172