@@ -22,11 +22,11 @@ |
||
22 | 22 | */ |
23 | 23 | final class Null_ implements Type |
24 | 24 | { |
25 | - /** |
|
26 | - * Returns a rendered output of the Type as it would be used in a DocBlock. |
|
27 | - */ |
|
28 | - public function __toString(): string |
|
29 | - { |
|
30 | - return 'null'; |
|
31 | - } |
|
25 | + /** |
|
26 | + * Returns a rendered output of the Type as it would be used in a DocBlock. |
|
27 | + */ |
|
28 | + public function __toString(): string |
|
29 | + { |
|
30 | + return 'null'; |
|
31 | + } |
|
32 | 32 | } |
@@ -29,11 +29,11 @@ |
||
29 | 29 | */ |
30 | 30 | final class Static_ implements Type |
31 | 31 | { |
32 | - /** |
|
33 | - * Returns a rendered output of the Type as it would be used in a DocBlock. |
|
34 | - */ |
|
35 | - public function __toString(): string |
|
36 | - { |
|
37 | - return 'static'; |
|
38 | - } |
|
32 | + /** |
|
33 | + * Returns a rendered output of the Type as it would be used in a DocBlock. |
|
34 | + */ |
|
35 | + public function __toString(): string |
|
36 | + { |
|
37 | + return 'static'; |
|
38 | + } |
|
39 | 39 | } |
@@ -30,40 +30,40 @@ |
||
30 | 30 | */ |
31 | 31 | final class Object_ implements Type |
32 | 32 | { |
33 | - /** @var Fqsen|null */ |
|
34 | - private $fqsen; |
|
33 | + /** @var Fqsen|null */ |
|
34 | + private $fqsen; |
|
35 | 35 | |
36 | - /** |
|
37 | - * Initializes this object with an optional FQSEN, if not provided this object is considered 'untyped'. |
|
38 | - * |
|
39 | - * @throws InvalidArgumentException When provided $fqsen is not a valid type. |
|
40 | - */ |
|
41 | - public function __construct(?Fqsen $fqsen = null) |
|
42 | - { |
|
43 | - if (strpos((string) $fqsen, '::') !== false || strpos((string) $fqsen, '()') !== false) { |
|
44 | - throw new InvalidArgumentException( |
|
45 | - 'Object types can only refer to a class, interface or trait but a method, function, constant or ' |
|
46 | - . 'property was received: ' . (string) $fqsen |
|
47 | - ); |
|
48 | - } |
|
36 | + /** |
|
37 | + * Initializes this object with an optional FQSEN, if not provided this object is considered 'untyped'. |
|
38 | + * |
|
39 | + * @throws InvalidArgumentException When provided $fqsen is not a valid type. |
|
40 | + */ |
|
41 | + public function __construct(?Fqsen $fqsen = null) |
|
42 | + { |
|
43 | + if (strpos((string) $fqsen, '::') !== false || strpos((string) $fqsen, '()') !== false) { |
|
44 | + throw new InvalidArgumentException( |
|
45 | + 'Object types can only refer to a class, interface or trait but a method, function, constant or ' |
|
46 | + . 'property was received: ' . (string) $fqsen |
|
47 | + ); |
|
48 | + } |
|
49 | 49 | |
50 | - $this->fqsen = $fqsen; |
|
51 | - } |
|
50 | + $this->fqsen = $fqsen; |
|
51 | + } |
|
52 | 52 | |
53 | - /** |
|
54 | - * Returns the FQSEN associated with this object. |
|
55 | - */ |
|
56 | - public function getFqsen(): ?Fqsen |
|
57 | - { |
|
58 | - return $this->fqsen; |
|
59 | - } |
|
53 | + /** |
|
54 | + * Returns the FQSEN associated with this object. |
|
55 | + */ |
|
56 | + public function getFqsen(): ?Fqsen |
|
57 | + { |
|
58 | + return $this->fqsen; |
|
59 | + } |
|
60 | 60 | |
61 | - public function __toString(): string |
|
62 | - { |
|
63 | - if ($this->fqsen) { |
|
64 | - return (string) $this->fqsen; |
|
65 | - } |
|
61 | + public function __toString(): string |
|
62 | + { |
|
63 | + if ($this->fqsen) { |
|
64 | + return (string) $this->fqsen; |
|
65 | + } |
|
66 | 66 | |
67 | - return 'object'; |
|
68 | - } |
|
67 | + return 'object'; |
|
68 | + } |
|
69 | 69 | } |
@@ -43,7 +43,7 @@ |
||
43 | 43 | if (strpos((string) $fqsen, '::') !== false || strpos((string) $fqsen, '()') !== false) { |
44 | 44 | throw new InvalidArgumentException( |
45 | 45 | 'Object types can only refer to a class, interface or trait but a method, function, constant or ' |
46 | - . 'property was received: ' . (string) $fqsen |
|
46 | + . 'property was received: '.(string) $fqsen |
|
47 | 47 | ); |
48 | 48 | } |
49 | 49 |
@@ -48,11 +48,11 @@ discard block |
||
48 | 48 | use const T_USE; |
49 | 49 | |
50 | 50 | if (!defined('T_NAME_QUALIFIED')) { |
51 | - define('T_NAME_QUALIFIED', 'T_NAME_QUALIFIED'); |
|
51 | + define('T_NAME_QUALIFIED', 'T_NAME_QUALIFIED'); |
|
52 | 52 | } |
53 | 53 | |
54 | 54 | if (!defined('T_NAME_FULLY_QUALIFIED')) { |
55 | - define('T_NAME_FULLY_QUALIFIED', 'T_NAME_FULLY_QUALIFIED'); |
|
55 | + define('T_NAME_FULLY_QUALIFIED', 'T_NAME_FULLY_QUALIFIED'); |
|
56 | 56 | } |
57 | 57 | |
58 | 58 | /** |
@@ -66,355 +66,355 @@ discard block |
||
66 | 66 | */ |
67 | 67 | final class ContextFactory |
68 | 68 | { |
69 | - /** The literal used at the end of a use statement. */ |
|
70 | - private const T_LITERAL_END_OF_USE = ';'; |
|
71 | - |
|
72 | - /** The literal used between sets of use statements */ |
|
73 | - private const T_LITERAL_USE_SEPARATOR = ','; |
|
74 | - |
|
75 | - /** |
|
76 | - * Build a Context given a Class Reflection. |
|
77 | - * |
|
78 | - * @see Context for more information on Contexts. |
|
79 | - */ |
|
80 | - public function createFromReflector(Reflector $reflector): Context |
|
81 | - { |
|
82 | - if ($reflector instanceof ReflectionClass) { |
|
83 | - //phpcs:ignore SlevomatCodingStandard.Commenting.InlineDocCommentDeclaration.MissingVariable |
|
84 | - /** @var ReflectionClass<object> $reflector */ |
|
85 | - |
|
86 | - return $this->createFromReflectionClass($reflector); |
|
87 | - } |
|
88 | - |
|
89 | - if ($reflector instanceof ReflectionParameter) { |
|
90 | - return $this->createFromReflectionParameter($reflector); |
|
91 | - } |
|
92 | - |
|
93 | - if ($reflector instanceof ReflectionMethod) { |
|
94 | - return $this->createFromReflectionMethod($reflector); |
|
95 | - } |
|
96 | - |
|
97 | - if ($reflector instanceof ReflectionProperty) { |
|
98 | - return $this->createFromReflectionProperty($reflector); |
|
99 | - } |
|
100 | - |
|
101 | - if ($reflector instanceof ReflectionClassConstant) { |
|
102 | - return $this->createFromReflectionClassConstant($reflector); |
|
103 | - } |
|
104 | - |
|
105 | - throw new UnexpectedValueException('Unhandled \Reflector instance given: ' . get_class($reflector)); |
|
106 | - } |
|
107 | - |
|
108 | - private function createFromReflectionParameter(ReflectionParameter $parameter): Context |
|
109 | - { |
|
110 | - $class = $parameter->getDeclaringClass(); |
|
111 | - if (!$class) { |
|
112 | - throw new InvalidArgumentException('Unable to get class of ' . $parameter->getName()); |
|
113 | - } |
|
114 | - |
|
115 | - return $this->createFromReflectionClass($class); |
|
116 | - } |
|
117 | - |
|
118 | - private function createFromReflectionMethod(ReflectionMethod $method): Context |
|
119 | - { |
|
120 | - $class = $method->getDeclaringClass(); |
|
121 | - |
|
122 | - return $this->createFromReflectionClass($class); |
|
123 | - } |
|
124 | - |
|
125 | - private function createFromReflectionProperty(ReflectionProperty $property): Context |
|
126 | - { |
|
127 | - $class = $property->getDeclaringClass(); |
|
128 | - |
|
129 | - return $this->createFromReflectionClass($class); |
|
130 | - } |
|
131 | - |
|
132 | - private function createFromReflectionClassConstant(ReflectionClassConstant $constant): Context |
|
133 | - { |
|
134 | - //phpcs:ignore SlevomatCodingStandard.Commenting.InlineDocCommentDeclaration.MissingVariable |
|
135 | - /** @phpstan-var ReflectionClass<object> $class */ |
|
136 | - $class = $constant->getDeclaringClass(); |
|
137 | - |
|
138 | - return $this->createFromReflectionClass($class); |
|
139 | - } |
|
140 | - |
|
141 | - /** |
|
142 | - * @phpstan-param ReflectionClass<object> $class |
|
143 | - */ |
|
144 | - private function createFromReflectionClass(ReflectionClass $class): Context |
|
145 | - { |
|
146 | - $fileName = $class->getFileName(); |
|
147 | - $namespace = $class->getNamespaceName(); |
|
148 | - |
|
149 | - if (is_string($fileName) && file_exists($fileName)) { |
|
150 | - $contents = file_get_contents($fileName); |
|
151 | - if ($contents === false) { |
|
152 | - throw new RuntimeException('Unable to read file "' . $fileName . '"'); |
|
153 | - } |
|
154 | - |
|
155 | - return $this->createForNamespace($namespace, $contents); |
|
156 | - } |
|
157 | - |
|
158 | - return new Context($namespace, []); |
|
159 | - } |
|
160 | - |
|
161 | - /** |
|
162 | - * Build a Context for a namespace in the provided file contents. |
|
163 | - * |
|
164 | - * @see Context for more information on Contexts. |
|
165 | - * |
|
166 | - * @param string $namespace It does not matter if a `\` precedes the namespace name, |
|
167 | - * this method first normalizes. |
|
168 | - * @param string $fileContents The file's contents to retrieve the aliases from with the given namespace. |
|
169 | - */ |
|
170 | - public function createForNamespace(string $namespace, string $fileContents): Context |
|
171 | - { |
|
172 | - $namespace = trim($namespace, '\\'); |
|
173 | - $useStatements = []; |
|
174 | - $currentNamespace = ''; |
|
175 | - $tokens = new ArrayIterator(token_get_all($fileContents)); |
|
176 | - |
|
177 | - while ($tokens->valid()) { |
|
178 | - $currentToken = $tokens->current(); |
|
179 | - switch ($currentToken[0]) { |
|
180 | - case T_NAMESPACE: |
|
181 | - $currentNamespace = $this->parseNamespace($tokens); |
|
182 | - break; |
|
183 | - case T_CLASS: |
|
184 | - // Fast-forward the iterator through the class so that any |
|
185 | - // T_USE tokens found within are skipped - these are not |
|
186 | - // valid namespace use statements so should be ignored. |
|
187 | - $braceLevel = 0; |
|
188 | - $firstBraceFound = false; |
|
189 | - while ($tokens->valid() && ($braceLevel > 0 || !$firstBraceFound)) { |
|
190 | - $currentToken = $tokens->current(); |
|
191 | - if ( |
|
192 | - $currentToken === '{' |
|
193 | - || in_array($currentToken[0], [T_CURLY_OPEN, T_DOLLAR_OPEN_CURLY_BRACES], true) |
|
194 | - ) { |
|
195 | - if (!$firstBraceFound) { |
|
196 | - $firstBraceFound = true; |
|
197 | - } |
|
198 | - |
|
199 | - ++$braceLevel; |
|
200 | - } |
|
201 | - |
|
202 | - if ($currentToken === '}') { |
|
203 | - --$braceLevel; |
|
204 | - } |
|
205 | - |
|
206 | - $tokens->next(); |
|
207 | - } |
|
208 | - |
|
209 | - break; |
|
210 | - case T_USE: |
|
211 | - if ($currentNamespace === $namespace) { |
|
212 | - $useStatements += $this->parseUseStatement($tokens); |
|
213 | - } |
|
214 | - |
|
215 | - break; |
|
216 | - } |
|
217 | - |
|
218 | - $tokens->next(); |
|
219 | - } |
|
220 | - |
|
221 | - return new Context($namespace, $useStatements); |
|
222 | - } |
|
223 | - |
|
224 | - /** |
|
225 | - * Deduce the name from tokens when we are at the T_NAMESPACE token. |
|
226 | - * |
|
227 | - * @param ArrayIterator<int, string|array{0:int,1:string,2:int}> $tokens |
|
228 | - */ |
|
229 | - private function parseNamespace(ArrayIterator $tokens): string |
|
230 | - { |
|
231 | - // skip to the first string or namespace separator |
|
232 | - $this->skipToNextStringOrNamespaceSeparator($tokens); |
|
233 | - |
|
234 | - $name = ''; |
|
235 | - $acceptedTokens = [T_STRING, T_NS_SEPARATOR, T_NAME_QUALIFIED]; |
|
236 | - while ($tokens->valid() && in_array($tokens->current()[0], $acceptedTokens, true)) { |
|
237 | - $name .= $tokens->current()[1]; |
|
238 | - $tokens->next(); |
|
239 | - } |
|
240 | - |
|
241 | - return $name; |
|
242 | - } |
|
243 | - |
|
244 | - /** |
|
245 | - * Deduce the names of all imports when we are at the T_USE token. |
|
246 | - * |
|
247 | - * @param ArrayIterator<int, string|array{0:int,1:string,2:int}> $tokens |
|
248 | - * |
|
249 | - * @return string[] |
|
250 | - * @psalm-return array<string, string> |
|
251 | - */ |
|
252 | - private function parseUseStatement(ArrayIterator $tokens): array |
|
253 | - { |
|
254 | - $uses = []; |
|
255 | - |
|
256 | - while ($tokens->valid()) { |
|
257 | - $this->skipToNextStringOrNamespaceSeparator($tokens); |
|
258 | - |
|
259 | - $uses += $this->extractUseStatements($tokens); |
|
260 | - $currentToken = $tokens->current(); |
|
261 | - if ($currentToken[0] === self::T_LITERAL_END_OF_USE) { |
|
262 | - return $uses; |
|
263 | - } |
|
264 | - } |
|
265 | - |
|
266 | - return $uses; |
|
267 | - } |
|
268 | - |
|
269 | - /** |
|
270 | - * Fast-forwards the iterator as longs as we don't encounter a T_STRING or T_NS_SEPARATOR token. |
|
271 | - * |
|
272 | - * @param ArrayIterator<int, string|array{0:int,1:string,2:int}> $tokens |
|
273 | - */ |
|
274 | - private function skipToNextStringOrNamespaceSeparator(ArrayIterator $tokens): void |
|
275 | - { |
|
276 | - while ($tokens->valid()) { |
|
277 | - $currentToken = $tokens->current(); |
|
278 | - if (in_array($currentToken[0], [T_STRING, T_NS_SEPARATOR], true)) { |
|
279 | - break; |
|
280 | - } |
|
281 | - |
|
282 | - if ($currentToken[0] === T_NAME_QUALIFIED) { |
|
283 | - break; |
|
284 | - } |
|
285 | - |
|
286 | - if (defined('T_NAME_FULLY_QUALIFIED') && $currentToken[0] === T_NAME_FULLY_QUALIFIED) { |
|
287 | - break; |
|
288 | - } |
|
289 | - |
|
290 | - $tokens->next(); |
|
291 | - } |
|
292 | - } |
|
293 | - |
|
294 | - /** |
|
295 | - * Deduce the namespace name and alias of an import when we are at the T_USE token or have not reached the end of |
|
296 | - * a USE statement yet. This will return a key/value array of the alias => namespace. |
|
297 | - * |
|
298 | - * @param ArrayIterator<int, string|array{0:int,1:string,2:int}> $tokens |
|
299 | - * |
|
300 | - * @return string[] |
|
301 | - * @psalm-return array<string, string> |
|
302 | - * |
|
303 | - * @psalm-suppress TypeDoesNotContainType |
|
304 | - */ |
|
305 | - private function extractUseStatements(ArrayIterator $tokens): array |
|
306 | - { |
|
307 | - $extractedUseStatements = []; |
|
308 | - $groupedNs = ''; |
|
309 | - $currentNs = ''; |
|
310 | - $currentAlias = ''; |
|
311 | - $state = 'start'; |
|
312 | - |
|
313 | - while ($tokens->valid()) { |
|
314 | - $currentToken = $tokens->current(); |
|
315 | - $tokenId = is_string($currentToken) ? $currentToken : $currentToken[0]; |
|
316 | - $tokenValue = is_string($currentToken) ? null : $currentToken[1]; |
|
317 | - switch ($state) { |
|
318 | - case 'start': |
|
319 | - switch ($tokenId) { |
|
320 | - case T_STRING: |
|
321 | - case T_NS_SEPARATOR: |
|
322 | - $currentNs .= (string) $tokenValue; |
|
323 | - $currentAlias = $tokenValue; |
|
324 | - break; |
|
325 | - case T_NAME_QUALIFIED: |
|
326 | - case T_NAME_FULLY_QUALIFIED: |
|
327 | - $currentNs .= (string) $tokenValue; |
|
328 | - $currentAlias = substr( |
|
329 | - (string) $tokenValue, |
|
330 | - (int) (strrpos((string) $tokenValue, '\\')) + 1 |
|
331 | - ); |
|
332 | - break; |
|
333 | - case T_CURLY_OPEN: |
|
334 | - case '{': |
|
335 | - $state = 'grouped'; |
|
336 | - $groupedNs = $currentNs; |
|
337 | - break; |
|
338 | - case T_AS: |
|
339 | - $state = 'start-alias'; |
|
340 | - break; |
|
341 | - case self::T_LITERAL_USE_SEPARATOR: |
|
342 | - case self::T_LITERAL_END_OF_USE: |
|
343 | - $state = 'end'; |
|
344 | - break; |
|
345 | - default: |
|
346 | - break; |
|
347 | - } |
|
348 | - |
|
349 | - break; |
|
350 | - case 'start-alias': |
|
351 | - switch ($tokenId) { |
|
352 | - case T_STRING: |
|
353 | - $currentAlias = $tokenValue; |
|
354 | - break; |
|
355 | - case self::T_LITERAL_USE_SEPARATOR: |
|
356 | - case self::T_LITERAL_END_OF_USE: |
|
357 | - $state = 'end'; |
|
358 | - break; |
|
359 | - default: |
|
360 | - break; |
|
361 | - } |
|
362 | - |
|
363 | - break; |
|
364 | - case 'grouped': |
|
365 | - switch ($tokenId) { |
|
366 | - case T_STRING: |
|
367 | - case T_NS_SEPARATOR: |
|
368 | - $currentNs .= (string) $tokenValue; |
|
369 | - $currentAlias = $tokenValue; |
|
370 | - break; |
|
371 | - case T_AS: |
|
372 | - $state = 'grouped-alias'; |
|
373 | - break; |
|
374 | - case self::T_LITERAL_USE_SEPARATOR: |
|
375 | - $state = 'grouped'; |
|
376 | - $extractedUseStatements[(string) $currentAlias] = $currentNs; |
|
377 | - $currentNs = $groupedNs; |
|
378 | - $currentAlias = ''; |
|
379 | - break; |
|
380 | - case self::T_LITERAL_END_OF_USE: |
|
381 | - $state = 'end'; |
|
382 | - break; |
|
383 | - default: |
|
384 | - break; |
|
385 | - } |
|
386 | - |
|
387 | - break; |
|
388 | - case 'grouped-alias': |
|
389 | - switch ($tokenId) { |
|
390 | - case T_STRING: |
|
391 | - $currentAlias = $tokenValue; |
|
392 | - break; |
|
393 | - case self::T_LITERAL_USE_SEPARATOR: |
|
394 | - $state = 'grouped'; |
|
395 | - $extractedUseStatements[(string) $currentAlias] = $currentNs; |
|
396 | - $currentNs = $groupedNs; |
|
397 | - $currentAlias = ''; |
|
398 | - break; |
|
399 | - case self::T_LITERAL_END_OF_USE: |
|
400 | - $state = 'end'; |
|
401 | - break; |
|
402 | - default: |
|
403 | - break; |
|
404 | - } |
|
405 | - } |
|
406 | - |
|
407 | - if ($state === 'end') { |
|
408 | - break; |
|
409 | - } |
|
410 | - |
|
411 | - $tokens->next(); |
|
412 | - } |
|
413 | - |
|
414 | - if ($groupedNs !== $currentNs) { |
|
415 | - $extractedUseStatements[(string) $currentAlias] = $currentNs; |
|
416 | - } |
|
417 | - |
|
418 | - return $extractedUseStatements; |
|
419 | - } |
|
69 | + /** The literal used at the end of a use statement. */ |
|
70 | + private const T_LITERAL_END_OF_USE = ';'; |
|
71 | + |
|
72 | + /** The literal used between sets of use statements */ |
|
73 | + private const T_LITERAL_USE_SEPARATOR = ','; |
|
74 | + |
|
75 | + /** |
|
76 | + * Build a Context given a Class Reflection. |
|
77 | + * |
|
78 | + * @see Context for more information on Contexts. |
|
79 | + */ |
|
80 | + public function createFromReflector(Reflector $reflector): Context |
|
81 | + { |
|
82 | + if ($reflector instanceof ReflectionClass) { |
|
83 | + //phpcs:ignore SlevomatCodingStandard.Commenting.InlineDocCommentDeclaration.MissingVariable |
|
84 | + /** @var ReflectionClass<object> $reflector */ |
|
85 | + |
|
86 | + return $this->createFromReflectionClass($reflector); |
|
87 | + } |
|
88 | + |
|
89 | + if ($reflector instanceof ReflectionParameter) { |
|
90 | + return $this->createFromReflectionParameter($reflector); |
|
91 | + } |
|
92 | + |
|
93 | + if ($reflector instanceof ReflectionMethod) { |
|
94 | + return $this->createFromReflectionMethod($reflector); |
|
95 | + } |
|
96 | + |
|
97 | + if ($reflector instanceof ReflectionProperty) { |
|
98 | + return $this->createFromReflectionProperty($reflector); |
|
99 | + } |
|
100 | + |
|
101 | + if ($reflector instanceof ReflectionClassConstant) { |
|
102 | + return $this->createFromReflectionClassConstant($reflector); |
|
103 | + } |
|
104 | + |
|
105 | + throw new UnexpectedValueException('Unhandled \Reflector instance given: ' . get_class($reflector)); |
|
106 | + } |
|
107 | + |
|
108 | + private function createFromReflectionParameter(ReflectionParameter $parameter): Context |
|
109 | + { |
|
110 | + $class = $parameter->getDeclaringClass(); |
|
111 | + if (!$class) { |
|
112 | + throw new InvalidArgumentException('Unable to get class of ' . $parameter->getName()); |
|
113 | + } |
|
114 | + |
|
115 | + return $this->createFromReflectionClass($class); |
|
116 | + } |
|
117 | + |
|
118 | + private function createFromReflectionMethod(ReflectionMethod $method): Context |
|
119 | + { |
|
120 | + $class = $method->getDeclaringClass(); |
|
121 | + |
|
122 | + return $this->createFromReflectionClass($class); |
|
123 | + } |
|
124 | + |
|
125 | + private function createFromReflectionProperty(ReflectionProperty $property): Context |
|
126 | + { |
|
127 | + $class = $property->getDeclaringClass(); |
|
128 | + |
|
129 | + return $this->createFromReflectionClass($class); |
|
130 | + } |
|
131 | + |
|
132 | + private function createFromReflectionClassConstant(ReflectionClassConstant $constant): Context |
|
133 | + { |
|
134 | + //phpcs:ignore SlevomatCodingStandard.Commenting.InlineDocCommentDeclaration.MissingVariable |
|
135 | + /** @phpstan-var ReflectionClass<object> $class */ |
|
136 | + $class = $constant->getDeclaringClass(); |
|
137 | + |
|
138 | + return $this->createFromReflectionClass($class); |
|
139 | + } |
|
140 | + |
|
141 | + /** |
|
142 | + * @phpstan-param ReflectionClass<object> $class |
|
143 | + */ |
|
144 | + private function createFromReflectionClass(ReflectionClass $class): Context |
|
145 | + { |
|
146 | + $fileName = $class->getFileName(); |
|
147 | + $namespace = $class->getNamespaceName(); |
|
148 | + |
|
149 | + if (is_string($fileName) && file_exists($fileName)) { |
|
150 | + $contents = file_get_contents($fileName); |
|
151 | + if ($contents === false) { |
|
152 | + throw new RuntimeException('Unable to read file "' . $fileName . '"'); |
|
153 | + } |
|
154 | + |
|
155 | + return $this->createForNamespace($namespace, $contents); |
|
156 | + } |
|
157 | + |
|
158 | + return new Context($namespace, []); |
|
159 | + } |
|
160 | + |
|
161 | + /** |
|
162 | + * Build a Context for a namespace in the provided file contents. |
|
163 | + * |
|
164 | + * @see Context for more information on Contexts. |
|
165 | + * |
|
166 | + * @param string $namespace It does not matter if a `\` precedes the namespace name, |
|
167 | + * this method first normalizes. |
|
168 | + * @param string $fileContents The file's contents to retrieve the aliases from with the given namespace. |
|
169 | + */ |
|
170 | + public function createForNamespace(string $namespace, string $fileContents): Context |
|
171 | + { |
|
172 | + $namespace = trim($namespace, '\\'); |
|
173 | + $useStatements = []; |
|
174 | + $currentNamespace = ''; |
|
175 | + $tokens = new ArrayIterator(token_get_all($fileContents)); |
|
176 | + |
|
177 | + while ($tokens->valid()) { |
|
178 | + $currentToken = $tokens->current(); |
|
179 | + switch ($currentToken[0]) { |
|
180 | + case T_NAMESPACE: |
|
181 | + $currentNamespace = $this->parseNamespace($tokens); |
|
182 | + break; |
|
183 | + case T_CLASS: |
|
184 | + // Fast-forward the iterator through the class so that any |
|
185 | + // T_USE tokens found within are skipped - these are not |
|
186 | + // valid namespace use statements so should be ignored. |
|
187 | + $braceLevel = 0; |
|
188 | + $firstBraceFound = false; |
|
189 | + while ($tokens->valid() && ($braceLevel > 0 || !$firstBraceFound)) { |
|
190 | + $currentToken = $tokens->current(); |
|
191 | + if ( |
|
192 | + $currentToken === '{' |
|
193 | + || in_array($currentToken[0], [T_CURLY_OPEN, T_DOLLAR_OPEN_CURLY_BRACES], true) |
|
194 | + ) { |
|
195 | + if (!$firstBraceFound) { |
|
196 | + $firstBraceFound = true; |
|
197 | + } |
|
198 | + |
|
199 | + ++$braceLevel; |
|
200 | + } |
|
201 | + |
|
202 | + if ($currentToken === '}') { |
|
203 | + --$braceLevel; |
|
204 | + } |
|
205 | + |
|
206 | + $tokens->next(); |
|
207 | + } |
|
208 | + |
|
209 | + break; |
|
210 | + case T_USE: |
|
211 | + if ($currentNamespace === $namespace) { |
|
212 | + $useStatements += $this->parseUseStatement($tokens); |
|
213 | + } |
|
214 | + |
|
215 | + break; |
|
216 | + } |
|
217 | + |
|
218 | + $tokens->next(); |
|
219 | + } |
|
220 | + |
|
221 | + return new Context($namespace, $useStatements); |
|
222 | + } |
|
223 | + |
|
224 | + /** |
|
225 | + * Deduce the name from tokens when we are at the T_NAMESPACE token. |
|
226 | + * |
|
227 | + * @param ArrayIterator<int, string|array{0:int,1:string,2:int}> $tokens |
|
228 | + */ |
|
229 | + private function parseNamespace(ArrayIterator $tokens): string |
|
230 | + { |
|
231 | + // skip to the first string or namespace separator |
|
232 | + $this->skipToNextStringOrNamespaceSeparator($tokens); |
|
233 | + |
|
234 | + $name = ''; |
|
235 | + $acceptedTokens = [T_STRING, T_NS_SEPARATOR, T_NAME_QUALIFIED]; |
|
236 | + while ($tokens->valid() && in_array($tokens->current()[0], $acceptedTokens, true)) { |
|
237 | + $name .= $tokens->current()[1]; |
|
238 | + $tokens->next(); |
|
239 | + } |
|
240 | + |
|
241 | + return $name; |
|
242 | + } |
|
243 | + |
|
244 | + /** |
|
245 | + * Deduce the names of all imports when we are at the T_USE token. |
|
246 | + * |
|
247 | + * @param ArrayIterator<int, string|array{0:int,1:string,2:int}> $tokens |
|
248 | + * |
|
249 | + * @return string[] |
|
250 | + * @psalm-return array<string, string> |
|
251 | + */ |
|
252 | + private function parseUseStatement(ArrayIterator $tokens): array |
|
253 | + { |
|
254 | + $uses = []; |
|
255 | + |
|
256 | + while ($tokens->valid()) { |
|
257 | + $this->skipToNextStringOrNamespaceSeparator($tokens); |
|
258 | + |
|
259 | + $uses += $this->extractUseStatements($tokens); |
|
260 | + $currentToken = $tokens->current(); |
|
261 | + if ($currentToken[0] === self::T_LITERAL_END_OF_USE) { |
|
262 | + return $uses; |
|
263 | + } |
|
264 | + } |
|
265 | + |
|
266 | + return $uses; |
|
267 | + } |
|
268 | + |
|
269 | + /** |
|
270 | + * Fast-forwards the iterator as longs as we don't encounter a T_STRING or T_NS_SEPARATOR token. |
|
271 | + * |
|
272 | + * @param ArrayIterator<int, string|array{0:int,1:string,2:int}> $tokens |
|
273 | + */ |
|
274 | + private function skipToNextStringOrNamespaceSeparator(ArrayIterator $tokens): void |
|
275 | + { |
|
276 | + while ($tokens->valid()) { |
|
277 | + $currentToken = $tokens->current(); |
|
278 | + if (in_array($currentToken[0], [T_STRING, T_NS_SEPARATOR], true)) { |
|
279 | + break; |
|
280 | + } |
|
281 | + |
|
282 | + if ($currentToken[0] === T_NAME_QUALIFIED) { |
|
283 | + break; |
|
284 | + } |
|
285 | + |
|
286 | + if (defined('T_NAME_FULLY_QUALIFIED') && $currentToken[0] === T_NAME_FULLY_QUALIFIED) { |
|
287 | + break; |
|
288 | + } |
|
289 | + |
|
290 | + $tokens->next(); |
|
291 | + } |
|
292 | + } |
|
293 | + |
|
294 | + /** |
|
295 | + * Deduce the namespace name and alias of an import when we are at the T_USE token or have not reached the end of |
|
296 | + * a USE statement yet. This will return a key/value array of the alias => namespace. |
|
297 | + * |
|
298 | + * @param ArrayIterator<int, string|array{0:int,1:string,2:int}> $tokens |
|
299 | + * |
|
300 | + * @return string[] |
|
301 | + * @psalm-return array<string, string> |
|
302 | + * |
|
303 | + * @psalm-suppress TypeDoesNotContainType |
|
304 | + */ |
|
305 | + private function extractUseStatements(ArrayIterator $tokens): array |
|
306 | + { |
|
307 | + $extractedUseStatements = []; |
|
308 | + $groupedNs = ''; |
|
309 | + $currentNs = ''; |
|
310 | + $currentAlias = ''; |
|
311 | + $state = 'start'; |
|
312 | + |
|
313 | + while ($tokens->valid()) { |
|
314 | + $currentToken = $tokens->current(); |
|
315 | + $tokenId = is_string($currentToken) ? $currentToken : $currentToken[0]; |
|
316 | + $tokenValue = is_string($currentToken) ? null : $currentToken[1]; |
|
317 | + switch ($state) { |
|
318 | + case 'start': |
|
319 | + switch ($tokenId) { |
|
320 | + case T_STRING: |
|
321 | + case T_NS_SEPARATOR: |
|
322 | + $currentNs .= (string) $tokenValue; |
|
323 | + $currentAlias = $tokenValue; |
|
324 | + break; |
|
325 | + case T_NAME_QUALIFIED: |
|
326 | + case T_NAME_FULLY_QUALIFIED: |
|
327 | + $currentNs .= (string) $tokenValue; |
|
328 | + $currentAlias = substr( |
|
329 | + (string) $tokenValue, |
|
330 | + (int) (strrpos((string) $tokenValue, '\\')) + 1 |
|
331 | + ); |
|
332 | + break; |
|
333 | + case T_CURLY_OPEN: |
|
334 | + case '{': |
|
335 | + $state = 'grouped'; |
|
336 | + $groupedNs = $currentNs; |
|
337 | + break; |
|
338 | + case T_AS: |
|
339 | + $state = 'start-alias'; |
|
340 | + break; |
|
341 | + case self::T_LITERAL_USE_SEPARATOR: |
|
342 | + case self::T_LITERAL_END_OF_USE: |
|
343 | + $state = 'end'; |
|
344 | + break; |
|
345 | + default: |
|
346 | + break; |
|
347 | + } |
|
348 | + |
|
349 | + break; |
|
350 | + case 'start-alias': |
|
351 | + switch ($tokenId) { |
|
352 | + case T_STRING: |
|
353 | + $currentAlias = $tokenValue; |
|
354 | + break; |
|
355 | + case self::T_LITERAL_USE_SEPARATOR: |
|
356 | + case self::T_LITERAL_END_OF_USE: |
|
357 | + $state = 'end'; |
|
358 | + break; |
|
359 | + default: |
|
360 | + break; |
|
361 | + } |
|
362 | + |
|
363 | + break; |
|
364 | + case 'grouped': |
|
365 | + switch ($tokenId) { |
|
366 | + case T_STRING: |
|
367 | + case T_NS_SEPARATOR: |
|
368 | + $currentNs .= (string) $tokenValue; |
|
369 | + $currentAlias = $tokenValue; |
|
370 | + break; |
|
371 | + case T_AS: |
|
372 | + $state = 'grouped-alias'; |
|
373 | + break; |
|
374 | + case self::T_LITERAL_USE_SEPARATOR: |
|
375 | + $state = 'grouped'; |
|
376 | + $extractedUseStatements[(string) $currentAlias] = $currentNs; |
|
377 | + $currentNs = $groupedNs; |
|
378 | + $currentAlias = ''; |
|
379 | + break; |
|
380 | + case self::T_LITERAL_END_OF_USE: |
|
381 | + $state = 'end'; |
|
382 | + break; |
|
383 | + default: |
|
384 | + break; |
|
385 | + } |
|
386 | + |
|
387 | + break; |
|
388 | + case 'grouped-alias': |
|
389 | + switch ($tokenId) { |
|
390 | + case T_STRING: |
|
391 | + $currentAlias = $tokenValue; |
|
392 | + break; |
|
393 | + case self::T_LITERAL_USE_SEPARATOR: |
|
394 | + $state = 'grouped'; |
|
395 | + $extractedUseStatements[(string) $currentAlias] = $currentNs; |
|
396 | + $currentNs = $groupedNs; |
|
397 | + $currentAlias = ''; |
|
398 | + break; |
|
399 | + case self::T_LITERAL_END_OF_USE: |
|
400 | + $state = 'end'; |
|
401 | + break; |
|
402 | + default: |
|
403 | + break; |
|
404 | + } |
|
405 | + } |
|
406 | + |
|
407 | + if ($state === 'end') { |
|
408 | + break; |
|
409 | + } |
|
410 | + |
|
411 | + $tokens->next(); |
|
412 | + } |
|
413 | + |
|
414 | + if ($groupedNs !== $currentNs) { |
|
415 | + $extractedUseStatements[(string) $currentAlias] = $currentNs; |
|
416 | + } |
|
417 | + |
|
418 | + return $extractedUseStatements; |
|
419 | + } |
|
420 | 420 | } |
@@ -47,11 +47,11 @@ discard block |
||
47 | 47 | use const T_STRING; |
48 | 48 | use const T_USE; |
49 | 49 | |
50 | -if (!defined('T_NAME_QUALIFIED')) { |
|
50 | +if ( ! defined('T_NAME_QUALIFIED')) { |
|
51 | 51 | define('T_NAME_QUALIFIED', 'T_NAME_QUALIFIED'); |
52 | 52 | } |
53 | 53 | |
54 | -if (!defined('T_NAME_FULLY_QUALIFIED')) { |
|
54 | +if ( ! defined('T_NAME_FULLY_QUALIFIED')) { |
|
55 | 55 | define('T_NAME_FULLY_QUALIFIED', 'T_NAME_FULLY_QUALIFIED'); |
56 | 56 | } |
57 | 57 | |
@@ -102,14 +102,14 @@ discard block |
||
102 | 102 | return $this->createFromReflectionClassConstant($reflector); |
103 | 103 | } |
104 | 104 | |
105 | - throw new UnexpectedValueException('Unhandled \Reflector instance given: ' . get_class($reflector)); |
|
105 | + throw new UnexpectedValueException('Unhandled \Reflector instance given: '.get_class($reflector)); |
|
106 | 106 | } |
107 | 107 | |
108 | 108 | private function createFromReflectionParameter(ReflectionParameter $parameter): Context |
109 | 109 | { |
110 | 110 | $class = $parameter->getDeclaringClass(); |
111 | - if (!$class) { |
|
112 | - throw new InvalidArgumentException('Unable to get class of ' . $parameter->getName()); |
|
111 | + if ( ! $class) { |
|
112 | + throw new InvalidArgumentException('Unable to get class of '.$parameter->getName()); |
|
113 | 113 | } |
114 | 114 | |
115 | 115 | return $this->createFromReflectionClass($class); |
@@ -149,7 +149,7 @@ discard block |
||
149 | 149 | if (is_string($fileName) && file_exists($fileName)) { |
150 | 150 | $contents = file_get_contents($fileName); |
151 | 151 | if ($contents === false) { |
152 | - throw new RuntimeException('Unable to read file "' . $fileName . '"'); |
|
152 | + throw new RuntimeException('Unable to read file "'.$fileName.'"'); |
|
153 | 153 | } |
154 | 154 | |
155 | 155 | return $this->createForNamespace($namespace, $contents); |
@@ -186,13 +186,13 @@ discard block |
||
186 | 186 | // valid namespace use statements so should be ignored. |
187 | 187 | $braceLevel = 0; |
188 | 188 | $firstBraceFound = false; |
189 | - while ($tokens->valid() && ($braceLevel > 0 || !$firstBraceFound)) { |
|
189 | + while ($tokens->valid() && ($braceLevel > 0 || ! $firstBraceFound)) { |
|
190 | 190 | $currentToken = $tokens->current(); |
191 | 191 | if ( |
192 | 192 | $currentToken === '{' |
193 | 193 | || in_array($currentToken[0], [T_CURLY_OPEN, T_DOLLAR_OPEN_CURLY_BRACES], true) |
194 | 194 | ) { |
195 | - if (!$firstBraceFound) { |
|
195 | + if ( ! $firstBraceFound) { |
|
196 | 196 | $firstBraceFound = true; |
197 | 197 | } |
198 | 198 | |
@@ -320,7 +320,7 @@ discard block |
||
320 | 320 | case T_STRING: |
321 | 321 | case T_NS_SEPARATOR: |
322 | 322 | $currentNs .= (string) $tokenValue; |
323 | - $currentAlias = $tokenValue; |
|
323 | + $currentAlias = $tokenValue; |
|
324 | 324 | break; |
325 | 325 | case T_NAME_QUALIFIED: |
326 | 326 | case T_NAME_FULLY_QUALIFIED: |
@@ -23,34 +23,34 @@ |
||
23 | 23 | */ |
24 | 24 | final class InterfaceString implements Type |
25 | 25 | { |
26 | - /** @var Fqsen|null */ |
|
27 | - private $fqsen; |
|
28 | - |
|
29 | - /** |
|
30 | - * Initializes this representation of a class string with the given Fqsen. |
|
31 | - */ |
|
32 | - public function __construct(?Fqsen $fqsen = null) |
|
33 | - { |
|
34 | - $this->fqsen = $fqsen; |
|
35 | - } |
|
36 | - |
|
37 | - /** |
|
38 | - * Returns the FQSEN associated with this object. |
|
39 | - */ |
|
40 | - public function getFqsen(): ?Fqsen |
|
41 | - { |
|
42 | - return $this->fqsen; |
|
43 | - } |
|
44 | - |
|
45 | - /** |
|
46 | - * Returns a rendered output of the Type as it would be used in a DocBlock. |
|
47 | - */ |
|
48 | - public function __toString(): string |
|
49 | - { |
|
50 | - if ($this->fqsen === null) { |
|
51 | - return 'interface-string'; |
|
52 | - } |
|
53 | - |
|
54 | - return 'interface-string<' . (string) $this->fqsen . '>'; |
|
55 | - } |
|
26 | + /** @var Fqsen|null */ |
|
27 | + private $fqsen; |
|
28 | + |
|
29 | + /** |
|
30 | + * Initializes this representation of a class string with the given Fqsen. |
|
31 | + */ |
|
32 | + public function __construct(?Fqsen $fqsen = null) |
|
33 | + { |
|
34 | + $this->fqsen = $fqsen; |
|
35 | + } |
|
36 | + |
|
37 | + /** |
|
38 | + * Returns the FQSEN associated with this object. |
|
39 | + */ |
|
40 | + public function getFqsen(): ?Fqsen |
|
41 | + { |
|
42 | + return $this->fqsen; |
|
43 | + } |
|
44 | + |
|
45 | + /** |
|
46 | + * Returns a rendered output of the Type as it would be used in a DocBlock. |
|
47 | + */ |
|
48 | + public function __toString(): string |
|
49 | + { |
|
50 | + if ($this->fqsen === null) { |
|
51 | + return 'interface-string'; |
|
52 | + } |
|
53 | + |
|
54 | + return 'interface-string<' . (string) $this->fqsen . '>'; |
|
55 | + } |
|
56 | 56 | } |
@@ -51,6 +51,6 @@ |
||
51 | 51 | return 'interface-string'; |
52 | 52 | } |
53 | 53 | |
54 | - return 'interface-string<' . (string) $this->fqsen . '>'; |
|
54 | + return 'interface-string<'.(string) $this->fqsen.'>'; |
|
55 | 55 | } |
56 | 56 | } |
@@ -22,11 +22,11 @@ |
||
22 | 22 | */ |
23 | 23 | class Integer implements Type |
24 | 24 | { |
25 | - /** |
|
26 | - * Returns a rendered output of the Type as it would be used in a DocBlock. |
|
27 | - */ |
|
28 | - public function __toString(): string |
|
29 | - { |
|
30 | - return 'int'; |
|
31 | - } |
|
25 | + /** |
|
26 | + * Returns a rendered output of the Type as it would be used in a DocBlock. |
|
27 | + */ |
|
28 | + public function __toString(): string |
|
29 | + { |
|
30 | + return 'int'; |
|
31 | + } |
|
32 | 32 | } |
@@ -22,30 +22,30 @@ |
||
22 | 22 | */ |
23 | 23 | final class Nullable implements Type |
24 | 24 | { |
25 | - /** @var Type The actual type that is wrapped */ |
|
26 | - private $realType; |
|
27 | - |
|
28 | - /** |
|
29 | - * Initialises this nullable type using the real type embedded |
|
30 | - */ |
|
31 | - public function __construct(Type $realType) |
|
32 | - { |
|
33 | - $this->realType = $realType; |
|
34 | - } |
|
35 | - |
|
36 | - /** |
|
37 | - * Provide access to the actual type directly, if needed. |
|
38 | - */ |
|
39 | - public function getActualType(): Type |
|
40 | - { |
|
41 | - return $this->realType; |
|
42 | - } |
|
43 | - |
|
44 | - /** |
|
45 | - * Returns a rendered output of the Type as it would be used in a DocBlock. |
|
46 | - */ |
|
47 | - public function __toString(): string |
|
48 | - { |
|
49 | - return '?' . $this->realType->__toString(); |
|
50 | - } |
|
25 | + /** @var Type The actual type that is wrapped */ |
|
26 | + private $realType; |
|
27 | + |
|
28 | + /** |
|
29 | + * Initialises this nullable type using the real type embedded |
|
30 | + */ |
|
31 | + public function __construct(Type $realType) |
|
32 | + { |
|
33 | + $this->realType = $realType; |
|
34 | + } |
|
35 | + |
|
36 | + /** |
|
37 | + * Provide access to the actual type directly, if needed. |
|
38 | + */ |
|
39 | + public function getActualType(): Type |
|
40 | + { |
|
41 | + return $this->realType; |
|
42 | + } |
|
43 | + |
|
44 | + /** |
|
45 | + * Returns a rendered output of the Type as it would be used in a DocBlock. |
|
46 | + */ |
|
47 | + public function __toString(): string |
|
48 | + { |
|
49 | + return '?' . $this->realType->__toString(); |
|
50 | + } |
|
51 | 51 | } |
@@ -46,6 +46,6 @@ |
||
46 | 46 | */ |
47 | 47 | public function __toString(): string |
48 | 48 | { |
49 | - return '?' . $this->realType->__toString(); |
|
49 | + return '?'.$this->realType->__toString(); |
|
50 | 50 | } |
51 | 51 | } |
@@ -22,11 +22,11 @@ |
||
22 | 22 | */ |
23 | 23 | final class Scalar implements Type |
24 | 24 | { |
25 | - /** |
|
26 | - * Returns a rendered output of the Type as it would be used in a DocBlock. |
|
27 | - */ |
|
28 | - public function __toString(): string |
|
29 | - { |
|
30 | - return 'scalar'; |
|
31 | - } |
|
25 | + /** |
|
26 | + * Returns a rendered output of the Type as it would be used in a DocBlock. |
|
27 | + */ |
|
28 | + public function __toString(): string |
|
29 | + { |
|
30 | + return 'scalar'; |
|
31 | + } |
|
32 | 32 | } |
@@ -24,39 +24,39 @@ |
||
24 | 24 | */ |
25 | 25 | final class ClassString extends String_ implements PseudoType |
26 | 26 | { |
27 | - /** @var Fqsen|null */ |
|
28 | - private $fqsen; |
|
29 | - |
|
30 | - /** |
|
31 | - * Initializes this representation of a class string with the given Fqsen. |
|
32 | - */ |
|
33 | - public function __construct(?Fqsen $fqsen = null) |
|
34 | - { |
|
35 | - $this->fqsen = $fqsen; |
|
36 | - } |
|
37 | - |
|
38 | - public function underlyingType(): Type |
|
39 | - { |
|
40 | - return new String_(); |
|
41 | - } |
|
42 | - |
|
43 | - /** |
|
44 | - * Returns the FQSEN associated with this object. |
|
45 | - */ |
|
46 | - public function getFqsen(): ?Fqsen |
|
47 | - { |
|
48 | - return $this->fqsen; |
|
49 | - } |
|
50 | - |
|
51 | - /** |
|
52 | - * Returns a rendered output of the Type as it would be used in a DocBlock. |
|
53 | - */ |
|
54 | - public function __toString(): string |
|
55 | - { |
|
56 | - if ($this->fqsen === null) { |
|
57 | - return 'class-string'; |
|
58 | - } |
|
59 | - |
|
60 | - return 'class-string<' . (string) $this->fqsen . '>'; |
|
61 | - } |
|
27 | + /** @var Fqsen|null */ |
|
28 | + private $fqsen; |
|
29 | + |
|
30 | + /** |
|
31 | + * Initializes this representation of a class string with the given Fqsen. |
|
32 | + */ |
|
33 | + public function __construct(?Fqsen $fqsen = null) |
|
34 | + { |
|
35 | + $this->fqsen = $fqsen; |
|
36 | + } |
|
37 | + |
|
38 | + public function underlyingType(): Type |
|
39 | + { |
|
40 | + return new String_(); |
|
41 | + } |
|
42 | + |
|
43 | + /** |
|
44 | + * Returns the FQSEN associated with this object. |
|
45 | + */ |
|
46 | + public function getFqsen(): ?Fqsen |
|
47 | + { |
|
48 | + return $this->fqsen; |
|
49 | + } |
|
50 | + |
|
51 | + /** |
|
52 | + * Returns a rendered output of the Type as it would be used in a DocBlock. |
|
53 | + */ |
|
54 | + public function __toString(): string |
|
55 | + { |
|
56 | + if ($this->fqsen === null) { |
|
57 | + return 'class-string'; |
|
58 | + } |
|
59 | + |
|
60 | + return 'class-string<' . (string) $this->fqsen . '>'; |
|
61 | + } |
|
62 | 62 | } |
@@ -57,6 +57,6 @@ |
||
57 | 57 | return 'class-string'; |
58 | 58 | } |
59 | 59 | |
60 | - return 'class-string<' . (string) $this->fqsen . '>'; |
|
60 | + return 'class-string<'.(string) $this->fqsen.'>'; |
|
61 | 61 | } |
62 | 62 | } |