PluginProbe
WPIDE – File Manager & Code Editor / 3.5.9
WPIDE – File Manager & Code Editor v3.5.9
3.5.9 3.5.8 3.5.7 2.0.14 2.0.15 2.0.16 2.0.2 2.0.4 2.0.5 2.0.6 2.0.7 2.0.8 2.0.9 2.1 2.2 2.3 2.3.1 2.3.2 2.4.0 2.5 2.6 3.0 3.1 3.2 3.3 All 55 releases
wpide / vendor / nikic / php-parser / lib / PhpParser / BuilderHelpers.php

BuilderHelpers.php in WPIDE – File Manager & Code Editor 3.5.9, at vendor/nikic/php-parser/lib/PhpParser/BuilderHelpers.php

341 lines 9.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;
4
5 use PhpParser\Node\ComplexType;
6 use PhpParser\Node\Expr;
7 use PhpParser\Node\Identifier;
8 use PhpParser\Node\Name;
9 use PhpParser\Node\Name\FullyQualified;
10 use PhpParser\Node\NullableType;
11 use PhpParser\Node\Scalar;
12 use PhpParser\Node\Stmt;
13
14 /**
15 * This class defines helpers used in the implementation of builders. Don't use it directly.
16 *
17 * @internal
18 */
19 final class BuilderHelpers
20 {
21 /**
22 * Normalizes a node: Converts builder objects to nodes.
23 *
24 * @param Node|Builder $node The node to normalize
25 *
26 * @return Node The normalized node
27 */
28 public static function normalizeNode($node) : Node {
29 if ($node instanceof Builder) {
30 return $node->getNode();
31 }
32
33 if ($node instanceof Node) {
34 return $node;
35 }
36
37 throw new \LogicException('Expected node or builder object');
38 }
39
40 /**
41 * Normalizes a node to a statement.
42 *
43 * Expressions are wrapped in a Stmt\Expression node.
44 *
45 * @param Node|Builder $node The node to normalize
46 *
47 * @return Stmt The normalized statement node
48 */
49 public static function normalizeStmt($node) : Stmt {
50 $node = self::normalizeNode($node);
51 if ($node instanceof Stmt) {
52 return $node;
53 }
54
55 if ($node instanceof Expr) {
56 return new Stmt\Expression($node);
57 }
58
59 throw new \LogicException('Expected statement or expression node');
60 }
61
62 /**
63 * Normalizes strings to Identifier.
64 *
65 * @param string|Identifier $name The identifier to normalize
66 *
67 * @return Identifier The normalized identifier
68 */
69 public static function normalizeIdentifier($name) : Identifier {
70 if ($name instanceof Identifier) {
71 return $name;
72 }
73
74 if (\is_string($name)) {
75 return new Identifier($name);
76 }
77
78 throw new \LogicException('Expected string or instance of Node\Identifier');
79 }
80
81 /**
82 * Normalizes strings to Identifier, also allowing expressions.
83 *
84 * @param string|Identifier|Expr $name The identifier to normalize
85 *
86 * @return Identifier|Expr The normalized identifier or expression
87 */
88 public static function normalizeIdentifierOrExpr($name) {
89 if ($name instanceof Identifier || $name instanceof Expr) {
90 return $name;
91 }
92
93 if (\is_string($name)) {
94 return new Identifier($name);
95 }
96
97 throw new \LogicException('Expected string or instance of Node\Identifier or Node\Expr');
98 }
99
100 /**
101 * Normalizes a name: Converts string names to Name nodes.
102 *
103 * @param Name|string $name The name to normalize
104 *
105 * @return Name The normalized name
106 */
107 public static function normalizeName($name) : Name {
108 if ($name instanceof Name) {
109 return $name;
110 }
111
112 if (is_string($name)) {
113 if (!$name) {
114 throw new \LogicException('Name cannot be empty');
115 }
116
117 if ($name[0] === '\\') {
118 return new Name\FullyQualified(substr($name, 1));
119 }
120
121 if (0 === strpos($name, 'namespace\\')) {
122 return new Name\Relative(substr($name, strlen('namespace\\')));
123 }
124
125 return new Name($name);
126 }
127
128 throw new \LogicException('Name must be a string or an instance of Node\Name');
129 }
130
131 /**
132 * Normalizes a name: Converts string names to Name nodes, while also allowing expressions.
133 *
134 * @param Expr|Name|string $name The name to normalize
135 *
136 * @return Name|Expr The normalized name or expression
137 */
138 public static function normalizeNameOrExpr($name) {
139 if ($name instanceof Expr) {
140 return $name;
141 }
142
143 if (!is_string($name) && !($name instanceof Name)) {
144 throw new \LogicException(
145 'Name must be a string or an instance of Node\Name or Node\Expr'
146 );
147 }
148
149 return self::normalizeName($name);
150 }
151
152 /**
153 * Normalizes a type: Converts plain-text type names into proper AST representation.
154 *
155 * In particular, builtin types become Identifiers, custom types become Names and nullables
156 * are wrapped in NullableType nodes.
157 *
158 * @param string|Name|Identifier|ComplexType $type The type to normalize
159 *
160 * @return Name|Identifier|ComplexType The normalized type
161 */
162 public static function normalizeType($type) {
163 if (!is_string($type)) {
164 if (
165 !$type instanceof Name && !$type instanceof Identifier &&
166 !$type instanceof ComplexType
167 ) {
168 throw new \LogicException(
169 'Type must be a string, or an instance of Name, Identifier or ComplexType'
170 );
171 }
172 return $type;
173 }
174
175 $nullable = false;
176 if (strlen($type) > 0 && $type[0] === '?') {
177 $nullable = true;
178 $type = substr($type, 1);
179 }
180
181 $builtinTypes = [
182 'array',
183 'callable',
184 'bool',
185 'int',
186 'float',
187 'string',
188 'iterable',
189 'void',
190 'object',
191 'null',
192 'false',
193 'mixed',
194 'never',
195 'true',
196 ];
197
198 $lowerType = strtolower($type);
199 if (in_array($lowerType, $builtinTypes)) {
200 $type = new Identifier($lowerType);
201 } else {
202 $type = self::normalizeName($type);
203 }
204
205 $notNullableTypes = [
206 'void', 'mixed', 'never',
207 ];
208 if ($nullable && in_array((string) $type, $notNullableTypes)) {
209 throw new \LogicException(sprintf('%s type cannot be nullable', $type));
210 }
211
212 return $nullable ? new NullableType($type) : $type;
213 }
214
215 /**
216 * Normalizes a value: Converts nulls, booleans, integers,
217 * floats, strings and arrays into their respective nodes
218 *
219 * @param Node\Expr|bool|null|int|float|string|array|\UnitEnum $value The value to normalize
220 *
221 * @return Expr The normalized value
222 */
223 public static function normalizeValue($value) : Expr {
224 if ($value instanceof Node\Expr) {
225 return $value;
226 }
227
228 if (is_null($value)) {
229 return new Expr\ConstFetch(
230 new Name('null')
231 );
232 }
233
234 if (is_bool($value)) {
235 return new Expr\ConstFetch(
236 new Name($value ? 'true' : 'false')
237 );
238 }
239
240 if (is_int($value)) {
241 return new Scalar\LNumber($value);
242 }
243
244 if (is_float($value)) {
245 return new Scalar\DNumber($value);
246 }
247
248 if (is_string($value)) {
249 return new Scalar\String_($value);
250 }
251
252 if (is_array($value)) {
253 $items = [];
254 $lastKey = -1;
255 foreach ($value as $itemKey => $itemValue) {
256 // for consecutive, numeric keys don't generate keys
257 if (null !== $lastKey && ++$lastKey === $itemKey) {
258 $items[] = new Expr\ArrayItem(
259 self::normalizeValue($itemValue)
260 );
261 } else {
262 $lastKey = null;
263 $items[] = new Expr\ArrayItem(
264 self::normalizeValue($itemValue),
265 self::normalizeValue($itemKey)
266 );
267 }
268 }
269
270 return new Expr\Array_($items);
271 }
272
273 if ($value instanceof \UnitEnum) {
274 return new Expr\ClassConstFetch(new FullyQualified(\get_class($value)), new Identifier($value->name));
275 }
276
277 throw new \LogicException('Invalid value');
278 }
279
280 /**
281 * Normalizes a doc comment: Converts plain strings to PhpParser\Comment\Doc.
282 *
283 * @param Comment\Doc|string $docComment The doc comment to normalize
284 *
285 * @return Comment\Doc The normalized doc comment
286 */
287 public static function normalizeDocComment($docComment) : Comment\Doc {
288 if ($docComment instanceof Comment\Doc) {
289 return $docComment;
290 }
291
292 if (is_string($docComment)) {
293 return new Comment\Doc($docComment);
294 }
295
296 throw new \LogicException('Doc comment must be a string or an instance of PhpParser\Comment\Doc');
297 }
298
299 /**
300 * Normalizes a attribute: Converts attribute to the Attribute Group if needed.
301 *
302 * @param Node\Attribute|Node\AttributeGroup $attribute
303 *
304 * @return Node\AttributeGroup The Attribute Group
305 */
306 public static function normalizeAttribute($attribute) : Node\AttributeGroup
307 {
308 if ($attribute instanceof Node\AttributeGroup) {
309 return $attribute;
310 }
311
312 if (!($attribute instanceof Node\Attribute)) {
313 throw new \LogicException('Attribute must be an instance of PhpParser\Node\Attribute or PhpParser\Node\AttributeGroup');
314 }
315
316 return new Node\AttributeGroup([$attribute]);
317 }
318
319 /**
320 * Adds a modifier and returns new modifier bitmask.
321 *
322 * @param int $modifiers Existing modifiers
323 * @param int $modifier Modifier to set
324 *
325 * @return int New modifiers
326 */
327 public static function addModifier(int $modifiers, int $modifier) : int {
328 Stmt\Class_::verifyModifier($modifiers, $modifier);
329 return $modifiers | $modifier;
330 }
331
332 /**
333 * Adds a modifier and returns new modifier bitmask.
334 * @return int New modifiers
335 */
336 public static function addClassModifier(int $existingModifiers, int $modifierToSet) : int {
337 Stmt\Class_::verifyClassModifier($existingModifiers, $modifierToSet);
338 return $existingModifiers | $modifierToSet;
339 }
340 }
341