Chris@13
|
1 <?php declare(strict_types=1);
|
Chris@0
|
2
|
Chris@0
|
3 namespace PhpParser\Builder;
|
Chris@0
|
4
|
Chris@0
|
5 use PhpParser;
|
Chris@13
|
6 use PhpParser\BuilderHelpers;
|
Chris@0
|
7 use PhpParser\Node;
|
Chris@0
|
8
|
Chris@13
|
9 class Param implements PhpParser\Builder
|
Chris@0
|
10 {
|
Chris@0
|
11 protected $name;
|
Chris@0
|
12
|
Chris@0
|
13 protected $default = null;
|
Chris@0
|
14
|
Chris@0
|
15 /** @var string|Node\Name|Node\NullableType|null */
|
Chris@0
|
16 protected $type = null;
|
Chris@0
|
17
|
Chris@0
|
18 protected $byRef = false;
|
Chris@0
|
19
|
Chris@0
|
20 protected $variadic = false;
|
Chris@0
|
21
|
Chris@0
|
22 /**
|
Chris@0
|
23 * Creates a parameter builder.
|
Chris@0
|
24 *
|
Chris@0
|
25 * @param string $name Name of the parameter
|
Chris@0
|
26 */
|
Chris@13
|
27 public function __construct(string $name) {
|
Chris@0
|
28 $this->name = $name;
|
Chris@0
|
29 }
|
Chris@0
|
30
|
Chris@0
|
31 /**
|
Chris@0
|
32 * Sets default value for the parameter.
|
Chris@0
|
33 *
|
Chris@0
|
34 * @param mixed $value Default value to use
|
Chris@0
|
35 *
|
Chris@0
|
36 * @return $this The builder instance (for fluid interface)
|
Chris@0
|
37 */
|
Chris@0
|
38 public function setDefault($value) {
|
Chris@13
|
39 $this->default = BuilderHelpers::normalizeValue($value);
|
Chris@0
|
40
|
Chris@0
|
41 return $this;
|
Chris@0
|
42 }
|
Chris@0
|
43
|
Chris@0
|
44 /**
|
Chris@17
|
45 * Sets type for the parameter.
|
Chris@0
|
46 *
|
Chris@17
|
47 * @param string|Node\Name|Node\NullableType $type Parameter type
|
Chris@0
|
48 *
|
Chris@0
|
49 * @return $this The builder instance (for fluid interface)
|
Chris@0
|
50 */
|
Chris@17
|
51 public function setType($type) {
|
Chris@13
|
52 $this->type = BuilderHelpers::normalizeType($type);
|
Chris@13
|
53 if ($this->type == 'void') {
|
Chris@0
|
54 throw new \LogicException('Parameter type cannot be void');
|
Chris@0
|
55 }
|
Chris@0
|
56
|
Chris@0
|
57 return $this;
|
Chris@0
|
58 }
|
Chris@0
|
59
|
Chris@0
|
60 /**
|
Chris@17
|
61 * Sets type for the parameter.
|
Chris@17
|
62 *
|
Chris@17
|
63 * @param string|Node\Name|Node\NullableType $type Parameter type
|
Chris@17
|
64 *
|
Chris@17
|
65 * @return $this The builder instance (for fluid interface)
|
Chris@17
|
66 *
|
Chris@17
|
67 * @deprecated Use setType() instead
|
Chris@17
|
68 */
|
Chris@17
|
69 public function setTypeHint($type) {
|
Chris@17
|
70 return $this->setType($type);
|
Chris@17
|
71 }
|
Chris@17
|
72
|
Chris@17
|
73 /**
|
Chris@0
|
74 * Make the parameter accept the value by reference.
|
Chris@0
|
75 *
|
Chris@0
|
76 * @return $this The builder instance (for fluid interface)
|
Chris@0
|
77 */
|
Chris@0
|
78 public function makeByRef() {
|
Chris@0
|
79 $this->byRef = true;
|
Chris@0
|
80
|
Chris@0
|
81 return $this;
|
Chris@0
|
82 }
|
Chris@0
|
83
|
Chris@0
|
84 /**
|
Chris@0
|
85 * Make the parameter variadic
|
Chris@0
|
86 *
|
Chris@0
|
87 * @return $this The builder instance (for fluid interface)
|
Chris@0
|
88 */
|
Chris@0
|
89 public function makeVariadic() {
|
Chris@0
|
90 $this->variadic = true;
|
Chris@0
|
91
|
Chris@0
|
92 return $this;
|
Chris@0
|
93 }
|
Chris@0
|
94
|
Chris@0
|
95 /**
|
Chris@0
|
96 * Returns the built parameter node.
|
Chris@0
|
97 *
|
Chris@0
|
98 * @return Node\Param The built parameter node
|
Chris@0
|
99 */
|
Chris@13
|
100 public function getNode() : Node {
|
Chris@0
|
101 return new Node\Param(
|
Chris@13
|
102 new Node\Expr\Variable($this->name),
|
Chris@13
|
103 $this->default, $this->type, $this->byRef, $this->variadic
|
Chris@0
|
104 );
|
Chris@0
|
105 }
|
Chris@0
|
106 }
|