@@ -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->isCorrectDeclaration($tokenID)) { |
|
| 267 | + if (!$this->isCorrectDeclaration($tokenID)) |
|
| 268 | + { |
|
| 259 | 269 | // PHP8.0 Named parameters ->foo(class: 'bar') |
| 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 | |
@@ -459,11 +489,13 @@ discard block |
||
| 459 | 489 | |
| 460 | 490 | //Tokens used to re-enable token detection |
| 461 | 491 | $stopTokens = [T_STRING, T_WHITESPACE, T_DOUBLE_COLON, T_OBJECT_OPERATOR, T_NS_SEPARATOR]; |
| 462 | - foreach ($tokens as $tokenID => $token) { |
|
| 492 | + foreach ($tokens as $tokenID => $token) |
|
| 493 | + { |
|
| 463 | 494 | $tokenType = $token[self::TOKEN_TYPE]; |
| 464 | 495 | |
| 465 | 496 | //We are not indexing function declarations or functions called from $objects. |
| 466 | - if (\in_array($tokenType, [T_FUNCTION, T_OBJECT_OPERATOR, T_NEW])) { |
|
| 497 | + if (\in_array($tokenType, [T_FUNCTION, T_OBJECT_OPERATOR, T_NEW])) |
|
| 498 | + { |
|
| 467 | 499 | if ( |
| 468 | 500 | empty($argumentsTID) |
| 469 | 501 | && ( |
@@ -475,8 +507,11 @@ discard block |
||
| 475 | 507 | $ignore = true; |
| 476 | 508 | continue; |
| 477 | 509 | } |
| 478 | - } elseif ($ignore) { |
|
| 479 | - if (!\in_array($tokenType, $stopTokens)) { |
|
| 510 | + } |
|
| 511 | + elseif ($ignore) |
|
| 512 | + { |
|
| 513 | + if (!\in_array($tokenType, $stopTokens)) |
|
| 514 | + { |
|
| 480 | 515 | //Returning to search |
| 481 | 516 | $ignore = false; |
| 482 | 517 | } |
@@ -484,13 +519,16 @@ discard block |
||
| 484 | 519 | } |
| 485 | 520 | |
| 486 | 521 | //We are inside function, and there is "(", indexing arguments. |
| 487 | - if (!empty($invocationTID) && ($tokenType === '(' || $tokenType === '[')) { |
|
| 488 | - if (empty($argumentsTID)) { |
|
| 522 | + if (!empty($invocationTID) && ($tokenType === '(' || $tokenType === '[')) |
|
| 523 | + { |
|
| 524 | + if (empty($argumentsTID)) |
|
| 525 | + { |
|
| 489 | 526 | $argumentsTID = $tokenID; |
| 490 | 527 | } |
| 491 | 528 | |
| 492 | 529 | ++$level; |
| 493 | - if ($level != 1) { |
|
| 530 | + if ($level != 1) |
|
| 531 | + { |
|
| 494 | 532 | //Not arguments beginning, but arguments part |
| 495 | 533 | $arguments[$tokenID] = $token; |
| 496 | 534 | } |
@@ -499,16 +537,19 @@ discard block |
||
| 499 | 537 | } |
| 500 | 538 | |
| 501 | 539 | //We are inside function arguments and ")" met. |
| 502 | - if (!empty($invocationTID) && ($tokenType === ')' || $tokenType === ']')) { |
|
| 540 | + if (!empty($invocationTID) && ($tokenType === ')' || $tokenType === ']')) |
|
| 541 | + { |
|
| 503 | 542 | --$level; |
| 504 | - if ($level == -1) { |
|
| 543 | + if ($level == -1) |
|
| 544 | + { |
|
| 505 | 545 | $invocationTID = false; |
| 506 | 546 | $level = 0; |
| 507 | 547 | continue; |
| 508 | 548 | } |
| 509 | 549 | |
| 510 | 550 | //Function fully indexed, we can process it now. |
| 511 | - if ($level == 0) { |
|
| 551 | + if ($level == 0) |
|
| 552 | + { |
|
| 512 | 553 | $this->registerInvocation( |
| 513 | 554 | $invocationTID, |
| 514 | 555 | $argumentsTID, |
@@ -520,7 +561,9 @@ discard block |
||
| 520 | 561 | //Closing search |
| 521 | 562 | $arguments = []; |
| 522 | 563 | $argumentsTID = $invocationTID = false; |
| 523 | - } else { |
|
| 564 | + } |
|
| 565 | + else |
|
| 566 | + { |
|
| 524 | 567 | //Not arguments beginning, but arguments part |
| 525 | 568 | $arguments[$tokenID] = $token; |
| 526 | 569 | } |
@@ -529,13 +572,15 @@ discard block |
||
| 529 | 572 | } |
| 530 | 573 | |
| 531 | 574 | //Still inside arguments. |
| 532 | - if (!empty($invocationTID) && !empty($level)) { |
|
| 575 | + if (!empty($invocationTID) && !empty($level)) |
|
| 576 | + { |
|
| 533 | 577 | $arguments[$tokenID] = $token; |
| 534 | 578 | continue; |
| 535 | 579 | } |
| 536 | 580 | |
| 537 | 581 | //Nothing valuable to remember, will be parsed later. |
| 538 | - if (!empty($invocationTID) && \in_array($tokenType, $stopTokens)) { |
|
| 582 | + if (!empty($invocationTID) && \in_array($tokenType, $stopTokens)) |
|
| 583 | + { |
|
| 539 | 584 | continue; |
| 540 | 585 | } |
| 541 | 586 | |
@@ -574,7 +619,8 @@ discard block |
||
| 574 | 619 | |
| 575 | 620 | [$class, $operator, $name] = $this->fetchContext($invocationID, $argumentsID); |
| 576 | 621 | |
| 577 | - if (!empty($operator) && empty($class)) { |
|
| 622 | + if (!empty($operator) && empty($class)) |
|
| 623 | + { |
|
| 578 | 624 | //Non detectable |
| 579 | 625 | return; |
| 580 | 626 | } |
@@ -600,17 +646,22 @@ discard block |
||
| 600 | 646 | $name = \trim($this->getSource($invocationTID, $argumentsTID), '( '); |
| 601 | 647 | |
| 602 | 648 | //Let's try to fetch all information we need |
| 603 | - if (\str_contains($name, '->')) { |
|
| 649 | + if (\str_contains($name, '->')) |
|
| 650 | + { |
|
| 604 | 651 | $operator = '->'; |
| 605 | - } elseif (\str_contains($name, '::')) { |
|
| 652 | + } |
|
| 653 | + elseif (\str_contains($name, '::')) |
|
| 654 | + { |
|
| 606 | 655 | $operator = '::'; |
| 607 | 656 | } |
| 608 | 657 | |
| 609 | - if (!empty($operator)) { |
|
| 658 | + if (!empty($operator)) |
|
| 659 | + { |
|
| 610 | 660 | [$class, $name] = \explode($operator, $name); |
| 611 | 661 | |
| 612 | 662 | //We now have to clarify class name |
| 613 | - if (\in_array($class, ['self', 'static', '$this'])) { |
|
| 663 | + if (\in_array($class, ['self', 'static', '$this'])) |
|
| 664 | + { |
|
| 614 | 665 | $class = $this->activeDeclaration($invocationTID); |
| 615 | 666 | } |
| 616 | 667 | } |
@@ -623,9 +674,12 @@ discard block |
||
| 623 | 674 | */ |
| 624 | 675 | private function activeDeclaration(int $tokenID): string |
| 625 | 676 | { |
| 626 | - foreach ($this->declarations as $declarations) { |
|
| 627 | - foreach ($declarations as $name => $position) { |
|
| 628 | - if ($tokenID >= $position[self::O_TOKEN] && $tokenID <= $position[self::C_TOKEN]) { |
|
| 677 | + foreach ($this->declarations as $declarations) |
|
| 678 | + { |
|
| 679 | + foreach ($declarations as $name => $position) |
|
| 680 | + { |
|
| 681 | + if ($tokenID >= $position[self::O_TOKEN] && $tokenID <= $position[self::C_TOKEN]) |
|
| 682 | + { |
|
| 629 | 683 | return $name; |
| 630 | 684 | } |
| 631 | 685 | } |
@@ -640,8 +694,10 @@ discard block |
||
| 640 | 694 | */ |
| 641 | 695 | private function activeNamespace(int $tokenID): string |
| 642 | 696 | { |
| 643 | - foreach ($this->namespaces as $namespace => $position) { |
|
| 644 | - if ($tokenID >= $position[self::O_TOKEN] && $tokenID <= $position[self::C_TOKEN]) { |
|
| 697 | + foreach ($this->namespaces as $namespace => $position) |
|
| 698 | + { |
|
| 699 | + if ($tokenID >= $position[self::O_TOKEN] && $tokenID <= $position[self::C_TOKEN]) |
|
| 700 | + { |
|
| 645 | 701 | return $namespace; |
| 646 | 702 | } |
| 647 | 703 | } |
@@ -662,18 +718,22 @@ discard block |
||
| 662 | 718 | private function endingToken(int $tokenID): int |
| 663 | 719 | { |
| 664 | 720 | $level = null; |
| 665 | - for ($localID = $tokenID; $localID < $this->countTokens; ++$localID) { |
|
| 721 | + for ($localID = $tokenID; $localID < $this->countTokens; ++$localID) |
|
| 722 | + { |
|
| 666 | 723 | $token = $this->tokens[$localID]; |
| 667 | - if ($token[self::TOKEN_CODE] === '{') { |
|
| 724 | + if ($token[self::TOKEN_CODE] === '{') |
|
| 725 | + { |
|
| 668 | 726 | ++$level; |
| 669 | 727 | continue; |
| 670 | 728 | } |
| 671 | 729 | |
| 672 | - if ($token[self::TOKEN_CODE] === '}') { |
|
| 730 | + if ($token[self::TOKEN_CODE] === '}') |
|
| 731 | + { |
|
| 673 | 732 | --$level; |
| 674 | 733 | } |
| 675 | 734 | |
| 676 | - if ($level === 0) { |
|
| 735 | + if ($level === 0) |
|
| 736 | + { |
|
| 677 | 737 | break; |
| 678 | 738 | } |
| 679 | 739 | } |
@@ -686,7 +746,8 @@ discard block |
||
| 686 | 746 | */ |
| 687 | 747 | private function lineNumber(int $tokenID): int |
| 688 | 748 | { |
| 689 | - while (empty($this->tokens[$tokenID][self::TOKEN_LINE])) { |
|
| 749 | + while (empty($this->tokens[$tokenID][self::TOKEN_LINE])) |
|
| 750 | + { |
|
| 690 | 751 | --$tokenID; |
| 691 | 752 | } |
| 692 | 753 | |
@@ -699,7 +760,8 @@ discard block |
||
| 699 | 760 | private function getSource(int $startID, int $endID): string |
| 700 | 761 | { |
| 701 | 762 | $result = ''; |
| 702 | - for ($tokenID = $startID; $tokenID <= $endID; ++$tokenID) { |
|
| 763 | + for ($tokenID = $startID; $tokenID <= $endID; ++$tokenID) |
|
| 764 | + { |
|
| 703 | 765 | //Collecting function usage src |
| 704 | 766 | $result .= $this->tokens[$tokenID][self::TOKEN_CODE]; |
| 705 | 767 | } |