Fix phpstan/phpstan#8231: Missing type narrowing after (string)$value#5355
Open
phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
Open
Fix phpstan/phpstan#8231: Missing type narrowing after (string)$value#5355phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
Conversation
- Added handling in TypeSpecifier::resolveNormalizedIdentical for Cast\String_ expressions - When (string)$x === '' or (string)$x !== '', propagate narrowing to inner expression $x - Types that produce '' when cast to string (null, false, '') are used as the narrowing type - New regression test in tests/PHPStan/Analyser/nsrt/bug-8231.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 comparing a string cast expression with an empty string (e.g.,
(string)$x !== ''), PHPStan was not propagating the type narrowing back to the inner expression. This meant that$xof typestring|nullwas not narrowed tostring(ornon-empty-string) inside the condition branch, causing false positive errors.Changes
src/Analyser/TypeSpecifier.phpinresolveNormalizedIdentical()to handleCast\String_expressions(string)$expr === ''is encountered, the narrowing is propagated to$exprusing the union typenull|false|''(the types that produce''when cast to string)=== ''narrows to those types,!== ''removes themRoot cause
TypeSpecifier::resolveNormalizedIdentical()already handled unwrappingAlwaysRememberedExprto propagate type narrowing to inner expressions, but did not handleExpr\Cast\String_. When(string)$x !== ''was analysed, the cast expression(string)$xwas correctly narrowed tonon-empty-string, but the underlying variable$xretained its originalstring|nulltype because the narrowing was never propagated through the cast.Test
Added
tests/PHPStan/Analyser/nsrt/bug-8231.phpwith assertions covering:string|nullnarrowed tonon-empty-stringafter(string)$x !== ''string|nullnarrowed to''|nullafter(string)$x === ''int|nullnarrowed toint(removing null)int|string|nullnarrowed toint|non-empty-stringbool|string|nullnarrowed tonon-empty-string|trueFixes phpstan/phpstan#8231