diff --git a/bench/Source/GotoLabelSource.php b/bench/Source/GotoLabelSource.php new file mode 100644 index 00000000000..1308cc4aa91 --- /dev/null +++ b/bench/Source/GotoLabelSource.php @@ -0,0 +1,771 @@ + 1) { + goto end; + dead: + $items = [$value, 1]; + foreach ($items as $item) { + $value += $item; + } + + end: + } + + return $value; + unreachable: + $value = $value * 1; + return $value; + } + + public function run2(int $value): int + { + if ($value > 2) { + goto end; + dead: + $items = [$value, 2]; + foreach ($items as $item) { + $value += $item; + } + + end: + } + + return $value; + unreachable: + $value = $value * 2; + return $value; + } + + public function run3(int $value): int + { + if ($value > 3) { + goto end; + dead: + $items = [$value, 3]; + foreach ($items as $item) { + $value += $item; + } + + end: + } + + return $value; + unreachable: + $value = $value * 3; + return $value; + } + + public function run4(int $value): int + { + if ($value > 4) { + goto end; + dead: + $items = [$value, 4]; + foreach ($items as $item) { + $value += $item; + } + + end: + } + + return $value; + unreachable: + $value = $value * 4; + return $value; + } + + public function run5(int $value): int + { + if ($value > 5) { + goto end; + dead: + $items = [$value, 5]; + foreach ($items as $item) { + $value += $item; + } + + end: + } + + return $value; + unreachable: + $value = $value * 5; + return $value; + } + + public function run6(int $value): int + { + if ($value > 6) { + goto end; + dead: + $items = [$value, 6]; + foreach ($items as $item) { + $value += $item; + } + + end: + } + + return $value; + unreachable: + $value = $value * 6; + return $value; + } + + public function run7(int $value): int + { + if ($value > 7) { + goto end; + dead: + $items = [$value, 7]; + foreach ($items as $item) { + $value += $item; + } + + end: + } + + return $value; + unreachable: + $value = $value * 7; + return $value; + } + + public function run8(int $value): int + { + if ($value > 8) { + goto end; + dead: + $items = [$value, 8]; + foreach ($items as $item) { + $value += $item; + } + + end: + } + + return $value; + unreachable: + $value = $value * 8; + return $value; + } + + public function run9(int $value): int + { + if ($value > 9) { + goto end; + dead: + $items = [$value, 9]; + foreach ($items as $item) { + $value += $item; + } + + end: + } + + return $value; + unreachable: + $value = $value * 9; + return $value; + } + + public function run10(int $value): int + { + if ($value > 10) { + goto end; + dead: + $items = [$value, 10]; + foreach ($items as $item) { + $value += $item; + } + + end: + } + + return $value; + unreachable: + $value = $value * 10; + return $value; + } + + public function run11(int $value): int + { + if ($value > 11) { + goto end; + dead: + $items = [$value, 11]; + foreach ($items as $item) { + $value += $item; + } + + end: + } + + return $value; + unreachable: + $value = $value * 11; + return $value; + } + + public function run12(int $value): int + { + if ($value > 12) { + goto end; + dead: + $items = [$value, 12]; + foreach ($items as $item) { + $value += $item; + } + + end: + } + + return $value; + unreachable: + $value = $value * 12; + return $value; + } + + public function run13(int $value): int + { + if ($value > 13) { + goto end; + dead: + $items = [$value, 13]; + foreach ($items as $item) { + $value += $item; + } + + end: + } + + return $value; + unreachable: + $value = $value * 13; + return $value; + } + + public function run14(int $value): int + { + if ($value > 14) { + goto end; + dead: + $items = [$value, 14]; + foreach ($items as $item) { + $value += $item; + } + + end: + } + + return $value; + unreachable: + $value = $value * 14; + return $value; + } + + public function run15(int $value): int + { + if ($value > 15) { + goto end; + dead: + $items = [$value, 15]; + foreach ($items as $item) { + $value += $item; + } + + end: + } + + return $value; + unreachable: + $value = $value * 15; + return $value; + } + + public function run16(int $value): int + { + if ($value > 16) { + goto end; + dead: + $items = [$value, 16]; + foreach ($items as $item) { + $value += $item; + } + + end: + } + + return $value; + unreachable: + $value = $value * 16; + return $value; + } + + public function run17(int $value): int + { + if ($value > 17) { + goto end; + dead: + $items = [$value, 17]; + foreach ($items as $item) { + $value += $item; + } + + end: + } + + return $value; + unreachable: + $value = $value * 17; + return $value; + } + + public function run18(int $value): int + { + if ($value > 18) { + goto end; + dead: + $items = [$value, 18]; + foreach ($items as $item) { + $value += $item; + } + + end: + } + + return $value; + unreachable: + $value = $value * 18; + return $value; + } + + public function run19(int $value): int + { + if ($value > 19) { + goto end; + dead: + $items = [$value, 19]; + foreach ($items as $item) { + $value += $item; + } + + end: + } + + return $value; + unreachable: + $value = $value * 19; + return $value; + } + + public function run20(int $value): int + { + if ($value > 20) { + goto end; + dead: + $items = [$value, 20]; + foreach ($items as $item) { + $value += $item; + } + + end: + } + + return $value; + unreachable: + $value = $value * 20; + return $value; + } + + public function run21(int $value): int + { + if ($value > 21) { + goto end; + dead: + $items = [$value, 21]; + foreach ($items as $item) { + $value += $item; + } + + end: + } + + return $value; + unreachable: + $value = $value * 21; + return $value; + } + + public function run22(int $value): int + { + if ($value > 22) { + goto end; + dead: + $items = [$value, 22]; + foreach ($items as $item) { + $value += $item; + } + + end: + } + + return $value; + unreachable: + $value = $value * 22; + return $value; + } + + public function run23(int $value): int + { + if ($value > 23) { + goto end; + dead: + $items = [$value, 23]; + foreach ($items as $item) { + $value += $item; + } + + end: + } + + return $value; + unreachable: + $value = $value * 23; + return $value; + } + + public function run24(int $value): int + { + if ($value > 24) { + goto end; + dead: + $items = [$value, 24]; + foreach ($items as $item) { + $value += $item; + } + + end: + } + + return $value; + unreachable: + $value = $value * 24; + return $value; + } + + public function run25(int $value): int + { + if ($value > 25) { + goto end; + dead: + $items = [$value, 25]; + foreach ($items as $item) { + $value += $item; + } + + end: + } + + return $value; + unreachable: + $value = $value * 25; + return $value; + } + + public function run26(int $value): int + { + if ($value > 26) { + goto end; + dead: + $items = [$value, 26]; + foreach ($items as $item) { + $value += $item; + } + + end: + } + + return $value; + unreachable: + $value = $value * 26; + return $value; + } + + public function run27(int $value): int + { + if ($value > 27) { + goto end; + dead: + $items = [$value, 27]; + foreach ($items as $item) { + $value += $item; + } + + end: + } + + return $value; + unreachable: + $value = $value * 27; + return $value; + } + + public function run28(int $value): int + { + if ($value > 28) { + goto end; + dead: + $items = [$value, 28]; + foreach ($items as $item) { + $value += $item; + } + + end: + } + + return $value; + unreachable: + $value = $value * 28; + return $value; + } + + public function run29(int $value): int + { + if ($value > 29) { + goto end; + dead: + $items = [$value, 29]; + foreach ($items as $item) { + $value += $item; + } + + end: + } + + return $value; + unreachable: + $value = $value * 29; + return $value; + } + + public function run30(int $value): int + { + if ($value > 30) { + goto end; + dead: + $items = [$value, 30]; + foreach ($items as $item) { + $value += $item; + } + + end: + } + + return $value; + unreachable: + $value = $value * 30; + return $value; + } + + public function run31(int $value): int + { + if ($value > 31) { + goto end; + dead: + $items = [$value, 31]; + foreach ($items as $item) { + $value += $item; + } + + end: + } + + return $value; + unreachable: + $value = $value * 31; + return $value; + } + + public function run32(int $value): int + { + if ($value > 32) { + goto end; + dead: + $items = [$value, 32]; + foreach ($items as $item) { + $value += $item; + } + + end: + } + + return $value; + unreachable: + $value = $value * 32; + return $value; + } + + public function run33(int $value): int + { + if ($value > 33) { + goto end; + dead: + $items = [$value, 33]; + foreach ($items as $item) { + $value += $item; + } + + end: + } + + return $value; + unreachable: + $value = $value * 33; + return $value; + } + + public function run34(int $value): int + { + if ($value > 34) { + goto end; + dead: + $items = [$value, 34]; + foreach ($items as $item) { + $value += $item; + } + + end: + } + + return $value; + unreachable: + $value = $value * 34; + return $value; + } + + public function run35(int $value): int + { + if ($value > 35) { + goto end; + dead: + $items = [$value, 35]; + foreach ($items as $item) { + $value += $item; + } + + end: + } + + return $value; + unreachable: + $value = $value * 35; + return $value; + } + + public function run36(int $value): int + { + if ($value > 36) { + goto end; + dead: + $items = [$value, 36]; + foreach ($items as $item) { + $value += $item; + } + + end: + } + + return $value; + unreachable: + $value = $value * 36; + return $value; + } + + public function run37(int $value): int + { + if ($value > 37) { + goto end; + dead: + $items = [$value, 37]; + foreach ($items as $item) { + $value += $item; + } + + end: + } + + return $value; + unreachable: + $value = $value * 37; + return $value; + } + + public function run38(int $value): int + { + if ($value > 38) { + goto end; + dead: + $items = [$value, 38]; + foreach ($items as $item) { + $value += $item; + } + + end: + } + + return $value; + unreachable: + $value = $value * 38; + return $value; + } + + public function run39(int $value): int + { + if ($value > 39) { + goto end; + dead: + $items = [$value, 39]; + foreach ($items as $item) { + $value += $item; + } + + end: + } + + return $value; + unreachable: + $value = $value * 39; + return $value; + } + + public function run40(int $value): int + { + if ($value > 40) { + goto end; + dead: + $items = [$value, 40]; + foreach ($items as $item) { + $value += $item; + } + + end: + } + + return $value; + unreachable: + $value = $value * 40; + return $value; + } +} diff --git a/bench/php_stan_node_scope_resolver_label_bench.php b/bench/php_stan_node_scope_resolver_label_bench.php new file mode 100644 index 00000000000..fe5fa18b778 --- /dev/null +++ b/bench/php_stan_node_scope_resolver_label_bench.php @@ -0,0 +1,93 @@ +create(); +$rectorConfig->boot(); + +/** @var TestingParser $testingParser */ +$testingParser = $rectorConfig->make(TestingParser::class); + +$sourceFilePaths = [ + 'no labels' => __DIR__ . '/../src/PhpParser/Printer/BetterStandardPrinter.php', + 'labels' => __DIR__ . '/Source/GotoLabelSource.php', +]; + +$cpuSeconds = static function (): float { + $usage = getrusage(); + return $usage['ru_utime.tv_sec'] + $usage['ru_utime.tv_usec'] / 1_000_000 + + $usage['ru_stime.tv_sec'] + $usage['ru_stime.tv_usec'] / 1_000_000; +}; + +printf('PHPStanNodeScopeResolver::processNodes() goto label benchmark%s', PHP_EOL); +printf(' repeat=%d%s', REPEAT, PHP_EOL); +printf('%s', PHP_EOL); + +foreach ($sourceFilePaths as $case => $sourceFilePath) { + // warm up autoloading / reflection once, excluded from timing + $stmts = $testingParser->parseFileToDecoratedNodes($sourceFilePath); + + $stmtCount = 0; + $unscopedStmtCount = 0; + SimpleCallableNodeTraverser::traverse($stmts, static function (Node $node) use ( + &$stmtCount, + &$unscopedStmtCount + ): null { + if ($node instanceof Stmt) { + ++$stmtCount; + if ($node->getAttribute(AttributeKey::SCOPE) === null) { + ++$unscopedStmtCount; + } + } + + return null; + }); + + $cpuStart = $cpuSeconds(); + $wallStart = hrtime(true); + + for ($repeat = 0; $repeat < REPEAT; ++$repeat) { + $testingParser->parseFileToDecoratedNodes($sourceFilePath); + } + + printf( + ' %-9s cpu=%.4fs wall=%.4fs stmts=%d without scope=%d%s', + $case, + $cpuSeconds() - $cpuStart, + (hrtime(true) - $wallStart) / 1_000_000_000, + $stmtCount, + $unscopedStmtCount, + PHP_EOL + ); +} diff --git a/rules-tests/Php54/Rector/Array_/LongArrayToShortArrayRector/Fixture/unreachable_after_goto_label.php.inc b/rules-tests/Php54/Rector/Array_/LongArrayToShortArrayRector/Fixture/unreachable_after_goto_label.php.inc new file mode 100644 index 00000000000..f5b1653757b --- /dev/null +++ b/rules-tests/Php54/Rector/Array_/LongArrayToShortArrayRector/Fixture/unreachable_after_goto_label.php.inc @@ -0,0 +1,21 @@ + +----- + diff --git a/src/NodeTypeResolver/PHPStan/Scope/PHPStanNodeScopeResolver.php b/src/NodeTypeResolver/PHPStan/Scope/PHPStanNodeScopeResolver.php index 7e510d472e8..0a0fa82c454 100644 --- a/src/NodeTypeResolver/PHPStan/Scope/PHPStanNodeScopeResolver.php +++ b/src/NodeTypeResolver/PHPStan/Scope/PHPStanNodeScopeResolver.php @@ -57,6 +57,7 @@ use PhpParser\Node\Stmt\For_; use PhpParser\Node\Stmt\Foreach_; use PhpParser\Node\Stmt\If_; +use PhpParser\Node\Stmt\Label; use PhpParser\Node\Stmt\Return_; use PhpParser\Node\Stmt\Switch_; use PhpParser\Node\Stmt\Trait_; @@ -82,6 +83,7 @@ use PHPStan\ShouldNotHappenException; use Rector\Contract\PhpParser\DecoratingNodeVisitorInterface; use Rector\NodeTypeResolver\Node\AttributeKey; +use Rector\PhpParser\Enum\NodeGroup; use Rector\PhpParser\Node\FileNode; use Rector\Util\Reflection\PrivatesAccessor; @@ -134,7 +136,15 @@ public function processNodes( */ private function fillScope(array $stmts, MutatingScope $scope): void { - $nodeCallback = function (Node $node, MutatingScope $mutatingScope) use (&$nodeCallback): void { + $stmtListsWithLabels = []; + $nodeCallback = function (Node $node, MutatingScope $mutatingScope) use (&$nodeCallback, &$stmtListsWithLabels): void { + if (NodeGroup::isStmtAwareNode($node)) { + $nodeStmts = $node->stmts ?? []; + if (array_any($nodeStmts, static fn (Stmt $stmt): bool => $stmt instanceof Label)) { + $stmtListsWithLabels[] = $nodeStmts; + } + } + if ($mutatingScope instanceof NodeCallbackScope) { $mutatingScope = $mutatingScope->toWalkScope(); } @@ -320,6 +330,40 @@ private function fillScope(array $stmts, MutatingScope $scope): void }; $this->nodeScopeResolverProcessNodes($stmts, $scope, $nodeCallback); + + while ($stmtListsWithLabels !== []) { + $this->processStatementsAfterLabels(array_pop($stmtListsWithLabels), $nodeCallback); + } + } + + /** + * @param Stmt[] $stmts + * @param callable(Node $node, MutatingScope $scope): void $nodeCallback + */ + private function processStatementsAfterLabels(array $stmts, callable $nodeCallback): void + { + $labelScope = null; + $unscopedStmts = []; + foreach ($stmts as $stmt) { + if ($stmt instanceof Label) { + if ($unscopedStmts !== [] && $labelScope instanceof MutatingScope) { + $this->nodeScopeResolverProcessNodes($unscopedStmts, $labelScope, $nodeCallback); + } + + $labelScope = $stmt->getAttribute(AttributeKey::SCOPE); + $unscopedStmts = []; + continue; + } + + if ($labelScope instanceof MutatingScope && ! $stmt->getAttribute(AttributeKey::SCOPE) instanceof MutatingScope) { + $unscopedStmts[] = $stmt; + } + } + + // PHPStan skips unreachable statements after a label without reporting an UnreachableStatementNode. + if ($unscopedStmts !== [] && $labelScope instanceof MutatingScope) { + $this->nodeScopeResolverProcessNodes($unscopedStmts, $labelScope, $nodeCallback); + } } private function processYield(Yield_ $yield, MutatingScope $mutatingScope): void