@@ -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 | } |
@@ -112,7 +112,7 @@ discard block |
||
| 112 | 112 | |
| 113 | 113 | public function __construct( |
| 114 | 114 | private readonly string $filename |
| 115 | - ) { |
|
| 115 | + ){ |
|
| 116 | 116 | $this->tokens = Tokenizer::getTokens($filename); |
| 117 | 117 | $this->countTokens = \count($this->tokens); |
| 118 | 118 | |
@@ -141,7 +141,7 @@ 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 | return []; |
| 146 | 146 | } |
| 147 | 147 | |
@@ -153,7 +153,7 @@ discard block |
||
| 153 | 153 | */ |
| 154 | 154 | public function getTraits(): array |
| 155 | 155 | { |
| 156 | - if (!isset($this->declarations['T_TRAIT'])) { |
|
| 156 | + if (!isset($this->declarations['T_TRAIT'])){ |
|
| 157 | 157 | return []; |
| 158 | 158 | } |
| 159 | 159 | |
@@ -165,7 +165,7 @@ discard block |
||
| 165 | 165 | */ |
| 166 | 166 | public function getInterfaces(): array |
| 167 | 167 | { |
| 168 | - if (!isset($this->declarations['T_INTERFACE'])) { |
|
| 168 | + if (!isset($this->declarations['T_INTERFACE'])){ |
|
| 169 | 169 | return []; |
| 170 | 170 | } |
| 171 | 171 | |
@@ -196,7 +196,7 @@ discard block |
||
| 196 | 196 | */ |
| 197 | 197 | public function getInvocations(): array |
| 198 | 198 | { |
| 199 | - if (empty($this->invocations)) { |
|
| 199 | + if (empty($this->invocations)){ |
|
| 200 | 200 | $this->locateInvocations($this->getTokens()); |
| 201 | 201 | } |
| 202 | 202 | |
@@ -224,12 +224,12 @@ discard block |
||
| 224 | 224 | */ |
| 225 | 225 | protected function locateDeclarations() |
| 226 | 226 | { |
| 227 | - foreach ($this->getTokens() as $tokenID => $token) { |
|
| 228 | - if (!\in_array($token[self::TOKEN_TYPE], self::$processTokens)) { |
|
| 227 | + foreach ($this->getTokens() as $tokenID => $token){ |
|
| 228 | + if (!\in_array($token[self::TOKEN_TYPE], self::$processTokens)){ |
|
| 229 | 229 | continue; |
| 230 | 230 | } |
| 231 | 231 | |
| 232 | - switch ($token[self::TOKEN_TYPE]) { |
|
| 232 | + switch ($token[self::TOKEN_TYPE]){ |
|
| 233 | 233 | case T_NAMESPACE: |
| 234 | 234 | $this->registerNamespace($tokenID); |
| 235 | 235 | break; |
@@ -245,17 +245,17 @@ discard block |
||
| 245 | 245 | case T_CLASS: |
| 246 | 246 | case T_TRAIT: |
| 247 | 247 | case T_INTERFACE: |
| 248 | - if ($this->isClassNameConst($tokenID)) { |
|
| 248 | + if ($this->isClassNameConst($tokenID)){ |
|
| 249 | 249 | // PHP5.5 ClassName::class constant |
| 250 | 250 | continue 2; |
| 251 | 251 | } |
| 252 | 252 | |
| 253 | - if ($this->isAnonymousClass($tokenID)) { |
|
| 253 | + if ($this->isAnonymousClass($tokenID)){ |
|
| 254 | 254 | // PHP7.0 Anonymous classes new class ('foo', 'bar') |
| 255 | 255 | continue 2; |
| 256 | 256 | } |
| 257 | 257 | |
| 258 | - if (!$this->isCorrectDeclaration($tokenID)) { |
|
| 258 | + if (!$this->isCorrectDeclaration($tokenID)){ |
|
| 259 | 259 | // PHP8.0 Named parameters ->foo(class: 'bar') |
| 260 | 260 | continue 2; |
| 261 | 261 | } |
@@ -272,7 +272,7 @@ discard block |
||
| 272 | 272 | } |
| 273 | 273 | |
| 274 | 274 | //Dropping empty namespace |
| 275 | - if (isset($this->namespaces[''])) { |
|
| 275 | + if (isset($this->namespaces[''])){ |
|
| 276 | 276 | $this->namespaces['\\'] = $this->namespaces['']; |
| 277 | 277 | unset($this->namespaces['']); |
| 278 | 278 | } |
@@ -286,14 +286,14 @@ discard block |
||
| 286 | 286 | $namespace = ''; |
| 287 | 287 | $localID = $tokenID + 1; |
| 288 | 288 | |
| 289 | - do { |
|
| 289 | + do{ |
|
| 290 | 290 | $token = $this->tokens[$localID++]; |
| 291 | - if ($token[self::TOKEN_CODE] === '{') { |
|
| 291 | + if ($token[self::TOKEN_CODE] === '{'){ |
|
| 292 | 292 | break; |
| 293 | 293 | } |
| 294 | 294 | |
| 295 | 295 | $namespace .= $token[self::TOKEN_CODE]; |
| 296 | - } while ( |
|
| 296 | + }while ( |
|
| 297 | 297 | isset($this->tokens[$localID]) |
| 298 | 298 | && $this->tokens[$localID][self::TOKEN_CODE] !== '{' |
| 299 | 299 | && $this->tokens[$localID][self::TOKEN_CODE] !== ';' |
@@ -303,13 +303,13 @@ discard block |
||
| 303 | 303 | $namespace = \trim($namespace); |
| 304 | 304 | |
| 305 | 305 | $uses = []; |
| 306 | - if (isset($this->namespaces[$namespace])) { |
|
| 306 | + if (isset($this->namespaces[$namespace])){ |
|
| 307 | 307 | $uses = $this->namespaces[$namespace]; |
| 308 | 308 | } |
| 309 | 309 | |
| 310 | - if ($this->tokens[$localID][self::TOKEN_CODE] === ';') { |
|
| 310 | + if ($this->tokens[$localID][self::TOKEN_CODE] === ';'){ |
|
| 311 | 311 | $endingID = \count($this->tokens) - 1; |
| 312 | - } else { |
|
| 312 | + }else{ |
|
| 313 | 313 | $endingID = $this->endingToken($tokenID); |
| 314 | 314 | } |
| 315 | 315 | |
@@ -329,20 +329,20 @@ discard block |
||
| 329 | 329 | |
| 330 | 330 | $class = ''; |
| 331 | 331 | $localAlias = null; |
| 332 | - for ($localID = $tokenID + 1; $this->tokens[$localID][self::TOKEN_CODE] !== ';'; ++$localID) { |
|
| 333 | - if ($this->tokens[$localID][self::TOKEN_TYPE] == T_AS) { |
|
| 332 | + for ($localID = $tokenID + 1; $this->tokens[$localID][self::TOKEN_CODE] !== ';'; ++$localID){ |
|
| 333 | + if ($this->tokens[$localID][self::TOKEN_TYPE] == T_AS){ |
|
| 334 | 334 | $localAlias = ''; |
| 335 | 335 | continue; |
| 336 | 336 | } |
| 337 | 337 | |
| 338 | - if ($localAlias === null) { |
|
| 338 | + if ($localAlias === null){ |
|
| 339 | 339 | $class .= $this->tokens[$localID][self::TOKEN_CODE]; |
| 340 | - } else { |
|
| 340 | + }else{ |
|
| 341 | 341 | $localAlias .= $this->tokens[$localID][self::TOKEN_CODE]; |
| 342 | 342 | } |
| 343 | 343 | } |
| 344 | 344 | |
| 345 | - if (empty($localAlias)) { |
|
| 345 | + if (empty($localAlias)){ |
|
| 346 | 346 | $names = explode('\\', $class); |
| 347 | 347 | $localAlias = end($names); |
| 348 | 348 | } |
@@ -355,9 +355,9 @@ discard block |
||
| 355 | 355 | */ |
| 356 | 356 | private function registerFunction(int $tokenID): void |
| 357 | 357 | { |
| 358 | - foreach ($this->declarations as $declarations) { |
|
| 359 | - foreach ($declarations as $location) { |
|
| 360 | - if ($tokenID >= $location[self::O_TOKEN] && $tokenID <= $location[self::C_TOKEN]) { |
|
| 358 | + foreach ($this->declarations as $declarations){ |
|
| 359 | + foreach ($declarations as $location){ |
|
| 360 | + if ($tokenID >= $location[self::O_TOKEN] && $tokenID <= $location[self::C_TOKEN]){ |
|
| 361 | 361 | //We are inside class, function is method |
| 362 | 362 | return; |
| 363 | 363 | } |
@@ -365,14 +365,14 @@ discard block |
||
| 365 | 365 | } |
| 366 | 366 | |
| 367 | 367 | $localID = $tokenID + 1; |
| 368 | - while ($this->tokens[$localID][self::TOKEN_TYPE] !== T_STRING) { |
|
| 368 | + while ($this->tokens[$localID][self::TOKEN_TYPE] !== T_STRING){ |
|
| 369 | 369 | //Fetching function name |
| 370 | 370 | ++$localID; |
| 371 | 371 | } |
| 372 | 372 | |
| 373 | 373 | $name = $this->tokens[$localID][self::TOKEN_CODE]; |
| 374 | - if (!empty($namespace = $this->activeNamespace($tokenID))) { |
|
| 375 | - $name = $namespace . self::NS_SEPARATOR . $name; |
|
| 374 | + if (!empty($namespace = $this->activeNamespace($tokenID))){ |
|
| 375 | + $name = $namespace.self::NS_SEPARATOR.$name; |
|
| 376 | 376 | } |
| 377 | 377 | |
| 378 | 378 | $this->functions[$name] = [ |
@@ -388,13 +388,13 @@ discard block |
||
| 388 | 388 | private function registerDeclaration(int $tokenID, int $tokenType): void |
| 389 | 389 | { |
| 390 | 390 | $localID = $tokenID + 1; |
| 391 | - while ($this->tokens[$localID][self::TOKEN_TYPE] !== T_STRING) { |
|
| 391 | + while ($this->tokens[$localID][self::TOKEN_TYPE] !== T_STRING){ |
|
| 392 | 392 | ++$localID; |
| 393 | 393 | } |
| 394 | 394 | |
| 395 | 395 | $name = $this->tokens[$localID][self::TOKEN_CODE]; |
| 396 | - if (!empty($namespace = $this->activeNamespace($tokenID))) { |
|
| 397 | - $name = $namespace . self::NS_SEPARATOR . $name; |
|
| 396 | + if (!empty($namespace = $this->activeNamespace($tokenID))){ |
|
| 397 | + $name = $namespace.self::NS_SEPARATOR.$name; |
|
| 398 | 398 | } |
| 399 | 399 | |
| 400 | 400 | $this->declarations[\token_name($tokenType)][$name] = [ |
@@ -416,7 +416,7 @@ discard block |
||
| 416 | 416 | /** |
| 417 | 417 | * Check if token ID represents anonymous class creation, e.g. `new class ('foo', 'bar')`. |
| 418 | 418 | */ |
| 419 | - private function isAnonymousClass(int|string $tokenID): bool |
|
| 419 | + private function isAnonymousClass(int | string $tokenID): bool |
|
| 420 | 420 | { |
| 421 | 421 | return $this->tokens[$tokenID][self::TOKEN_TYPE] === T_CLASS |
| 422 | 422 | && isset($this->tokens[$tokenID - 2]) |
@@ -426,7 +426,7 @@ discard block |
||
| 426 | 426 | /** |
| 427 | 427 | * Check if token ID represents named parameter with name `class`, e.g. `foo(class: SomeClass::name)`. |
| 428 | 428 | */ |
| 429 | - private function isCorrectDeclaration(int|string $tokenID): bool |
|
| 429 | + private function isCorrectDeclaration(int | string $tokenID): bool |
|
| 430 | 430 | { |
| 431 | 431 | return \in_array($this->tokens[$tokenID][self::TOKEN_TYPE], [T_CLASS, T_TRAIT, T_INTERFACE], true) |
| 432 | 432 | && isset($this->tokens[$tokenID + 2]) |
@@ -459,24 +459,24 @@ discard block |
||
| 459 | 459 | |
| 460 | 460 | //Tokens used to re-enable token detection |
| 461 | 461 | $stopTokens = [T_STRING, T_WHITESPACE, T_DOUBLE_COLON, T_OBJECT_OPERATOR, T_NS_SEPARATOR]; |
| 462 | - foreach ($tokens as $tokenID => $token) { |
|
| 462 | + foreach ($tokens as $tokenID => $token){ |
|
| 463 | 463 | $tokenType = $token[self::TOKEN_TYPE]; |
| 464 | 464 | |
| 465 | 465 | //We are not indexing function declarations or functions called from $objects. |
| 466 | - if (\in_array($tokenType, [T_FUNCTION, T_OBJECT_OPERATOR, T_NEW])) { |
|
| 466 | + if (\in_array($tokenType, [T_FUNCTION, T_OBJECT_OPERATOR, T_NEW])){ |
|
| 467 | 467 | if ( |
| 468 | 468 | empty($argumentsTID) |
| 469 | 469 | && ( |
| 470 | 470 | empty($invocationTID) |
| 471 | 471 | || $this->getSource($invocationTID, $tokenID - 1) !== '$this' |
| 472 | 472 | ) |
| 473 | - ) { |
|
| 473 | + ){ |
|
| 474 | 474 | //Not a call, function declaration, or object method |
| 475 | 475 | $ignore = true; |
| 476 | 476 | continue; |
| 477 | 477 | } |
| 478 | - } elseif ($ignore) { |
|
| 479 | - if (!\in_array($tokenType, $stopTokens)) { |
|
| 478 | + } elseif ($ignore){ |
|
| 479 | + if (!\in_array($tokenType, $stopTokens)){ |
|
| 480 | 480 | //Returning to search |
| 481 | 481 | $ignore = false; |
| 482 | 482 | } |
@@ -484,13 +484,13 @@ discard block |
||
| 484 | 484 | } |
| 485 | 485 | |
| 486 | 486 | //We are inside function, and there is "(", indexing arguments. |
| 487 | - if (!empty($invocationTID) && ($tokenType === '(' || $tokenType === '[')) { |
|
| 488 | - if (empty($argumentsTID)) { |
|
| 487 | + if (!empty($invocationTID) && ($tokenType === '(' || $tokenType === '[')){ |
|
| 488 | + if (empty($argumentsTID)){ |
|
| 489 | 489 | $argumentsTID = $tokenID; |
| 490 | 490 | } |
| 491 | 491 | |
| 492 | 492 | ++$level; |
| 493 | - if ($level != 1) { |
|
| 493 | + if ($level != 1){ |
|
| 494 | 494 | //Not arguments beginning, but arguments part |
| 495 | 495 | $arguments[$tokenID] = $token; |
| 496 | 496 | } |
@@ -499,16 +499,16 @@ discard block |
||
| 499 | 499 | } |
| 500 | 500 | |
| 501 | 501 | //We are inside function arguments and ")" met. |
| 502 | - if (!empty($invocationTID) && ($tokenType === ')' || $tokenType === ']')) { |
|
| 502 | + if (!empty($invocationTID) && ($tokenType === ')' || $tokenType === ']')){ |
|
| 503 | 503 | --$level; |
| 504 | - if ($level == -1) { |
|
| 504 | + if ($level == -1){ |
|
| 505 | 505 | $invocationTID = false; |
| 506 | 506 | $level = 0; |
| 507 | 507 | continue; |
| 508 | 508 | } |
| 509 | 509 | |
| 510 | 510 | //Function fully indexed, we can process it now. |
| 511 | - if ($level == 0) { |
|
| 511 | + if ($level == 0){ |
|
| 512 | 512 | $this->registerInvocation( |
| 513 | 513 | $invocationTID, |
| 514 | 514 | $argumentsTID, |
@@ -520,7 +520,7 @@ discard block |
||
| 520 | 520 | //Closing search |
| 521 | 521 | $arguments = []; |
| 522 | 522 | $argumentsTID = $invocationTID = false; |
| 523 | - } else { |
|
| 523 | + }else{ |
|
| 524 | 524 | //Not arguments beginning, but arguments part |
| 525 | 525 | $arguments[$tokenID] = $token; |
| 526 | 526 | } |
@@ -529,13 +529,13 @@ discard block |
||
| 529 | 529 | } |
| 530 | 530 | |
| 531 | 531 | //Still inside arguments. |
| 532 | - if (!empty($invocationTID) && !empty($level)) { |
|
| 532 | + if (!empty($invocationTID) && !empty($level)){ |
|
| 533 | 533 | $arguments[$tokenID] = $token; |
| 534 | 534 | continue; |
| 535 | 535 | } |
| 536 | 536 | |
| 537 | 537 | //Nothing valuable to remember, will be parsed later. |
| 538 | - if (!empty($invocationTID) && \in_array($tokenType, $stopTokens)) { |
|
| 538 | + if (!empty($invocationTID) && \in_array($tokenType, $stopTokens)){ |
|
| 539 | 539 | continue; |
| 540 | 540 | } |
| 541 | 541 | |
@@ -545,7 +545,7 @@ discard block |
||
| 545 | 545 | || $tokenType == T_STATIC |
| 546 | 546 | || $tokenType == T_NS_SEPARATOR |
| 547 | 547 | || ($tokenType == T_VARIABLE && $token[self::TOKEN_CODE] === '$this') |
| 548 | - ) { |
|
| 548 | + ){ |
|
| 549 | 549 | $invocationTID = $tokenID; |
| 550 | 550 | $level = 0; |
| 551 | 551 | |
@@ -574,7 +574,7 @@ discard block |
||
| 574 | 574 | |
| 575 | 575 | [$class, $operator, $name] = $this->fetchContext($invocationID, $argumentsID); |
| 576 | 576 | |
| 577 | - if (!empty($operator) && empty($class)) { |
|
| 577 | + if (!empty($operator) && empty($class)){ |
|
| 578 | 578 | //Non detectable |
| 579 | 579 | return; |
| 580 | 580 | } |
@@ -600,17 +600,17 @@ discard block |
||
| 600 | 600 | $name = \trim($this->getSource($invocationTID, $argumentsTID), '( '); |
| 601 | 601 | |
| 602 | 602 | //Let's try to fetch all information we need |
| 603 | - if (\str_contains($name, '->')) { |
|
| 603 | + if (\str_contains($name, '->')){ |
|
| 604 | 604 | $operator = '->'; |
| 605 | - } elseif (\str_contains($name, '::')) { |
|
| 605 | + } elseif (\str_contains($name, '::')){ |
|
| 606 | 606 | $operator = '::'; |
| 607 | 607 | } |
| 608 | 608 | |
| 609 | - if (!empty($operator)) { |
|
| 609 | + if (!empty($operator)){ |
|
| 610 | 610 | [$class, $name] = \explode($operator, $name); |
| 611 | 611 | |
| 612 | 612 | //We now have to clarify class name |
| 613 | - if (\in_array($class, ['self', 'static', '$this'])) { |
|
| 613 | + if (\in_array($class, ['self', 'static', '$this'])){ |
|
| 614 | 614 | $class = $this->activeDeclaration($invocationTID); |
| 615 | 615 | } |
| 616 | 616 | } |
@@ -623,9 +623,9 @@ discard block |
||
| 623 | 623 | */ |
| 624 | 624 | private function activeDeclaration(int $tokenID): string |
| 625 | 625 | { |
| 626 | - foreach ($this->declarations as $declarations) { |
|
| 627 | - foreach ($declarations as $name => $position) { |
|
| 628 | - if ($tokenID >= $position[self::O_TOKEN] && $tokenID <= $position[self::C_TOKEN]) { |
|
| 626 | + foreach ($this->declarations as $declarations){ |
|
| 627 | + foreach ($declarations as $name => $position){ |
|
| 628 | + if ($tokenID >= $position[self::O_TOKEN] && $tokenID <= $position[self::C_TOKEN]){ |
|
| 629 | 629 | return $name; |
| 630 | 630 | } |
| 631 | 631 | } |
@@ -640,8 +640,8 @@ discard block |
||
| 640 | 640 | */ |
| 641 | 641 | private function activeNamespace(int $tokenID): string |
| 642 | 642 | { |
| 643 | - foreach ($this->namespaces as $namespace => $position) { |
|
| 644 | - if ($tokenID >= $position[self::O_TOKEN] && $tokenID <= $position[self::C_TOKEN]) { |
|
| 643 | + foreach ($this->namespaces as $namespace => $position){ |
|
| 644 | + if ($tokenID >= $position[self::O_TOKEN] && $tokenID <= $position[self::C_TOKEN]){ |
|
| 645 | 645 | return $namespace; |
| 646 | 646 | } |
| 647 | 647 | } |
@@ -662,18 +662,18 @@ discard block |
||
| 662 | 662 | private function endingToken(int $tokenID): int |
| 663 | 663 | { |
| 664 | 664 | $level = null; |
| 665 | - for ($localID = $tokenID; $localID < $this->countTokens; ++$localID) { |
|
| 665 | + for ($localID = $tokenID; $localID < $this->countTokens; ++$localID){ |
|
| 666 | 666 | $token = $this->tokens[$localID]; |
| 667 | - if ($token[self::TOKEN_CODE] === '{') { |
|
| 667 | + if ($token[self::TOKEN_CODE] === '{'){ |
|
| 668 | 668 | ++$level; |
| 669 | 669 | continue; |
| 670 | 670 | } |
| 671 | 671 | |
| 672 | - if ($token[self::TOKEN_CODE] === '}') { |
|
| 672 | + if ($token[self::TOKEN_CODE] === '}'){ |
|
| 673 | 673 | --$level; |
| 674 | 674 | } |
| 675 | 675 | |
| 676 | - if ($level === 0) { |
|
| 676 | + if ($level === 0){ |
|
| 677 | 677 | break; |
| 678 | 678 | } |
| 679 | 679 | } |
@@ -686,7 +686,7 @@ discard block |
||
| 686 | 686 | */ |
| 687 | 687 | private function lineNumber(int $tokenID): int |
| 688 | 688 | { |
| 689 | - while (empty($this->tokens[$tokenID][self::TOKEN_LINE])) { |
|
| 689 | + while (empty($this->tokens[$tokenID][self::TOKEN_LINE])){ |
|
| 690 | 690 | --$tokenID; |
| 691 | 691 | } |
| 692 | 692 | |
@@ -699,7 +699,7 @@ discard block |
||
| 699 | 699 | private function getSource(int $startID, int $endID): string |
| 700 | 700 | { |
| 701 | 701 | $result = ''; |
| 702 | - for ($tokenID = $startID; $tokenID <= $endID; ++$tokenID) { |
|
| 702 | + for ($tokenID = $startID; $tokenID <= $endID; ++$tokenID){ |
|
| 703 | 703 | //Collecting function usage src |
| 704 | 704 | $result .= $this->tokens[$tokenID][self::TOKEN_CODE]; |
| 705 | 705 | } |
@@ -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 | } |
@@ -19,13 +19,13 @@ |
||
| 19 | 19 | { |
| 20 | 20 | |
| 21 | 21 | } |
| 22 | -class;FOO; |
|
| 22 | +class; FOO; |
|
| 23 | 23 | |
| 24 | 24 | <<<'class' |
| 25 | 25 | class FooBar |
| 26 | 26 | { |
| 27 | 27 | |
| 28 | 28 | } |
| 29 | -class ;FOO; |
|
| 29 | +class; FOO; |
|
| 30 | 30 | } |
| 31 | 31 | } |
| 32 | 32 | \ No newline at end of file |
@@ -11,7 +11,7 @@ |
||
| 11 | 11 | public function __construct() |
| 12 | 12 | { |
| 13 | 13 | <<<class |
| 14 | -FooBar |
|
| 14 | +foobar |
|
| 15 | 15 | class; |
| 16 | 16 | |
| 17 | 17 | <<<class |
@@ -16,17 +16,17 @@ discard block |
||
| 16 | 16 | $this->assertContains(TestTrait::class, $reflection->getTraits()); |
| 17 | 17 | $this->assertContains(TestInterface::class, $reflection->getInterfaces()); |
| 18 | 18 | |
| 19 | - $this->assertSame([__NAMESPACE__ . '\hello'], $reflection->getFunctions()); |
|
| 19 | + $this->assertSame([__NAMESPACE__.'\hello'], $reflection->getFunctions()); |
|
| 20 | 20 | |
| 21 | 21 | $functionA = null; |
| 22 | 22 | $functionB = null; |
| 23 | 23 | |
| 24 | - foreach ($reflection->getInvocations() as $invocation) { |
|
| 25 | - if ($invocation->getName() == 'test_function_a') { |
|
| 24 | + foreach ($reflection->getInvocations() as $invocation){ |
|
| 25 | + if ($invocation->getName() == 'test_function_a'){ |
|
| 26 | 26 | $functionA = $invocation; |
| 27 | 27 | } |
| 28 | 28 | |
| 29 | - if ($invocation->getName() == 'test_function_b') { |
|
| 29 | + if ($invocation->getName() == 'test_function_b'){ |
|
| 30 | 30 | $functionB = $invocation; |
| 31 | 31 | } |
| 32 | 32 | } |
@@ -53,7 +53,7 @@ discard block |
||
| 53 | 53 | |
| 54 | 54 | public function testReflectionFileWithNamedParameters(): void |
| 55 | 55 | { |
| 56 | - $reflection = new ReflectionFile(__DIR__ . '/Classes/ClassWithNamedParameter.php'); |
|
| 56 | + $reflection = new ReflectionFile(__DIR__.'/Classes/ClassWithNamedParameter.php'); |
|
| 57 | 57 | |
| 58 | 58 | $this->assertSame([ |
| 59 | 59 | 'Spiral\Tests\Tokenizer\Classes\ClassWithNamedParameter', |
@@ -62,7 +62,7 @@ discard block |
||
| 62 | 62 | |
| 63 | 63 | public function testReflectionFileAnonymousClass(): void |
| 64 | 64 | { |
| 65 | - $reflection = new ReflectionFile(__DIR__ . '/Classes/ClassWithAnonymousClass.php'); |
|
| 65 | + $reflection = new ReflectionFile(__DIR__.'/Classes/ClassWithAnonymousClass.php'); |
|
| 66 | 66 | |
| 67 | 67 | $this->assertSame([ |
| 68 | 68 | 'Spiral\Tests\Tokenizer\Classes\ClassWithAnonymousClass', |
@@ -71,7 +71,7 @@ discard block |
||
| 71 | 71 | |
| 72 | 72 | public function testReflectionFileWithHeredoc(): void |
| 73 | 73 | { |
| 74 | - $reflection = new ReflectionFile(__DIR__ . '/Classes/ClassWithHeredoc.php'); |
|
| 74 | + $reflection = new ReflectionFile(__DIR__.'/Classes/ClassWithHeredoc.php'); |
|
| 75 | 75 | |
| 76 | 76 | $this->assertSame([ |
| 77 | 77 | 'Spiral\Tests\Tokenizer\Classes\ClassWithHeredoc', |