@@ -8,7 +8,8 @@ |
||
8 | 8 | { |
9 | 9 | public function __construct() |
10 | 10 | { |
11 | - $class = new class ('foo', 'bar') { |
|
11 | + $class = new class ('foo', 'bar') |
|
12 | + { |
|
12 | 13 | private function someFunc(): void |
13 | 14 | { |
14 | 15 | } |
@@ -141,7 +141,8 @@ discard block |
||
141 | 141 | */ |
142 | 142 | public function getClasses(): array |
143 | 143 | { |
144 | - if (!isset($this->declarations['T_CLASS'])) { |
|
144 | + if (!isset($this->declarations['T_CLASS'])) |
|
145 | + { |
|
145 | 146 | return []; |
146 | 147 | } |
147 | 148 | |
@@ -153,7 +154,8 @@ discard block |
||
153 | 154 | */ |
154 | 155 | public function getTraits(): array |
155 | 156 | { |
156 | - if (!isset($this->declarations['T_TRAIT'])) { |
|
157 | + if (!isset($this->declarations['T_TRAIT'])) |
|
158 | + { |
|
157 | 159 | return []; |
158 | 160 | } |
159 | 161 | |
@@ -165,7 +167,8 @@ discard block |
||
165 | 167 | */ |
166 | 168 | public function getInterfaces(): array |
167 | 169 | { |
168 | - if (!isset($this->declarations['T_INTERFACE'])) { |
|
170 | + if (!isset($this->declarations['T_INTERFACE'])) |
|
171 | + { |
|
169 | 172 | return []; |
170 | 173 | } |
171 | 174 | |
@@ -196,7 +199,8 @@ discard block |
||
196 | 199 | */ |
197 | 200 | public function getInvocations(): array |
198 | 201 | { |
199 | - if (empty($this->invocations)) { |
|
202 | + if (empty($this->invocations)) |
|
203 | + { |
|
200 | 204 | $this->locateInvocations($this->getTokens()); |
201 | 205 | } |
202 | 206 | |
@@ -224,12 +228,15 @@ discard block |
||
224 | 228 | */ |
225 | 229 | protected function locateDeclarations() |
226 | 230 | { |
227 | - foreach ($this->getTokens() as $tokenID => $token) { |
|
228 | - if (!\in_array($token[self::TOKEN_TYPE], self::$processTokens)) { |
|
231 | + foreach ($this->getTokens() as $tokenID => $token) |
|
232 | + { |
|
233 | + if (!\in_array($token[self::TOKEN_TYPE], self::$processTokens)) |
|
234 | + { |
|
229 | 235 | continue; |
230 | 236 | } |
231 | 237 | |
232 | - switch ($token[self::TOKEN_TYPE]) { |
|
238 | + switch ($token[self::TOKEN_TYPE]) |
|
239 | + { |
|
233 | 240 | case T_NAMESPACE: |
234 | 241 | $this->registerNamespace($tokenID); |
235 | 242 | break; |
@@ -245,17 +252,20 @@ discard block |
||
245 | 252 | case T_CLASS: |
246 | 253 | case T_TRAIT: |
247 | 254 | case T_INTERFACE: |
248 | - if ($this->isClassNameConst($tokenID)) { |
|
255 | + if ($this->isClassNameConst($tokenID)) |
|
256 | + { |
|
249 | 257 | // PHP5.5 ClassName::class constant |
250 | 258 | continue 2; |
251 | 259 | } |
252 | 260 | |
253 | - if ($this->isAnonymousClass($tokenID)) { |
|
261 | + if ($this->isAnonymousClass($tokenID)) |
|
262 | + { |
|
254 | 263 | // PHP7.0 Anonymous classes new class ('foo', 'bar') |
255 | 264 | continue 2; |
256 | 265 | } |
257 | 266 | |
258 | - if ($this->isNamedParameter($tokenID)) { |
|
267 | + if ($this->isNamedParameter($tokenID)) |
|
268 | + { |
|
259 | 269 | // PHP8.0 Named parameters |
260 | 270 | continue 2; |
261 | 271 | } |
@@ -272,7 +282,8 @@ discard block |
||
272 | 282 | } |
273 | 283 | |
274 | 284 | //Dropping empty namespace |
275 | - if (isset($this->namespaces[''])) { |
|
285 | + if (isset($this->namespaces[''])) |
|
286 | + { |
|
276 | 287 | $this->namespaces['\\'] = $this->namespaces['']; |
277 | 288 | unset($this->namespaces['']); |
278 | 289 | } |
@@ -286,9 +297,11 @@ discard block |
||
286 | 297 | $namespace = ''; |
287 | 298 | $localID = $tokenID + 1; |
288 | 299 | |
289 | - do { |
|
300 | + do |
|
301 | + { |
|
290 | 302 | $token = $this->tokens[$localID++]; |
291 | - if ($token[self::TOKEN_CODE] === '{') { |
|
303 | + if ($token[self::TOKEN_CODE] === '{') |
|
304 | + { |
|
292 | 305 | break; |
293 | 306 | } |
294 | 307 | |
@@ -303,13 +316,17 @@ discard block |
||
303 | 316 | $namespace = \trim($namespace); |
304 | 317 | |
305 | 318 | $uses = []; |
306 | - if (isset($this->namespaces[$namespace])) { |
|
319 | + if (isset($this->namespaces[$namespace])) |
|
320 | + { |
|
307 | 321 | $uses = $this->namespaces[$namespace]; |
308 | 322 | } |
309 | 323 | |
310 | - if ($this->tokens[$localID][self::TOKEN_CODE] === ';') { |
|
324 | + if ($this->tokens[$localID][self::TOKEN_CODE] === ';') |
|
325 | + { |
|
311 | 326 | $endingID = \count($this->tokens) - 1; |
312 | - } else { |
|
327 | + } |
|
328 | + else |
|
329 | + { |
|
313 | 330 | $endingID = $this->endingToken($tokenID); |
314 | 331 | } |
315 | 332 | |
@@ -329,20 +346,26 @@ discard block |
||
329 | 346 | |
330 | 347 | $class = ''; |
331 | 348 | $localAlias = null; |
332 | - for ($localID = $tokenID + 1; $this->tokens[$localID][self::TOKEN_CODE] !== ';'; ++$localID) { |
|
333 | - if ($this->tokens[$localID][self::TOKEN_TYPE] == T_AS) { |
|
349 | + for ($localID = $tokenID + 1; $this->tokens[$localID][self::TOKEN_CODE] !== ';'; ++$localID) |
|
350 | + { |
|
351 | + if ($this->tokens[$localID][self::TOKEN_TYPE] == T_AS) |
|
352 | + { |
|
334 | 353 | $localAlias = ''; |
335 | 354 | continue; |
336 | 355 | } |
337 | 356 | |
338 | - if ($localAlias === null) { |
|
357 | + if ($localAlias === null) |
|
358 | + { |
|
339 | 359 | $class .= $this->tokens[$localID][self::TOKEN_CODE]; |
340 | - } else { |
|
360 | + } |
|
361 | + else |
|
362 | + { |
|
341 | 363 | $localAlias .= $this->tokens[$localID][self::TOKEN_CODE]; |
342 | 364 | } |
343 | 365 | } |
344 | 366 | |
345 | - if (empty($localAlias)) { |
|
367 | + if (empty($localAlias)) |
|
368 | + { |
|
346 | 369 | $names = explode('\\', $class); |
347 | 370 | $localAlias = end($names); |
348 | 371 | } |
@@ -355,9 +378,12 @@ discard block |
||
355 | 378 | */ |
356 | 379 | private function registerFunction(int $tokenID): void |
357 | 380 | { |
358 | - foreach ($this->declarations as $declarations) { |
|
359 | - foreach ($declarations as $location) { |
|
360 | - if ($tokenID >= $location[self::O_TOKEN] && $tokenID <= $location[self::C_TOKEN]) { |
|
381 | + foreach ($this->declarations as $declarations) |
|
382 | + { |
|
383 | + foreach ($declarations as $location) |
|
384 | + { |
|
385 | + if ($tokenID >= $location[self::O_TOKEN] && $tokenID <= $location[self::C_TOKEN]) |
|
386 | + { |
|
361 | 387 | //We are inside class, function is method |
362 | 388 | return; |
363 | 389 | } |
@@ -365,13 +391,15 @@ discard block |
||
365 | 391 | } |
366 | 392 | |
367 | 393 | $localID = $tokenID + 1; |
368 | - while ($this->tokens[$localID][self::TOKEN_TYPE] !== T_STRING) { |
|
394 | + while ($this->tokens[$localID][self::TOKEN_TYPE] !== T_STRING) |
|
395 | + { |
|
369 | 396 | //Fetching function name |
370 | 397 | ++$localID; |
371 | 398 | } |
372 | 399 | |
373 | 400 | $name = $this->tokens[$localID][self::TOKEN_CODE]; |
374 | - if (!empty($namespace = $this->activeNamespace($tokenID))) { |
|
401 | + if (!empty($namespace = $this->activeNamespace($tokenID))) |
|
402 | + { |
|
375 | 403 | $name = $namespace . self::NS_SEPARATOR . $name; |
376 | 404 | } |
377 | 405 | |
@@ -388,12 +416,14 @@ discard block |
||
388 | 416 | private function registerDeclaration(int $tokenID, int $tokenType): void |
389 | 417 | { |
390 | 418 | $localID = $tokenID + 1; |
391 | - while ($this->tokens[$localID][self::TOKEN_TYPE] !== T_STRING) { |
|
419 | + while ($this->tokens[$localID][self::TOKEN_TYPE] !== T_STRING) |
|
420 | + { |
|
392 | 421 | ++$localID; |
393 | 422 | } |
394 | 423 | |
395 | 424 | $name = $this->tokens[$localID][self::TOKEN_CODE]; |
396 | - if (!empty($namespace = $this->activeNamespace($tokenID))) { |
|
425 | + if (!empty($namespace = $this->activeNamespace($tokenID))) |
|
426 | + { |
|
397 | 427 | $name = $namespace . self::NS_SEPARATOR . $name; |
398 | 428 | } |
399 | 429 | |
@@ -458,11 +488,13 @@ discard block |
||
458 | 488 | |
459 | 489 | //Tokens used to re-enable token detection |
460 | 490 | $stopTokens = [T_STRING, T_WHITESPACE, T_DOUBLE_COLON, T_OBJECT_OPERATOR, T_NS_SEPARATOR]; |
461 | - foreach ($tokens as $tokenID => $token) { |
|
491 | + foreach ($tokens as $tokenID => $token) |
|
492 | + { |
|
462 | 493 | $tokenType = $token[self::TOKEN_TYPE]; |
463 | 494 | |
464 | 495 | //We are not indexing function declarations or functions called from $objects. |
465 | - if (\in_array($tokenType, [T_FUNCTION, T_OBJECT_OPERATOR, T_NEW])) { |
|
496 | + if (\in_array($tokenType, [T_FUNCTION, T_OBJECT_OPERATOR, T_NEW])) |
|
497 | + { |
|
466 | 498 | if ( |
467 | 499 | empty($argumentsTID) |
468 | 500 | && ( |
@@ -474,8 +506,11 @@ discard block |
||
474 | 506 | $ignore = true; |
475 | 507 | continue; |
476 | 508 | } |
477 | - } elseif ($ignore) { |
|
478 | - if (!\in_array($tokenType, $stopTokens)) { |
|
509 | + } |
|
510 | + elseif ($ignore) |
|
511 | + { |
|
512 | + if (!\in_array($tokenType, $stopTokens)) |
|
513 | + { |
|
479 | 514 | //Returning to search |
480 | 515 | $ignore = false; |
481 | 516 | } |
@@ -483,13 +518,16 @@ discard block |
||
483 | 518 | } |
484 | 519 | |
485 | 520 | //We are inside function, and there is "(", indexing arguments. |
486 | - if (!empty($invocationTID) && ($tokenType === '(' || $tokenType === '[')) { |
|
487 | - if (empty($argumentsTID)) { |
|
521 | + if (!empty($invocationTID) && ($tokenType === '(' || $tokenType === '[')) |
|
522 | + { |
|
523 | + if (empty($argumentsTID)) |
|
524 | + { |
|
488 | 525 | $argumentsTID = $tokenID; |
489 | 526 | } |
490 | 527 | |
491 | 528 | ++$level; |
492 | - if ($level != 1) { |
|
529 | + if ($level != 1) |
|
530 | + { |
|
493 | 531 | //Not arguments beginning, but arguments part |
494 | 532 | $arguments[$tokenID] = $token; |
495 | 533 | } |
@@ -498,16 +536,19 @@ discard block |
||
498 | 536 | } |
499 | 537 | |
500 | 538 | //We are inside function arguments and ")" met. |
501 | - if (!empty($invocationTID) && ($tokenType === ')' || $tokenType === ']')) { |
|
539 | + if (!empty($invocationTID) && ($tokenType === ')' || $tokenType === ']')) |
|
540 | + { |
|
502 | 541 | --$level; |
503 | - if ($level == -1) { |
|
542 | + if ($level == -1) |
|
543 | + { |
|
504 | 544 | $invocationTID = false; |
505 | 545 | $level = 0; |
506 | 546 | continue; |
507 | 547 | } |
508 | 548 | |
509 | 549 | //Function fully indexed, we can process it now. |
510 | - if ($level == 0) { |
|
550 | + if ($level == 0) |
|
551 | + { |
|
511 | 552 | $this->registerInvocation( |
512 | 553 | $invocationTID, |
513 | 554 | $argumentsTID, |
@@ -519,7 +560,9 @@ discard block |
||
519 | 560 | //Closing search |
520 | 561 | $arguments = []; |
521 | 562 | $argumentsTID = $invocationTID = false; |
522 | - } else { |
|
563 | + } |
|
564 | + else |
|
565 | + { |
|
523 | 566 | //Not arguments beginning, but arguments part |
524 | 567 | $arguments[$tokenID] = $token; |
525 | 568 | } |
@@ -528,13 +571,15 @@ discard block |
||
528 | 571 | } |
529 | 572 | |
530 | 573 | //Still inside arguments. |
531 | - if (!empty($invocationTID) && !empty($level)) { |
|
574 | + if (!empty($invocationTID) && !empty($level)) |
|
575 | + { |
|
532 | 576 | $arguments[$tokenID] = $token; |
533 | 577 | continue; |
534 | 578 | } |
535 | 579 | |
536 | 580 | //Nothing valuable to remember, will be parsed later. |
537 | - if (!empty($invocationTID) && \in_array($tokenType, $stopTokens)) { |
|
581 | + if (!empty($invocationTID) && \in_array($tokenType, $stopTokens)) |
|
582 | + { |
|
538 | 583 | continue; |
539 | 584 | } |
540 | 585 | |
@@ -573,7 +618,8 @@ discard block |
||
573 | 618 | |
574 | 619 | [$class, $operator, $name] = $this->fetchContext($invocationID, $argumentsID); |
575 | 620 | |
576 | - if (!empty($operator) && empty($class)) { |
|
621 | + if (!empty($operator) && empty($class)) |
|
622 | + { |
|
577 | 623 | //Non detectable |
578 | 624 | return; |
579 | 625 | } |
@@ -599,17 +645,22 @@ discard block |
||
599 | 645 | $name = \trim($this->getSource($invocationTID, $argumentsTID), '( '); |
600 | 646 | |
601 | 647 | //Let's try to fetch all information we need |
602 | - if (\str_contains($name, '->')) { |
|
648 | + if (\str_contains($name, '->')) |
|
649 | + { |
|
603 | 650 | $operator = '->'; |
604 | - } elseif (\str_contains($name, '::')) { |
|
651 | + } |
|
652 | + elseif (\str_contains($name, '::')) |
|
653 | + { |
|
605 | 654 | $operator = '::'; |
606 | 655 | } |
607 | 656 | |
608 | - if (!empty($operator)) { |
|
657 | + if (!empty($operator)) |
|
658 | + { |
|
609 | 659 | [$class, $name] = \explode($operator, $name); |
610 | 660 | |
611 | 661 | //We now have to clarify class name |
612 | - if (\in_array($class, ['self', 'static', '$this'])) { |
|
662 | + if (\in_array($class, ['self', 'static', '$this'])) |
|
663 | + { |
|
613 | 664 | $class = $this->activeDeclaration($invocationTID); |
614 | 665 | } |
615 | 666 | } |
@@ -622,9 +673,12 @@ discard block |
||
622 | 673 | */ |
623 | 674 | private function activeDeclaration(int $tokenID): string |
624 | 675 | { |
625 | - foreach ($this->declarations as $declarations) { |
|
626 | - foreach ($declarations as $name => $position) { |
|
627 | - if ($tokenID >= $position[self::O_TOKEN] && $tokenID <= $position[self::C_TOKEN]) { |
|
676 | + foreach ($this->declarations as $declarations) |
|
677 | + { |
|
678 | + foreach ($declarations as $name => $position) |
|
679 | + { |
|
680 | + if ($tokenID >= $position[self::O_TOKEN] && $tokenID <= $position[self::C_TOKEN]) |
|
681 | + { |
|
628 | 682 | return $name; |
629 | 683 | } |
630 | 684 | } |
@@ -639,8 +693,10 @@ discard block |
||
639 | 693 | */ |
640 | 694 | private function activeNamespace(int $tokenID): string |
641 | 695 | { |
642 | - foreach ($this->namespaces as $namespace => $position) { |
|
643 | - if ($tokenID >= $position[self::O_TOKEN] && $tokenID <= $position[self::C_TOKEN]) { |
|
696 | + foreach ($this->namespaces as $namespace => $position) |
|
697 | + { |
|
698 | + if ($tokenID >= $position[self::O_TOKEN] && $tokenID <= $position[self::C_TOKEN]) |
|
699 | + { |
|
644 | 700 | return $namespace; |
645 | 701 | } |
646 | 702 | } |
@@ -661,18 +717,22 @@ discard block |
||
661 | 717 | private function endingToken(int $tokenID): int |
662 | 718 | { |
663 | 719 | $level = null; |
664 | - for ($localID = $tokenID; $localID < $this->countTokens; ++$localID) { |
|
720 | + for ($localID = $tokenID; $localID < $this->countTokens; ++$localID) |
|
721 | + { |
|
665 | 722 | $token = $this->tokens[$localID]; |
666 | - if ($token[self::TOKEN_CODE] === '{') { |
|
723 | + if ($token[self::TOKEN_CODE] === '{') |
|
724 | + { |
|
667 | 725 | ++$level; |
668 | 726 | continue; |
669 | 727 | } |
670 | 728 | |
671 | - if ($token[self::TOKEN_CODE] === '}') { |
|
729 | + if ($token[self::TOKEN_CODE] === '}') |
|
730 | + { |
|
672 | 731 | --$level; |
673 | 732 | } |
674 | 733 | |
675 | - if ($level === 0) { |
|
734 | + if ($level === 0) |
|
735 | + { |
|
676 | 736 | break; |
677 | 737 | } |
678 | 738 | } |
@@ -685,7 +745,8 @@ discard block |
||
685 | 745 | */ |
686 | 746 | private function lineNumber(int $tokenID): int |
687 | 747 | { |
688 | - while (empty($this->tokens[$tokenID][self::TOKEN_LINE])) { |
|
748 | + while (empty($this->tokens[$tokenID][self::TOKEN_LINE])) |
|
749 | + { |
|
689 | 750 | --$tokenID; |
690 | 751 | } |
691 | 752 | |
@@ -698,7 +759,8 @@ discard block |
||
698 | 759 | private function getSource(int $startID, int $endID): string |
699 | 760 | { |
700 | 761 | $result = ''; |
701 | - for ($tokenID = $startID; $tokenID <= $endID; ++$tokenID) { |
|
762 | + for ($tokenID = $startID; $tokenID <= $endID; ++$tokenID) |
|
763 | + { |
|
702 | 764 | //Collecting function usage src |
703 | 765 | $result .= $this->tokens[$tokenID][self::TOKEN_CODE]; |
704 | 766 | } |