Add regression test for @var annotation before foreach not defining the expression variable#5458
Merged
VincentLanglet merged 1 commit intophpstan:2.1.xfrom Apr 14, 2026
Conversation
…ng the expression variable - Add regression test for phpstan/phpstan#6688 verifying that `@var` PHPDoc annotations before `foreach` correctly define the expression variable in scope - Test covers the original issue: `/** @var string[][] $result */ foreach ($result as $data)` and the maintainer's reproduction: `/** @var string[] $c */ foreach ($c as $v)` - The fix was applied in commit 4a08845 by moving the `processVarAnnotation` call before `processExprNode` in the `Foreach_` branch of `NodeScopeResolver` - Investigated analogous constructs (if, while, do-while, for, switch, match): all are correctly handled by the general `processStmtVarAnnotation` call at the top of `processStmtNode` — only `Foreach_` was excluded from that general handling and needed its own pre-expression annotation processing
VincentLanglet
approved these changes
Apr 14, 2026
staabm
approved these changes
Apr 14, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Issue phpstan/phpstan#6688 reported a false positive "Variable $result might not be defined" when a
@varPHPDoc annotation was placed before aforeachloop to define the expression variable. This was fixed in commit 4a08845 but no regression test was added. This PR adds the regression test.Changes
tests/PHPStan/Rules/Variables/data/bug-6688.phpreproducing both the original issue and the maintainer's confirmation casetestBug6688()method intests/PHPStan/Rules/Variables/DefinedVariableRuleTest.phpRoot cause
The
Foreach_statement was explicitly excluded from the generalprocessStmtVarAnnotationcall at the top ofNodeScopeResolver::processStmtNode()(line 507). This exclusion exists because foreach has its own@varannotation handling for iteration variables viaenterForeach(). However, theprocessVarAnnotationcall for the foreach expression variable was positioned AFTERprocessExprNode, meaning the variable was evaluated before the@varannotation could define it. The fix (commit 4a08845) moved this call to before the expression processing.Other control flow statements (
if,while,do-while,for,switch,match) are NOT affected because they are handled by the generalprocessStmtVarAnnotationcall which is not excluded for them.Test
The regression test verifies that:
/** @var string[] $c */ foreach ($c as $v)produces no "variable undefined" error (the@varannotation defines$c)/** @var string[][] $result */ foreach ($result as $data)produces no error (the original issue)echo $a) still correctly reports "Variable $a might not be defined"Confirmed the test fails when the fix is reverted (removing lines 1222-1224 in
NodeScopeResolver.php).Fixes phpstan/phpstan#6688