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