@@ -8,20 +8,20 @@ |
||
8 | 8 | * Represents the "..." in "foo(...)" of the first-class callable syntax. |
9 | 9 | */ |
10 | 10 | class VariadicPlaceholder extends NodeAbstract { |
11 | - /** |
|
12 | - * Create a variadic argument placeholder (first-class callable syntax). |
|
13 | - * |
|
14 | - * @param array $attributes Additional attributes |
|
15 | - */ |
|
16 | - public function __construct(array $attributes = []) { |
|
17 | - $this->attributes = $attributes; |
|
18 | - } |
|
11 | + /** |
|
12 | + * Create a variadic argument placeholder (first-class callable syntax). |
|
13 | + * |
|
14 | + * @param array $attributes Additional attributes |
|
15 | + */ |
|
16 | + public function __construct(array $attributes = []) { |
|
17 | + $this->attributes = $attributes; |
|
18 | + } |
|
19 | 19 | |
20 | - public function getType(): string { |
|
21 | - return 'VariadicPlaceholder'; |
|
22 | - } |
|
20 | + public function getType(): string { |
|
21 | + return 'VariadicPlaceholder'; |
|
22 | + } |
|
23 | 23 | |
24 | - public function getSubNodeNames(): array { |
|
25 | - return []; |
|
26 | - } |
|
24 | + public function getSubNodeNames(): array { |
|
25 | + return []; |
|
26 | + } |
|
27 | 27 | } |
28 | 28 | \ No newline at end of file |
@@ -7,25 +7,25 @@ |
||
7 | 7 | |
8 | 8 | class MatchArm extends NodeAbstract |
9 | 9 | { |
10 | - /** @var null|Node\Expr[] */ |
|
11 | - public $conds; |
|
12 | - /** @var Node\Expr */ |
|
13 | - public $body; |
|
10 | + /** @var null|Node\Expr[] */ |
|
11 | + public $conds; |
|
12 | + /** @var Node\Expr */ |
|
13 | + public $body; |
|
14 | 14 | |
15 | - /** |
|
16 | - * @param null|Node\Expr[] $conds |
|
17 | - */ |
|
18 | - public function __construct($conds, Node\Expr $body, array $attributes = []) { |
|
19 | - $this->conds = $conds; |
|
20 | - $this->body = $body; |
|
21 | - $this->attributes = $attributes; |
|
22 | - } |
|
15 | + /** |
|
16 | + * @param null|Node\Expr[] $conds |
|
17 | + */ |
|
18 | + public function __construct($conds, Node\Expr $body, array $attributes = []) { |
|
19 | + $this->conds = $conds; |
|
20 | + $this->body = $body; |
|
21 | + $this->attributes = $attributes; |
|
22 | + } |
|
23 | 23 | |
24 | - public function getSubNodeNames() : array { |
|
25 | - return ['conds', 'body']; |
|
26 | - } |
|
24 | + public function getSubNodeNames() : array { |
|
25 | + return ['conds', 'body']; |
|
26 | + } |
|
27 | 27 | |
28 | - public function getType() : string { |
|
29 | - return 'MatchArm'; |
|
30 | - } |
|
28 | + public function getType() : string { |
|
29 | + return 'MatchArm'; |
|
30 | + } |
|
31 | 31 | } |
@@ -4,25 +4,25 @@ |
||
4 | 4 | |
5 | 5 | class NullableType extends ComplexType |
6 | 6 | { |
7 | - /** @var Identifier|Name Type */ |
|
8 | - public $type; |
|
7 | + /** @var Identifier|Name Type */ |
|
8 | + public $type; |
|
9 | 9 | |
10 | - /** |
|
11 | - * Constructs a nullable type (wrapping another type). |
|
12 | - * |
|
13 | - * @param string|Identifier|Name $type Type |
|
14 | - * @param array $attributes Additional attributes |
|
15 | - */ |
|
16 | - public function __construct($type, array $attributes = []) { |
|
17 | - $this->attributes = $attributes; |
|
18 | - $this->type = \is_string($type) ? new Identifier($type) : $type; |
|
19 | - } |
|
10 | + /** |
|
11 | + * Constructs a nullable type (wrapping another type). |
|
12 | + * |
|
13 | + * @param string|Identifier|Name $type Type |
|
14 | + * @param array $attributes Additional attributes |
|
15 | + */ |
|
16 | + public function __construct($type, array $attributes = []) { |
|
17 | + $this->attributes = $attributes; |
|
18 | + $this->type = \is_string($type) ? new Identifier($type) : $type; |
|
19 | + } |
|
20 | 20 | |
21 | - public function getSubNodeNames() : array { |
|
22 | - return ['type']; |
|
23 | - } |
|
21 | + public function getSubNodeNames() : array { |
|
22 | + return ['type']; |
|
23 | + } |
|
24 | 24 | |
25 | - public function getType() : string { |
|
26 | - return 'NullableType'; |
|
27 | - } |
|
25 | + public function getType() : string { |
|
26 | + return 'NullableType'; |
|
27 | + } |
|
28 | 28 | } |
@@ -11,7 +11,7 @@ |
||
11 | 11 | */ |
12 | 12 | class VarLikeIdentifier extends Identifier |
13 | 13 | { |
14 | - public function getType() : string { |
|
15 | - return 'VarLikeIdentifier'; |
|
16 | - } |
|
14 | + public function getType() : string { |
|
15 | + return 'VarLikeIdentifier'; |
|
16 | + } |
|
17 | 17 | } |
@@ -7,40 +7,40 @@ |
||
7 | 7 | |
8 | 8 | class Arg extends NodeAbstract |
9 | 9 | { |
10 | - /** @var Identifier|null Parameter name (for named parameters) */ |
|
11 | - public $name; |
|
12 | - /** @var Expr Value to pass */ |
|
13 | - public $value; |
|
14 | - /** @var bool Whether to pass by ref */ |
|
15 | - public $byRef; |
|
16 | - /** @var bool Whether to unpack the argument */ |
|
17 | - public $unpack; |
|
10 | + /** @var Identifier|null Parameter name (for named parameters) */ |
|
11 | + public $name; |
|
12 | + /** @var Expr Value to pass */ |
|
13 | + public $value; |
|
14 | + /** @var bool Whether to pass by ref */ |
|
15 | + public $byRef; |
|
16 | + /** @var bool Whether to unpack the argument */ |
|
17 | + public $unpack; |
|
18 | 18 | |
19 | - /** |
|
20 | - * Constructs a function call argument node. |
|
21 | - * |
|
22 | - * @param Expr $value Value to pass |
|
23 | - * @param bool $byRef Whether to pass by ref |
|
24 | - * @param bool $unpack Whether to unpack the argument |
|
25 | - * @param array $attributes Additional attributes |
|
26 | - * @param Identifier|null $name Parameter name (for named parameters) |
|
27 | - */ |
|
28 | - public function __construct( |
|
29 | - Expr $value, bool $byRef = false, bool $unpack = false, array $attributes = [], |
|
30 | - Identifier $name = null |
|
31 | - ) { |
|
32 | - $this->attributes = $attributes; |
|
33 | - $this->name = $name; |
|
34 | - $this->value = $value; |
|
35 | - $this->byRef = $byRef; |
|
36 | - $this->unpack = $unpack; |
|
37 | - } |
|
19 | + /** |
|
20 | + * Constructs a function call argument node. |
|
21 | + * |
|
22 | + * @param Expr $value Value to pass |
|
23 | + * @param bool $byRef Whether to pass by ref |
|
24 | + * @param bool $unpack Whether to unpack the argument |
|
25 | + * @param array $attributes Additional attributes |
|
26 | + * @param Identifier|null $name Parameter name (for named parameters) |
|
27 | + */ |
|
28 | + public function __construct( |
|
29 | + Expr $value, bool $byRef = false, bool $unpack = false, array $attributes = [], |
|
30 | + Identifier $name = null |
|
31 | + ) { |
|
32 | + $this->attributes = $attributes; |
|
33 | + $this->name = $name; |
|
34 | + $this->value = $value; |
|
35 | + $this->byRef = $byRef; |
|
36 | + $this->unpack = $unpack; |
|
37 | + } |
|
38 | 38 | |
39 | - public function getSubNodeNames() : array { |
|
40 | - return ['name', 'value', 'byRef', 'unpack']; |
|
41 | - } |
|
39 | + public function getSubNodeNames() : array { |
|
40 | + return ['name', 'value', 'byRef', 'unpack']; |
|
41 | + } |
|
42 | 42 | |
43 | - public function getType() : string { |
|
44 | - return 'Arg'; |
|
45 | - } |
|
43 | + public function getType() : string { |
|
44 | + return 'Arg'; |
|
45 | + } |
|
46 | 46 | } |
@@ -6,237 +6,237 @@ |
||
6 | 6 | |
7 | 7 | class Name extends NodeAbstract |
8 | 8 | { |
9 | - /** @var string[] Parts of the name */ |
|
10 | - public $parts; |
|
11 | - |
|
12 | - private static $specialClassNames = [ |
|
13 | - 'self' => true, |
|
14 | - 'parent' => true, |
|
15 | - 'static' => true, |
|
16 | - ]; |
|
17 | - |
|
18 | - /** |
|
19 | - * Constructs a name node. |
|
20 | - * |
|
21 | - * @param string|string[]|self $name Name as string, part array or Name instance (copy ctor) |
|
22 | - * @param array $attributes Additional attributes |
|
23 | - */ |
|
24 | - public function __construct($name, array $attributes = []) { |
|
25 | - $this->attributes = $attributes; |
|
26 | - $this->parts = self::prepareName($name); |
|
27 | - } |
|
28 | - |
|
29 | - public function getSubNodeNames() : array { |
|
30 | - return ['parts']; |
|
31 | - } |
|
32 | - |
|
33 | - /** |
|
34 | - * Gets the first part of the name, i.e. everything before the first namespace separator. |
|
35 | - * |
|
36 | - * @return string First part of the name |
|
37 | - */ |
|
38 | - public function getFirst() : string { |
|
39 | - return $this->parts[0]; |
|
40 | - } |
|
41 | - |
|
42 | - /** |
|
43 | - * Gets the last part of the name, i.e. everything after the last namespace separator. |
|
44 | - * |
|
45 | - * @return string Last part of the name |
|
46 | - */ |
|
47 | - public function getLast() : string { |
|
48 | - return $this->parts[count($this->parts) - 1]; |
|
49 | - } |
|
50 | - |
|
51 | - /** |
|
52 | - * Checks whether the name is unqualified. (E.g. Name) |
|
53 | - * |
|
54 | - * @return bool Whether the name is unqualified |
|
55 | - */ |
|
56 | - public function isUnqualified() : bool { |
|
57 | - return 1 === count($this->parts); |
|
58 | - } |
|
59 | - |
|
60 | - /** |
|
61 | - * Checks whether the name is qualified. (E.g. Name\Name) |
|
62 | - * |
|
63 | - * @return bool Whether the name is qualified |
|
64 | - */ |
|
65 | - public function isQualified() : bool { |
|
66 | - return 1 < count($this->parts); |
|
67 | - } |
|
68 | - |
|
69 | - /** |
|
70 | - * Checks whether the name is fully qualified. (E.g. \Name) |
|
71 | - * |
|
72 | - * @return bool Whether the name is fully qualified |
|
73 | - */ |
|
74 | - public function isFullyQualified() : bool { |
|
75 | - return false; |
|
76 | - } |
|
77 | - |
|
78 | - /** |
|
79 | - * Checks whether the name is explicitly relative to the current namespace. (E.g. namespace\Name) |
|
80 | - * |
|
81 | - * @return bool Whether the name is relative |
|
82 | - */ |
|
83 | - public function isRelative() : bool { |
|
84 | - return false; |
|
85 | - } |
|
86 | - |
|
87 | - /** |
|
88 | - * Returns a string representation of the name itself, without taking the name type into |
|
89 | - * account (e.g., not including a leading backslash for fully qualified names). |
|
90 | - * |
|
91 | - * @return string String representation |
|
92 | - */ |
|
93 | - public function toString() : string { |
|
94 | - return implode('\\', $this->parts); |
|
95 | - } |
|
96 | - |
|
97 | - /** |
|
98 | - * Returns a string representation of the name as it would occur in code (e.g., including |
|
99 | - * leading backslash for fully qualified names. |
|
100 | - * |
|
101 | - * @return string String representation |
|
102 | - */ |
|
103 | - public function toCodeString() : string { |
|
104 | - return $this->toString(); |
|
105 | - } |
|
106 | - |
|
107 | - /** |
|
108 | - * Returns lowercased string representation of the name, without taking the name type into |
|
109 | - * account (e.g., no leading backslash for fully qualified names). |
|
110 | - * |
|
111 | - * @return string Lowercased string representation |
|
112 | - */ |
|
113 | - public function toLowerString() : string { |
|
114 | - return strtolower(implode('\\', $this->parts)); |
|
115 | - } |
|
116 | - |
|
117 | - /** |
|
118 | - * Checks whether the identifier is a special class name (self, parent or static). |
|
119 | - * |
|
120 | - * @return bool Whether identifier is a special class name |
|
121 | - */ |
|
122 | - public function isSpecialClassName() : bool { |
|
123 | - return count($this->parts) === 1 |
|
124 | - && isset(self::$specialClassNames[strtolower($this->parts[0])]); |
|
125 | - } |
|
126 | - |
|
127 | - /** |
|
128 | - * Returns a string representation of the name by imploding the namespace parts with the |
|
129 | - * namespace separator. |
|
130 | - * |
|
131 | - * @return string String representation |
|
132 | - */ |
|
133 | - public function __toString() : string { |
|
134 | - return implode('\\', $this->parts); |
|
135 | - } |
|
136 | - |
|
137 | - /** |
|
138 | - * Gets a slice of a name (similar to array_slice). |
|
139 | - * |
|
140 | - * This method returns a new instance of the same type as the original and with the same |
|
141 | - * attributes. |
|
142 | - * |
|
143 | - * If the slice is empty, null is returned. The null value will be correctly handled in |
|
144 | - * concatenations using concat(). |
|
145 | - * |
|
146 | - * Offset and length have the same meaning as in array_slice(). |
|
147 | - * |
|
148 | - * @param int $offset Offset to start the slice at (may be negative) |
|
149 | - * @param int|null $length Length of the slice (may be negative) |
|
150 | - * |
|
151 | - * @return static|null Sliced name |
|
152 | - */ |
|
153 | - public function slice(int $offset, int $length = null) { |
|
154 | - $numParts = count($this->parts); |
|
155 | - |
|
156 | - $realOffset = $offset < 0 ? $offset + $numParts : $offset; |
|
157 | - if ($realOffset < 0 || $realOffset > $numParts) { |
|
158 | - throw new \OutOfBoundsException(sprintf('Offset %d is out of bounds', $offset)); |
|
159 | - } |
|
160 | - |
|
161 | - if (null === $length) { |
|
162 | - $realLength = $numParts - $realOffset; |
|
163 | - } else { |
|
164 | - $realLength = $length < 0 ? $length + $numParts - $realOffset : $length; |
|
165 | - if ($realLength < 0 || $realLength > $numParts) { |
|
166 | - throw new \OutOfBoundsException(sprintf('Length %d is out of bounds', $length)); |
|
167 | - } |
|
168 | - } |
|
169 | - |
|
170 | - if ($realLength === 0) { |
|
171 | - // Empty slice is represented as null |
|
172 | - return null; |
|
173 | - } |
|
174 | - |
|
175 | - return new static(array_slice($this->parts, $realOffset, $realLength), $this->attributes); |
|
176 | - } |
|
177 | - |
|
178 | - /** |
|
179 | - * Concatenate two names, yielding a new Name instance. |
|
180 | - * |
|
181 | - * The type of the generated instance depends on which class this method is called on, for |
|
182 | - * example Name\FullyQualified::concat() will yield a Name\FullyQualified instance. |
|
183 | - * |
|
184 | - * If one of the arguments is null, a new instance of the other name will be returned. If both |
|
185 | - * arguments are null, null will be returned. As such, writing |
|
186 | - * Name::concat($namespace, $shortName) |
|
187 | - * where $namespace is a Name node or null will work as expected. |
|
188 | - * |
|
189 | - * @param string|string[]|self|null $name1 The first name |
|
190 | - * @param string|string[]|self|null $name2 The second name |
|
191 | - * @param array $attributes Attributes to assign to concatenated name |
|
192 | - * |
|
193 | - * @return static|null Concatenated name |
|
194 | - */ |
|
195 | - public static function concat($name1, $name2, array $attributes = []) { |
|
196 | - if (null === $name1 && null === $name2) { |
|
197 | - return null; |
|
198 | - } elseif (null === $name1) { |
|
199 | - return new static(self::prepareName($name2), $attributes); |
|
200 | - } elseif (null === $name2) { |
|
201 | - return new static(self::prepareName($name1), $attributes); |
|
202 | - } else { |
|
203 | - return new static( |
|
204 | - array_merge(self::prepareName($name1), self::prepareName($name2)), $attributes |
|
205 | - ); |
|
206 | - } |
|
207 | - } |
|
208 | - |
|
209 | - /** |
|
210 | - * Prepares a (string, array or Name node) name for use in name changing methods by converting |
|
211 | - * it to an array. |
|
212 | - * |
|
213 | - * @param string|string[]|self $name Name to prepare |
|
214 | - * |
|
215 | - * @return string[] Prepared name |
|
216 | - */ |
|
217 | - private static function prepareName($name) : array { |
|
218 | - if (\is_string($name)) { |
|
219 | - if ('' === $name) { |
|
220 | - throw new \InvalidArgumentException('Name cannot be empty'); |
|
221 | - } |
|
222 | - |
|
223 | - return explode('\\', $name); |
|
224 | - } elseif (\is_array($name)) { |
|
225 | - if (empty($name)) { |
|
226 | - throw new \InvalidArgumentException('Name cannot be empty'); |
|
227 | - } |
|
228 | - |
|
229 | - return $name; |
|
230 | - } elseif ($name instanceof self) { |
|
231 | - return $name->parts; |
|
232 | - } |
|
233 | - |
|
234 | - throw new \InvalidArgumentException( |
|
235 | - 'Expected string, array of parts or Name instance' |
|
236 | - ); |
|
237 | - } |
|
238 | - |
|
239 | - public function getType() : string { |
|
240 | - return 'Name'; |
|
241 | - } |
|
9 | + /** @var string[] Parts of the name */ |
|
10 | + public $parts; |
|
11 | + |
|
12 | + private static $specialClassNames = [ |
|
13 | + 'self' => true, |
|
14 | + 'parent' => true, |
|
15 | + 'static' => true, |
|
16 | + ]; |
|
17 | + |
|
18 | + /** |
|
19 | + * Constructs a name node. |
|
20 | + * |
|
21 | + * @param string|string[]|self $name Name as string, part array or Name instance (copy ctor) |
|
22 | + * @param array $attributes Additional attributes |
|
23 | + */ |
|
24 | + public function __construct($name, array $attributes = []) { |
|
25 | + $this->attributes = $attributes; |
|
26 | + $this->parts = self::prepareName($name); |
|
27 | + } |
|
28 | + |
|
29 | + public function getSubNodeNames() : array { |
|
30 | + return ['parts']; |
|
31 | + } |
|
32 | + |
|
33 | + /** |
|
34 | + * Gets the first part of the name, i.e. everything before the first namespace separator. |
|
35 | + * |
|
36 | + * @return string First part of the name |
|
37 | + */ |
|
38 | + public function getFirst() : string { |
|
39 | + return $this->parts[0]; |
|
40 | + } |
|
41 | + |
|
42 | + /** |
|
43 | + * Gets the last part of the name, i.e. everything after the last namespace separator. |
|
44 | + * |
|
45 | + * @return string Last part of the name |
|
46 | + */ |
|
47 | + public function getLast() : string { |
|
48 | + return $this->parts[count($this->parts) - 1]; |
|
49 | + } |
|
50 | + |
|
51 | + /** |
|
52 | + * Checks whether the name is unqualified. (E.g. Name) |
|
53 | + * |
|
54 | + * @return bool Whether the name is unqualified |
|
55 | + */ |
|
56 | + public function isUnqualified() : bool { |
|
57 | + return 1 === count($this->parts); |
|
58 | + } |
|
59 | + |
|
60 | + /** |
|
61 | + * Checks whether the name is qualified. (E.g. Name\Name) |
|
62 | + * |
|
63 | + * @return bool Whether the name is qualified |
|
64 | + */ |
|
65 | + public function isQualified() : bool { |
|
66 | + return 1 < count($this->parts); |
|
67 | + } |
|
68 | + |
|
69 | + /** |
|
70 | + * Checks whether the name is fully qualified. (E.g. \Name) |
|
71 | + * |
|
72 | + * @return bool Whether the name is fully qualified |
|
73 | + */ |
|
74 | + public function isFullyQualified() : bool { |
|
75 | + return false; |
|
76 | + } |
|
77 | + |
|
78 | + /** |
|
79 | + * Checks whether the name is explicitly relative to the current namespace. (E.g. namespace\Name) |
|
80 | + * |
|
81 | + * @return bool Whether the name is relative |
|
82 | + */ |
|
83 | + public function isRelative() : bool { |
|
84 | + return false; |
|
85 | + } |
|
86 | + |
|
87 | + /** |
|
88 | + * Returns a string representation of the name itself, without taking the name type into |
|
89 | + * account (e.g., not including a leading backslash for fully qualified names). |
|
90 | + * |
|
91 | + * @return string String representation |
|
92 | + */ |
|
93 | + public function toString() : string { |
|
94 | + return implode('\\', $this->parts); |
|
95 | + } |
|
96 | + |
|
97 | + /** |
|
98 | + * Returns a string representation of the name as it would occur in code (e.g., including |
|
99 | + * leading backslash for fully qualified names. |
|
100 | + * |
|
101 | + * @return string String representation |
|
102 | + */ |
|
103 | + public function toCodeString() : string { |
|
104 | + return $this->toString(); |
|
105 | + } |
|
106 | + |
|
107 | + /** |
|
108 | + * Returns lowercased string representation of the name, without taking the name type into |
|
109 | + * account (e.g., no leading backslash for fully qualified names). |
|
110 | + * |
|
111 | + * @return string Lowercased string representation |
|
112 | + */ |
|
113 | + public function toLowerString() : string { |
|
114 | + return strtolower(implode('\\', $this->parts)); |
|
115 | + } |
|
116 | + |
|
117 | + /** |
|
118 | + * Checks whether the identifier is a special class name (self, parent or static). |
|
119 | + * |
|
120 | + * @return bool Whether identifier is a special class name |
|
121 | + */ |
|
122 | + public function isSpecialClassName() : bool { |
|
123 | + return count($this->parts) === 1 |
|
124 | + && isset(self::$specialClassNames[strtolower($this->parts[0])]); |
|
125 | + } |
|
126 | + |
|
127 | + /** |
|
128 | + * Returns a string representation of the name by imploding the namespace parts with the |
|
129 | + * namespace separator. |
|
130 | + * |
|
131 | + * @return string String representation |
|
132 | + */ |
|
133 | + public function __toString() : string { |
|
134 | + return implode('\\', $this->parts); |
|
135 | + } |
|
136 | + |
|
137 | + /** |
|
138 | + * Gets a slice of a name (similar to array_slice). |
|
139 | + * |
|
140 | + * This method returns a new instance of the same type as the original and with the same |
|
141 | + * attributes. |
|
142 | + * |
|
143 | + * If the slice is empty, null is returned. The null value will be correctly handled in |
|
144 | + * concatenations using concat(). |
|
145 | + * |
|
146 | + * Offset and length have the same meaning as in array_slice(). |
|
147 | + * |
|
148 | + * @param int $offset Offset to start the slice at (may be negative) |
|
149 | + * @param int|null $length Length of the slice (may be negative) |
|
150 | + * |
|
151 | + * @return static|null Sliced name |
|
152 | + */ |
|
153 | + public function slice(int $offset, int $length = null) { |
|
154 | + $numParts = count($this->parts); |
|
155 | + |
|
156 | + $realOffset = $offset < 0 ? $offset + $numParts : $offset; |
|
157 | + if ($realOffset < 0 || $realOffset > $numParts) { |
|
158 | + throw new \OutOfBoundsException(sprintf('Offset %d is out of bounds', $offset)); |
|
159 | + } |
|
160 | + |
|
161 | + if (null === $length) { |
|
162 | + $realLength = $numParts - $realOffset; |
|
163 | + } else { |
|
164 | + $realLength = $length < 0 ? $length + $numParts - $realOffset : $length; |
|
165 | + if ($realLength < 0 || $realLength > $numParts) { |
|
166 | + throw new \OutOfBoundsException(sprintf('Length %d is out of bounds', $length)); |
|
167 | + } |
|
168 | + } |
|
169 | + |
|
170 | + if ($realLength === 0) { |
|
171 | + // Empty slice is represented as null |
|
172 | + return null; |
|
173 | + } |
|
174 | + |
|
175 | + return new static(array_slice($this->parts, $realOffset, $realLength), $this->attributes); |
|
176 | + } |
|
177 | + |
|
178 | + /** |
|
179 | + * Concatenate two names, yielding a new Name instance. |
|
180 | + * |
|
181 | + * The type of the generated instance depends on which class this method is called on, for |
|
182 | + * example Name\FullyQualified::concat() will yield a Name\FullyQualified instance. |
|
183 | + * |
|
184 | + * If one of the arguments is null, a new instance of the other name will be returned. If both |
|
185 | + * arguments are null, null will be returned. As such, writing |
|
186 | + * Name::concat($namespace, $shortName) |
|
187 | + * where $namespace is a Name node or null will work as expected. |
|
188 | + * |
|
189 | + * @param string|string[]|self|null $name1 The first name |
|
190 | + * @param string|string[]|self|null $name2 The second name |
|
191 | + * @param array $attributes Attributes to assign to concatenated name |
|
192 | + * |
|
193 | + * @return static|null Concatenated name |
|
194 | + */ |
|
195 | + public static function concat($name1, $name2, array $attributes = []) { |
|
196 | + if (null === $name1 && null === $name2) { |
|
197 | + return null; |
|
198 | + } elseif (null === $name1) { |
|
199 | + return new static(self::prepareName($name2), $attributes); |
|
200 | + } elseif (null === $name2) { |
|
201 | + return new static(self::prepareName($name1), $attributes); |
|
202 | + } else { |
|
203 | + return new static( |
|
204 | + array_merge(self::prepareName($name1), self::prepareName($name2)), $attributes |
|
205 | + ); |
|
206 | + } |
|
207 | + } |
|
208 | + |
|
209 | + /** |
|
210 | + * Prepares a (string, array or Name node) name for use in name changing methods by converting |
|
211 | + * it to an array. |
|
212 | + * |
|
213 | + * @param string|string[]|self $name Name to prepare |
|
214 | + * |
|
215 | + * @return string[] Prepared name |
|
216 | + */ |
|
217 | + private static function prepareName($name) : array { |
|
218 | + if (\is_string($name)) { |
|
219 | + if ('' === $name) { |
|
220 | + throw new \InvalidArgumentException('Name cannot be empty'); |
|
221 | + } |
|
222 | + |
|
223 | + return explode('\\', $name); |
|
224 | + } elseif (\is_array($name)) { |
|
225 | + if (empty($name)) { |
|
226 | + throw new \InvalidArgumentException('Name cannot be empty'); |
|
227 | + } |
|
228 | + |
|
229 | + return $name; |
|
230 | + } elseif ($name instanceof self) { |
|
231 | + return $name->parts; |
|
232 | + } |
|
233 | + |
|
234 | + throw new \InvalidArgumentException( |
|
235 | + 'Expected string, array of parts or Name instance' |
|
236 | + ); |
|
237 | + } |
|
238 | + |
|
239 | + public function getType() : string { |
|
240 | + return 'Name'; |
|
241 | + } |
|
242 | 242 | } |
@@ -9,29 +9,29 @@ |
||
9 | 9 | */ |
10 | 10 | class Const_ extends NodeAbstract |
11 | 11 | { |
12 | - /** @var Identifier Name */ |
|
13 | - public $name; |
|
14 | - /** @var Expr Value */ |
|
15 | - public $value; |
|
12 | + /** @var Identifier Name */ |
|
13 | + public $name; |
|
14 | + /** @var Expr Value */ |
|
15 | + public $value; |
|
16 | 16 | |
17 | - /** |
|
18 | - * Constructs a const node for use in class const and const statements. |
|
19 | - * |
|
20 | - * @param string|Identifier $name Name |
|
21 | - * @param Expr $value Value |
|
22 | - * @param array $attributes Additional attributes |
|
23 | - */ |
|
24 | - public function __construct($name, Expr $value, array $attributes = []) { |
|
25 | - $this->attributes = $attributes; |
|
26 | - $this->name = \is_string($name) ? new Identifier($name) : $name; |
|
27 | - $this->value = $value; |
|
28 | - } |
|
17 | + /** |
|
18 | + * Constructs a const node for use in class const and const statements. |
|
19 | + * |
|
20 | + * @param string|Identifier $name Name |
|
21 | + * @param Expr $value Value |
|
22 | + * @param array $attributes Additional attributes |
|
23 | + */ |
|
24 | + public function __construct($name, Expr $value, array $attributes = []) { |
|
25 | + $this->attributes = $attributes; |
|
26 | + $this->name = \is_string($name) ? new Identifier($name) : $name; |
|
27 | + $this->value = $value; |
|
28 | + } |
|
29 | 29 | |
30 | - public function getSubNodeNames() : array { |
|
31 | - return ['name', 'value']; |
|
32 | - } |
|
30 | + public function getSubNodeNames() : array { |
|
31 | + return ['name', 'value']; |
|
32 | + } |
|
33 | 33 | |
34 | - public function getType() : string { |
|
35 | - return 'Const'; |
|
36 | - } |
|
34 | + public function getType() : string { |
|
35 | + return 'Const'; |
|
36 | + } |
|
37 | 37 | } |
@@ -6,25 +6,25 @@ |
||
6 | 6 | |
7 | 7 | class IntersectionType extends ComplexType |
8 | 8 | { |
9 | - /** @var (Identifier|Name)[] Types */ |
|
10 | - public $types; |
|
9 | + /** @var (Identifier|Name)[] Types */ |
|
10 | + public $types; |
|
11 | 11 | |
12 | - /** |
|
13 | - * Constructs an intersection type. |
|
14 | - * |
|
15 | - * @param (Identifier|Name)[] $types Types |
|
16 | - * @param array $attributes Additional attributes |
|
17 | - */ |
|
18 | - public function __construct(array $types, array $attributes = []) { |
|
19 | - $this->attributes = $attributes; |
|
20 | - $this->types = $types; |
|
21 | - } |
|
12 | + /** |
|
13 | + * Constructs an intersection type. |
|
14 | + * |
|
15 | + * @param (Identifier|Name)[] $types Types |
|
16 | + * @param array $attributes Additional attributes |
|
17 | + */ |
|
18 | + public function __construct(array $types, array $attributes = []) { |
|
19 | + $this->attributes = $attributes; |
|
20 | + $this->types = $types; |
|
21 | + } |
|
22 | 22 | |
23 | - public function getSubNodeNames() : array { |
|
24 | - return ['types']; |
|
25 | - } |
|
23 | + public function getSubNodeNames() : array { |
|
24 | + return ['types']; |
|
25 | + } |
|
26 | 26 | |
27 | - public function getType() : string { |
|
28 | - return 'IntersectionType'; |
|
29 | - } |
|
27 | + public function getType() : string { |
|
28 | + return 'IntersectionType'; |
|
29 | + } |
|
30 | 30 | } |
@@ -6,25 +6,25 @@ |
||
6 | 6 | |
7 | 7 | class Eval_ extends Expr |
8 | 8 | { |
9 | - /** @var Expr Expression */ |
|
10 | - public $expr; |
|
9 | + /** @var Expr Expression */ |
|
10 | + public $expr; |
|
11 | 11 | |
12 | - /** |
|
13 | - * Constructs an eval() node. |
|
14 | - * |
|
15 | - * @param Expr $expr Expression |
|
16 | - * @param array $attributes Additional attributes |
|
17 | - */ |
|
18 | - public function __construct(Expr $expr, array $attributes = []) { |
|
19 | - $this->attributes = $attributes; |
|
20 | - $this->expr = $expr; |
|
21 | - } |
|
12 | + /** |
|
13 | + * Constructs an eval() node. |
|
14 | + * |
|
15 | + * @param Expr $expr Expression |
|
16 | + * @param array $attributes Additional attributes |
|
17 | + */ |
|
18 | + public function __construct(Expr $expr, array $attributes = []) { |
|
19 | + $this->attributes = $attributes; |
|
20 | + $this->expr = $expr; |
|
21 | + } |
|
22 | 22 | |
23 | - public function getSubNodeNames() : array { |
|
24 | - return ['expr']; |
|
25 | - } |
|
23 | + public function getSubNodeNames() : array { |
|
24 | + return ['expr']; |
|
25 | + } |
|
26 | 26 | |
27 | - public function getType() : string { |
|
28 | - return 'Expr_Eval'; |
|
29 | - } |
|
27 | + public function getType() : string { |
|
28 | + return 'Expr_Eval'; |
|
29 | + } |
|
30 | 30 | } |