Fix phpstan/phpstan#14227: "Variable might not be defined" false positive#5129
Open
phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
Open
Fix phpstan/phpstan#14227: "Variable might not be defined" false positive#5129phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
Conversation
…ve with unset in mutually exclusive branch - Added preserveSafeConditionalExpressions() to MutatingScope to preserve conditional expressions through merges when one branch invalidates a variable (e.g. via unset()) but the guard condition is disjoint from the other branch's types - This also fixes related false positives for variables defined in different branches of if/elseif/else (bug-4173, dynamic-access test improvements) - Restricted preservation to simple Variable expressions to avoid stale method call narrowing - New regression test in tests/PHPStan/Rules/Variables/data/bug-14227.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
Fixes a false positive "Variable $value might not be defined" when a variable is assigned under one condition and
unset()under a mutually exclusive condition. This was found while reviewing #5125.Changes
preserveSafeConditionalExpressions()method tosrc/Analyser/MutatingScope.phpthat preserves conditional expressions through scope merges when one branch invalidates a variable but the guard condition is disjoint from the other branch's typesmergeWith()for both scope sides, betweenintersectConditionalExpressions()andcreateConditionalExpressions()Variableexpressions to avoid stale method call narrowing issuestests/PHPStan/Rules/Variables/DefinedVariableRuleTest.php:testBug14227()regression testtestDynamicAccess()expectations (resolved false positives for variables in mutually exclusive branches)testBug4173()expectations (resolved a known false positive marked "could be fixed")tests/PHPStan/Rules/Variables/data/bug-14227.phpwith both the fix case and a counterexampleRoot cause
When
unset($value)was called inside an if-block,invalidateExpression()removed both the variable and its conditional expressions from the truthy scope. When merging back with the falsey scope,intersectConditionalExpressions()dropped conditional expressions that existed only in the falsey scope (because the truthy scope no longer had them). This caused the information "when $key === 1, $value is defined" to be lost.The fix preserves these conditional expressions when:
Test
Added
tests/PHPStan/Rules/Variables/data/bug-14227.phpwith:foo(): the reported case whereunset()in a mutually exclusive branch should not cause a false positive (no errors expected)bar(): a counterexample whereunset()runs under a compatible condition, so the error should still be reportedFixes phpstan/phpstan#14227