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@0
|
45 * Sets type hint for the parameter.
|
Chris@0
|
46 *
|
Chris@0
|
47 * @param string|Node\Name|Node\NullableType $type Type hint to use
|
Chris@0
|
48 *
|
Chris@0
|
49 * @return $this The builder instance (for fluid interface)
|
Chris@0
|
50 */
|
Chris@0
|
51 public function setTypeHint($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@0
|
61 * Make the parameter accept the value by reference.
|
Chris@0
|
62 *
|
Chris@0
|
63 * @return $this The builder instance (for fluid interface)
|
Chris@0
|
64 */
|
Chris@0
|
65 public function makeByRef() {
|
Chris@0
|
66 $this->byRef = true;
|
Chris@0
|
67
|
Chris@0
|
68 return $this;
|
Chris@0
|
69 }
|
Chris@0
|
70
|
Chris@0
|
71 /**
|
Chris@0
|
72 * Make the parameter variadic
|
Chris@0
|
73 *
|
Chris@0
|
74 * @return $this The builder instance (for fluid interface)
|
Chris@0
|
75 */
|
Chris@0
|
76 public function makeVariadic() {
|
Chris@0
|
77 $this->variadic = true;
|
Chris@0
|
78
|
Chris@0
|
79 return $this;
|
Chris@0
|
80 }
|
Chris@0
|
81
|
Chris@0
|
82 /**
|
Chris@0
|
83 * Returns the built parameter node.
|
Chris@0
|
84 *
|
Chris@0
|
85 * @return Node\Param The built parameter node
|
Chris@0
|
86 */
|
Chris@13
|
87 public function getNode() : Node {
|
Chris@0
|
88 return new Node\Param(
|
Chris@13
|
89 new Node\Expr\Variable($this->name),
|
Chris@13
|
90 $this->default, $this->type, $this->byRef, $this->variadic
|
Chris@0
|
91 );
|
Chris@0
|
92 }
|
Chris@0
|
93 }
|