From 42d034e68ff493266d7fa6b91135e63a50cc2f3b Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Tue, 17 Nov 2026 17:35:02 +0700 Subject: [PATCH 1/5] Fix missing scope for unreachable statements after labels --- .../Scope/PHPStanNodeScopeResolver.php | 47 ++++++++++++++++++- 1 file changed, 46 insertions(+), 1 deletion(-) diff --git a/src/NodeTypeResolver/PHPStan/Scope/PHPStanNodeScopeResolver.php b/src/NodeTypeResolver/PHPStan/Scope/PHPStanNodeScopeResolver.php index 7e510d472e8..7cb36c716f8 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,16 @@ public function processNodes( */ private function fillScope(array $stmts, MutatingScope $scope): void { - $nodeCallback = function (Node $node, MutatingScope $mutatingScope) use (&$nodeCallback): void { + $stmtListsWithLabels = [$stmts]; + $nodeCallback = function (Node $node, MutatingScope $mutatingScope) use (&$nodeCallback, &$stmtListsWithLabels): void { + if (NodeGroup::isStmtAwareNode($node)) { + $nodeStmts = $node->stmts ?? []; + $label = array_find($nodeStmts, static fn (Stmt $stmt): bool => $stmt instanceof Label); + if ($label instanceof Label) { + $stmtListsWithLabels[spl_object_id($label)] = $nodeStmts; + } + } + if ($mutatingScope instanceof NodeCallbackScope) { $mutatingScope = $mutatingScope->toWalkScope(); } @@ -320,6 +331,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 From 07dc61557481a402add5da7f2433e6de2806204a Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Tue, 17 Nov 2026 17:36:52 +0700 Subject: [PATCH 2/5] add fixture --- .../unreachable_after_goto_label.php.inc | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 rules-tests/Php54/Rector/Array_/LongArrayToShortArrayRector/Fixture/unreachable_after_goto_label.php.inc 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..db87e90a991 --- /dev/null +++ b/rules-tests/Php54/Rector/Array_/LongArrayToShortArrayRector/Fixture/unreachable_after_goto_label.php.inc @@ -0,0 +1,17 @@ + +----- + From e610d72179f393e25a3da26bd5f4c86b6e36111a Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Tue, 17 Nov 2026 17:40:23 +0700 Subject: [PATCH 3/5] fix init --- .../Fixture/unreachable_after_goto_label.php.inc | 4 ++++ .../PHPStan/Scope/PHPStanNodeScopeResolver.php | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) 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 index db87e90a991..f5b1653757b 100644 --- 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 @@ -1,5 +1,7 @@ stmts ?? []; From 7f71664f3a3a615f20db620d1567c9c265f38686 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Tue, 17 Nov 2026 17:45:31 +0700 Subject: [PATCH 4/5] final touch: clean up unneeded spl_object_id() --- .../PHPStan/Scope/PHPStanNodeScopeResolver.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/NodeTypeResolver/PHPStan/Scope/PHPStanNodeScopeResolver.php b/src/NodeTypeResolver/PHPStan/Scope/PHPStanNodeScopeResolver.php index aea0a663b42..0a0fa82c454 100644 --- a/src/NodeTypeResolver/PHPStan/Scope/PHPStanNodeScopeResolver.php +++ b/src/NodeTypeResolver/PHPStan/Scope/PHPStanNodeScopeResolver.php @@ -140,9 +140,8 @@ private function fillScope(array $stmts, MutatingScope $scope): void $nodeCallback = function (Node $node, MutatingScope $mutatingScope) use (&$nodeCallback, &$stmtListsWithLabels): void { if (NodeGroup::isStmtAwareNode($node)) { $nodeStmts = $node->stmts ?? []; - $label = array_find($nodeStmts, static fn (Stmt $stmt): bool => $stmt instanceof Label); - if ($label instanceof Label) { - $stmtListsWithLabels[spl_object_id($label)] = $nodeStmts; + if (array_any($nodeStmts, static fn (Stmt $stmt): bool => $stmt instanceof Label)) { + $stmtListsWithLabels[] = $nodeStmts; } } From 4aa630fc27659421e622c6606d47bdc1fa804ef2 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Tue, 17 Nov 2026 22:59:39 +0700 Subject: [PATCH 5/5] add benchmark for label tweak --- bench/Source/GotoLabelSource.php | 771 ++++++++++++++++++ ...p_stan_node_scope_resolver_label_bench.php | 93 +++ 2 files changed, 864 insertions(+) create mode 100644 bench/Source/GotoLabelSource.php create mode 100644 bench/php_stan_node_scope_resolver_label_bench.php 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 + ); +}