PluginProbe
Code Engine – PHP Snippets, AI Functions & Automation for WordPress / 0.4.2
Code Engine – PHP Snippets, AI Functions & Automation for WordPress v0.4.2
0.5.7 0.5.6 0.5.5 0.5.4 0.5.3 0.5.2 0.5.1 0.5.0 0.4.9 0.4.8 0.4.7 0.4.6 trunk 0.0.1 0.0.2 0.2.8 0.2.9 0.3.0 0.3.1 0.3.2 0.3.3 0.3.4 0.3.5 0.3.6 0.3.7 All 33 releases
code-engine / vendor / nikic / php-parser / lib / PhpParser / Builder / Param.php

Param.php in Code Engine – PHP Snippets, AI Functions & Automation for WordPress 0.4.2, at vendor/nikic/php-parser/lib/PhpParser/Builder/Param.php

150 lines 3.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 /** @var Node\Identifier|Node\Name|Node\ComplexType|null */
14 protected ?Node $type = null;
15 protected bool $byRef = false;
16 protected int $flags = 0;
17 protected bool $variadic = false;
18 /** @var list<Node\AttributeGroup> */
19 protected array $attributeGroups = [];
20
21 /**
22 * Creates a parameter builder.
23 *
24 * @param string $name Name of the parameter
25 */
26 public function __construct(string $name) {
27 $this->name = $name;
28 }
29
30 /**
31 * Sets default value for the parameter.
32 *
33 * @param mixed $value Default value to use
34 *
35 * @return $this The builder instance (for fluid interface)
36 */
37 public function setDefault($value) {
38 $this->default = BuilderHelpers::normalizeValue($value);
39
40 return $this;
41 }
42
43 /**
44 * Sets type for the parameter.
45 *
46 * @param string|Node\Name|Node\Identifier|Node\ComplexType $type Parameter type
47 *
48 * @return $this The builder instance (for fluid interface)
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 * Make the parameter accept the value by reference.
61 *
62 * @return $this The builder instance (for fluid interface)
63 */
64 public function makeByRef() {
65 $this->byRef = true;
66
67 return $this;
68 }
69
70 /**
71 * Make the parameter variadic
72 *
73 * @return $this The builder instance (for fluid interface)
74 */
75 public function makeVariadic() {
76 $this->variadic = true;
77
78 return $this;
79 }
80
81 /**
82 * Makes the (promoted) parameter public.
83 *
84 * @return $this The builder instance (for fluid interface)
85 */
86 public function makePublic() {
87 $this->flags = BuilderHelpers::addModifier($this->flags, Modifiers::PUBLIC);
88
89 return $this;
90 }
91
92 /**
93 * Makes the (promoted) parameter protected.
94 *
95 * @return $this The builder instance (for fluid interface)
96 */
97 public function makeProtected() {
98 $this->flags = BuilderHelpers::addModifier($this->flags, Modifiers::PROTECTED);
99
100 return $this;
101 }
102
103 /**
104 * Makes the (promoted) parameter private.
105 *
106 * @return $this The builder instance (for fluid interface)
107 */
108 public function makePrivate() {
109 $this->flags = BuilderHelpers::addModifier($this->flags, Modifiers::PRIVATE);
110
111 return $this;
112 }
113
114 /**
115 * Makes the (promoted) parameter readonly.
116 *
117 * @return $this The builder instance (for fluid interface)
118 */
119 public function makeReadonly() {
120 $this->flags = BuilderHelpers::addModifier($this->flags, Modifiers::READONLY);
121
122 return $this;
123 }
124
125 /**
126 * Adds an attribute group.
127 *
128 * @param Node\Attribute|Node\AttributeGroup $attribute
129 *
130 * @return $this The builder instance (for fluid interface)
131 */
132 public function addAttribute($attribute) {
133 $this->attributeGroups[] = BuilderHelpers::normalizeAttribute($attribute);
134
135 return $this;
136 }
137
138 /**
139 * Returns the built parameter node.
140 *
141 * @return Node\Param The built parameter node
142 */
143 public function getNode(): Node {
144 return new Node\Param(
145 new Node\Expr\Variable($this->name),
146 $this->default, $this->type, $this->byRef, $this->variadic, [], $this->flags, $this->attributeGroups
147 );
148 }
149 }
150