Fix narrowing to non-falsy-string when '' and '0' equality checks are in compound conditions#5688
Open
phpstan-bot wants to merge 1 commit into
Open
Conversation
When checking '' and '0' in a single || or && expression, the type narrowing to non-falsy-string was lost because the sequential removal of constant string types from the scope was lossy — removing '0' from 'string' cannot be represented, so the removal information was lost before '' could be removed (which would have enabled the '0' removal to succeed on the narrower non-empty-string type). Two fixes: 1. TypeCombinator::remove() now retries union type member removals in a loop, so that removing ''|'0' from string correctly yields non-falsy-string regardless of member ordering. 2. BooleanAndHandler/BooleanOrHandler now re-apply "lossy" sureNotType removals from the left arm after the right arm's narrowing succeeds, enabling the sequential per-arm path to also produce non-falsy-string. Closes phpstan/phpstan#9114
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 phpstan/phpstan#9114
When
''and'0'equality checks appear in a compound boolean condition (||or&&), PHPStan failed to narrow the variable tonon-falsy-string. This happened because removing'0'fromstringis "lossy" (cannot be represented in the type system), so by the time''removal narrowed the type tonon-empty-string, the'0'removal information was already lost.Changes
TypeCombinator::remove(): When removing a union type, retry members that failed in a loop until no more progress is made. This handles the case where removing''|'0'fromstringnow correctly producesnon-falsy-stringregardless of member ordering.BooleanAndHandler: After computing the truthy scope, re-apply left arm's sureNotType removals that were previously lossy but can now succeed on the narrower type from the right arm.BooleanOrHandler: Same logic for the falsey scope computation.Test cases
All variants from the issue are covered:
'' === $foo || '0' === $foofollowed by throw →non-falsy-string'0' === $foo || '' === $foofollowed by throw →non-falsy-string'' !== $foo && '0' !== $foo→non-falsy-stringin truthy branch'0' !== $foo && '' !== $foo→non-falsy-stringin truthy branchTest plan
tests/PHPStan/Analyser/nsrt/bug-9114.phppassesmake phpstanself-analysis passes with no errors