Fix phpstan/phpstan#7718: "Variable might not be defined" in second identical if block#5331
Open
phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
Open
Fix phpstan/phpstan#7718: "Variable might not be defined" in second identical if block#5331phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
if block#5331phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
Conversation
…ntical if block - In resolveEqual's general fallback, also track the condition expression itself via handleDefaultTruthyOrFalseyContext so it serves as a type guard during scope merging, enabling conditional expressions for variables defined under the condition - Only applied when both sides of the comparison are deterministic (non-impure) - Updated TypeSpecifierTest to expect the new condition expression tracking - Added regression test in tests/PHPStan/Rules/Variables/data/bug-7718.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
When two identical
ifstatements appear in sequence, and the first defines a variable, PHPStan incorrectly reports "Variable might not be defined" in the secondifblock. This happens specifically with==(loose comparison) where one side ismixed, because the TypeSpecifier doesn't narrow the sub-expression types, resulting in no type guards being created during scope merging.Changes
src/Analyser/TypeSpecifier.php: InresolveEqual's general fallback case, also track the condition expression itself viahandleDefaultTruthyOrFalseyContext. This narrows the comparison expression frombooltotrue/falsein the truthy/falsy scopes, creating a type guard that enables conditional expressions for variables defined under the condition.rand().tests/PHPStan/Analyser/TypeSpecifierTest.phpto expect the new condition expression in the specified types output.tests/PHPStan/Rules/Variables/data/bug-7718.phpregression test.testBug7718intests/PHPStan/Rules/Variables/DefinedVariableRuleTest.php.Root cause
After
if ($cond) { $var = 1; }, the truthy scope (where$varis defined) merges with the falsy scope (where it isn't), making$var"maybe defined". For the secondif ($cond)to restore$var's certainty, the merge must create conditional expressions linking$varto a type guard — an expression whose type differs between the truthy and falsy branches.With
===, the TypeSpecifier narrows$_GET['something']to'banana', creating a type difference that serves as a type guard. With==andmixed, no meaningful type narrowing occurs, so no type guard exists and no conditional expression is created.The fix tracks the condition expression itself (e.g.,
$_GET['something'] == 'banana') in the scope, narrowing it totruein the truthy branch andfalsein the falsy branch. This creates the type difference needed for the conditional expression mechanism to work.Test
Added
tests/PHPStan/Rules/Variables/data/bug-7718.phpwhich reproduces the original issue: two identicalif ($_GET['something'] == 'banana')blocks where the first defines$bananasand the second uses it. The test expects no errors (empty error array).Fixes phpstan/phpstan#7718