Skip to content

Fix while-true-if-break: Variable might not be defined#5118

Merged
VincentLanglet merged 6 commits intophpstan:2.1.xfrom
VincentLanglet:fix/9023
Mar 3, 2026
Merged

Fix while-true-if-break: Variable might not be defined#5118
VincentLanglet merged 6 commits intophpstan:2.1.xfrom
VincentLanglet:fix/9023

Conversation

@VincentLanglet
Copy link
Contributor

@VincentLanglet VincentLanglet commented Mar 1, 2026

$this->checkMaybeUndefinedVariables = true;
$this->polluteScopeWithAlwaysIterableForeach = true;

$this->analyse([__DIR__ . '/data/bug-11545.php'], []);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This currently does not work because of
https://phpstan.org/r/cd248edd-5252-4bba-ada5-26cce593585b

phpstan-bot and others added 6 commits March 2, 2026 21:17
- In while(true) loops, the only way to exit is via break, so the post-loop scope should be constructed solely from break exit point scopes
- Previously, break scopes were merged with the "normal exit" scope (filterByFalseyValue), which degraded variable certainty to "maybe" for variables only defined in the break path
- New regression test in tests/PHPStan/Rules/Variables/data/bug-9023.php

Closes phpstan/phpstan#9023
@VincentLanglet VincentLanglet marked this pull request as ready for review March 2, 2026 20:18
@phpstan-bot
Copy link
Collaborator

This pull request has been marked as ready for review.

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.

3 participants