From b72ad8fc510f1cbe1aa81e809af71226399ce396 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Mon, 30 Mar 2026 23:59:02 +0000 Subject: [PATCH] Fix phpstan/phpstan#11181: Allow bare yield after return for empty generators - Skip bare `yield;` (no key, no value) in getNextUnreachableStatements - This is a valid PHP idiom (`return; yield;`) to create an empty generator - Added regression test in tests/PHPStan/Rules/DeadCode/data/bug-11181.php --- src/Analyser/NodeScopeResolver.php | 8 +++++++ .../DeadCode/UnreachableStatementRuleTest.php | 6 ++++++ .../PHPStan/Rules/DeadCode/data/bug-11181.php | 21 +++++++++++++++++++ 3 files changed, 35 insertions(+) create mode 100644 tests/PHPStan/Rules/DeadCode/data/bug-11181.php diff --git a/src/Analyser/NodeScopeResolver.php b/src/Analyser/NodeScopeResolver.php index 001c8b5b36..126932e2f6 100644 --- a/src/Analyser/NodeScopeResolver.php +++ b/src/Analyser/NodeScopeResolver.php @@ -4442,6 +4442,14 @@ private function getNextUnreachableStatements(array $nodes, bool $earlyBinding): if ($node instanceof Node\Stmt\Nop || $node instanceof Node\Stmt\InlineHTML) { continue; } + if ( + $node instanceof Node\Stmt\Expression + && $node->expr instanceof Expr\Yield_ + && $node->expr->key === null + && $node->expr->value === null + ) { + continue; + } if (!$node instanceof Node\Stmt) { continue; } diff --git a/tests/PHPStan/Rules/DeadCode/UnreachableStatementRuleTest.php b/tests/PHPStan/Rules/DeadCode/UnreachableStatementRuleTest.php index d191658d0e..b3ce494b6f 100644 --- a/tests/PHPStan/Rules/DeadCode/UnreachableStatementRuleTest.php +++ b/tests/PHPStan/Rules/DeadCode/UnreachableStatementRuleTest.php @@ -397,6 +397,12 @@ public function testBug14328(): void ]); } + public function testBug11181(): void + { + $this->treatPhpDocTypesAsCertain = true; + $this->analyse([__DIR__ . '/data/bug-11181.php'], []); + } + public function testBug14369(): void { $this->treatPhpDocTypesAsCertain = true; diff --git a/tests/PHPStan/Rules/DeadCode/data/bug-11181.php b/tests/PHPStan/Rules/DeadCode/data/bug-11181.php new file mode 100644 index 0000000000..94c69ceb94 --- /dev/null +++ b/tests/PHPStan/Rules/DeadCode/data/bug-11181.php @@ -0,0 +1,21 @@ +