Fix phpstan/phpstan#14353: Falsy "Variable might not be defined"#5280
Open
phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
Open
Fix phpstan/phpstan#14353: Falsy "Variable might not be defined"#5280phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
Conversation
…r foreach - After a foreach that conditionally defines a variable, conditional expressions are created tracking "if array is empty, variable doesn't exist" - When isset() confirms the variable exists, specifyExpressionType sets certainty to Yes but stale conditional expressions with No certainty persist - A subsequent foreach over the same array triggers filterByTruthyValue for the "array is empty" case, activating the stale conditional and removing the variable - Fix: in specifyExpressionType, when certainty is Yes, remove conditional expressions for the variable that have No certainty (would unset it) - New regression test in tests/PHPStan/Rules/Variables/data/bug-14353.php
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
False positive "Variable $report_ids might not be defined" reported inside an
isset()block after an unrelatedforeachloop over the same array that was used to conditionally define the variable.Changes
src/Analyser/MutatingScope.phpinspecifyExpressionType(): when certainty is set to Yes, remove conditional expressions for that variable where the type holder has No certainty (i.e., "variable doesn't exist" conditions)tests/PHPStan/Rules/Variables/data/bug-14353.phptestBug14353intests/PHPStan/Rules/Variables/DefinedVariableRuleTest.phpRoot cause
After a foreach like
foreach ($reports as $v) { $report_ids[$v] = 1; }, the scope merge creates conditional expressions tracking the relationship: "if$reportsis empty (the loop didn't execute), then$report_idsdoesn't exist."When
isset($report_ids)later confirms the variable exists,specifyExpressionTypesets certainty to Yes but does not clean up these stale conditional expressions. A subsequentforeach ($reports as $v) {}triggersfilterByTruthyValuefor the "array is empty" case, which activates the stale conditional expression and removes$report_idsfrom the scope. The merge then downgrades$report_idscertainty from Yes to Maybe, causing the false positive.The fix removes No-certainty conditional expressions for a variable when its certainty is explicitly set to Yes, since confirming a variable exists invalidates any conditional that says it doesn't.
Test
Added a rule test reproducing the exact scenario from the issue: a variable defined inside both branches of an if/else (each containing a foreach), checked with
isset(), then accessed after an unrelated foreach loop. The test expects no errors.Fixes phpstan/phpstan#14353