Fix phpstan/phpstan#9426: Variable always defined when needed#5450
Open
phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
Open
Fix phpstan/phpstan#9426: Variable always defined when needed#5450phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
Conversation
- When no typeGuards exist, recover type guards from ourExpressionTypes for entries removed by the first loop whose type differs from merged - Only create conditional expressions for variables whose certainty changed from YES to MAYBE (restricting to Variable nodes) - This handles isset()-correlated variable definedness without affecting other conditional expression creation paths - New regression test in tests/PHPStan/Rules/Variables/data/bug-9426.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
PHPStan incorrectly reports "Variable $b might not be defined" when
$bis assigned insideif (isset($a['something']))and later used in the false branch of!isset($a['something']) ? 'something' : $b. Since the false branch of!isset($a['something'])impliesisset($a['something'])is true,$bis guaranteed to be defined.Changes
MutatingScope::createConditionalExpressions()insrc/Analyser/MutatingScope.phpto handle the case where no regular typeGuards are found$newVariableTypeswhose type differs between branchesRoot cause
After
if (isset($a['something'])) { $b = ...; },createConditionalExpressions()is called duringmergeWith(). The variable$awas removed from$newVariableTypesby the first loop because the merged type (array{something?: string}) matched the else-branch type. Without$aas a type guard, no conditional expressions linking$a's narrowed type to$b's definedness were created. Later, when filtering the scope byisset($a['something'])in the ternary's false branch, the conditional expression matching loop found nothing to activate, so$bremained "maybe defined".The fix adds a targeted recovery path: when no typeGuards exist but there are variables whose certainty changed (YES in our branch, MAYBE in merged), it scans
ourExpressionTypesfor entries removed from$newVariableTypesthat have different types between branches. These become "recovered type guards" used only for creating conditional expressions about the certainty-changed variables.Test
Added regression test
tests/PHPStan/Rules/Variables/data/bug-9426.phpwith the exact code from the issue. The test verifies that no "Variable $b might not be defined" error is reported.Fixes phpstan/phpstan#9426