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: /** @var Node\AttributeGroup[] */
23: protected $attributeGroups = [];
24:
25: /**
26: * Creates a parameter builder.
27: *
28: * @param string $name Name of the parameter
29: */
30: public function __construct(string $name) {
31: $this->name = $name;
32: }
33:
34: /**
35: * Sets default value for the parameter.
36: *
37: * @param mixed $value Default value to use
38: *
39: * @return $this The builder instance (for fluid interface)
40: */
41: public function setDefault($value) {
42: $this->default = BuilderHelpers::normalizeValue($value);
43:
44: return $this;
45: }
46:
47: /**
48: * Sets type for the parameter.
49: *
50: * @param string|Node\Name|Node\Identifier|Node\ComplexType $type Parameter type
51: *
52: * @return $this The builder instance (for fluid interface)
53: */
54: public function setType($type) {
55: $this->type = BuilderHelpers::normalizeType($type);
56: if ($this->type == 'void') {
57: throw new \LogicException('Parameter type cannot be void');
58: }
59:
60: return $this;
61: }
62:
63: /**
64: * Sets type for the parameter.
65: *
66: * @param string|Node\Name|Node\Identifier|Node\ComplexType $type Parameter type
67: *
68: * @return $this The builder instance (for fluid interface)
69: *
70: * @deprecated Use setType() instead
71: */
72: public function setTypeHint($type) {
73: return $this->setType($type);
74: }
75:
76: /**
77: * Make the parameter accept the value by reference.
78: *
79: * @return $this The builder instance (for fluid interface)
80: */
81: public function makeByRef() {
82: $this->byRef = true;
83:
84: return $this;
85: }
86:
87: /**
88: * Make the parameter variadic
89: *
90: * @return $this The builder instance (for fluid interface)
91: */
92: public function makeVariadic() {
93: $this->variadic = true;
94:
95: return $this;
96: }
97:
98: /**
99: * Adds an attribute group.
100: *
101: * @param Node\Attribute|Node\AttributeGroup $attribute
102: *
103: * @return $this The builder instance (for fluid interface)
104: */
105: public function addAttribute($attribute) {
106: $this->attributeGroups[] = BuilderHelpers::normalizeAttribute($attribute);
107:
108: return $this;
109: }
110:
111: /**
112: * Returns the built parameter node.
113: *
114: * @return Node\Param The built parameter node
115: */
116: public function getNode() : Node {
117: return new Node\Param(
118: new Node\Expr\Variable($this->name),
119: $this->default, $this->type, $this->byRef, $this->variadic, [], 0, $this->attributeGroups
120: );
121: }
122: }
123: