Skip to content

Fix phpstan/phpstan#9426: Variable always defined when needed#5450

Open
phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
phpstan-bot:create-pull-request/patch-esm15wf
Open

Fix phpstan/phpstan#9426: Variable always defined when needed#5450
phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
phpstan-bot:create-pull-request/patch-esm15wf

Conversation

@phpstan-bot
Copy link
Copy Markdown
Collaborator

Summary

PHPStan incorrectly reports "Variable $b might not be defined" when $b is assigned inside if (isset($a['something'])) and later used in the false branch of !isset($a['something']) ? 'something' : $b. Since the false branch of !isset($a['something']) implies isset($a['something']) is true, $b is guaranteed to be defined.

Changes

  • Modified MutatingScope::createConditionalExpressions() in src/Analyser/MutatingScope.php to handle the case where no regular typeGuards are found
  • Added a recovery mechanism that detects variables whose certainty changed from YES to MAYBE (indicating conditional definition) and finds type guards among entries removed from $newVariableTypes whose type differs between branches
  • Creates conditional expressions only for these certainty-changed variables, avoiding the broader "NOT defined" conditional creation that caused regressions

Root cause

After if (isset($a['something'])) { $b = ...; }, createConditionalExpressions() is called during mergeWith(). The variable $a was removed from $newVariableTypes by the first loop because the merged type (array{something?: string}) matched the else-branch type. Without $a as a type guard, no conditional expressions linking $a's narrowed type to $b's definedness were created. Later, when filtering the scope by isset($a['something']) in the ternary's false branch, the conditional expression matching loop found nothing to activate, so $b remained "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 ourExpressionTypes for entries removed from $newVariableTypes that 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.php with the exact code from the issue. The test verifies that no "Variable $b might not be defined" error is reported.

Fixes phpstan/phpstan#9426

- 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants