1: | <?php declare(strict_types=1); |
2: | |
3: | namespace PhpParser\Builder; |
4: | |
5: | use PhpParser; |
6: | use PhpParser\BuilderHelpers; |
7: | use PhpParser\Modifiers; |
8: | use PhpParser\Node; |
9: | |
10: | class Param implements PhpParser\Builder { |
11: | protected string $name; |
12: | protected ?Node\Expr $default = null; |
13: | |
14: | protected ?Node $type = null; |
15: | protected bool $byRef = false; |
16: | protected int $flags = 0; |
17: | protected bool $variadic = false; |
18: | |
19: | protected array $attributeGroups = []; |
20: | |
21: | |
22: | |
23: | |
24: | |
25: | |
26: | public function __construct(string $name) { |
27: | $this->name = $name; |
28: | } |
29: | |
30: | |
31: | |
32: | |
33: | |
34: | |
35: | |
36: | |
37: | public function setDefault($value) { |
38: | $this->default = BuilderHelpers::normalizeValue($value); |
39: | |
40: | return $this; |
41: | } |
42: | |
43: | |
44: | |
45: | |
46: | |
47: | |
48: | |
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: | |
61: | |
62: | |
63: | |
64: | public function makeByRef() { |
65: | $this->byRef = true; |
66: | |
67: | return $this; |
68: | } |
69: | |
70: | |
71: | |
72: | |
73: | |
74: | |
75: | public function makeVariadic() { |
76: | $this->variadic = true; |
77: | |
78: | return $this; |
79: | } |
80: | |
81: | |
82: | |
83: | |
84: | |
85: | |
86: | public function makePublic() { |
87: | $this->flags = BuilderHelpers::addModifier($this->flags, Modifiers::PUBLIC); |
88: | |
89: | return $this; |
90: | } |
91: | |
92: | |
93: | |
94: | |
95: | |
96: | |
97: | public function makeProtected() { |
98: | $this->flags = BuilderHelpers::addModifier($this->flags, Modifiers::PROTECTED); |
99: | |
100: | return $this; |
101: | } |
102: | |
103: | |
104: | |
105: | |
106: | |
107: | |
108: | public function makePrivate() { |
109: | $this->flags = BuilderHelpers::addModifier($this->flags, Modifiers::PRIVATE); |
110: | |
111: | return $this; |
112: | } |
113: | |
114: | |
115: | |
116: | |
117: | |
118: | |
119: | public function makeReadonly() { |
120: | $this->flags = BuilderHelpers::addModifier($this->flags, Modifiers::READONLY); |
121: | |
122: | return $this; |
123: | } |
124: | |
125: | |
126: | |
127: | |
128: | |
129: | |
130: | public function makePrivateSet() { |
131: | $this->flags = BuilderHelpers::addModifier($this->flags, Modifiers::PRIVATE_SET); |
132: | |
133: | return $this; |
134: | } |
135: | |
136: | |
137: | |
138: | |
139: | |
140: | |
141: | public function makeProtectedSet() { |
142: | $this->flags = BuilderHelpers::addModifier($this->flags, Modifiers::PROTECTED_SET); |
143: | |
144: | return $this; |
145: | } |
146: | |
147: | |
148: | |
149: | |
150: | |
151: | |
152: | |
153: | |
154: | public function addAttribute($attribute) { |
155: | $this->attributeGroups[] = BuilderHelpers::normalizeAttribute($attribute); |
156: | |
157: | return $this; |
158: | } |
159: | |
160: | |
161: | |
162: | |
163: | |
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: | |