-
Notifications
You must be signed in to change notification settings - Fork 574
Fix phpstan/phpstan#7858: Redundant type casting drops next expression from condition and switches the context #5080
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
phpstan-bot
wants to merge
8
commits into
phpstan:2.1.x
from
phpstan-bot:create-pull-request/patch-ybui14l
Closed
Changes from 3 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
ce34267
Fix redundant type cast dropping type narrowing in comparisons
github-actions[bot] d60cc3d
Update bug-7858.php
staabm b769b67
Simplify cast unwrapping in comparisons to always unwrap
phpstan-bot 93e0938
Update bug-7858.php
staabm a1b75be
Update bug-7858.php
staabm 7f37e2f
Update bug-7858.php
staabm 6f5cbae
Only unwrap cast in comparisons when it is redundant
phpstan-bot 1fa9600
Improve type narrowing for partially redundant casts in comparisons
phpstan-bot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| <?php declare(strict_types = 1); | ||
|
|
||
| namespace Bug7858; | ||
|
|
||
| use function PHPStan\Testing\assertType; | ||
|
|
||
| function foo(int $year): void | ||
| { | ||
| if (!ctype_digit($year) || (int) $year < 2022) { | ||
| throw new \RuntimeException(); | ||
| } | ||
| assertType('int<2022, max>', $year); | ||
| } | ||
|
|
||
| function bar(int $year): void | ||
| { | ||
| if (!ctype_digit($year) || $year < 2022) { | ||
| throw new \RuntimeException(); | ||
| } | ||
| assertType('int<2022, max>', $year); | ||
| } | ||
|
|
||
| function baz($year): void | ||
| { | ||
| if (!ctype_digit($year) || (int)$year < 2022) { | ||
| throw new \RuntimeException(); | ||
| } | ||
| assertType('int<2022, max>|numeric-string', $year); | ||
| } | ||
|
|
||
| function bam(int|string $year): void | ||
| { | ||
| if (!ctype_digit($year) || (int)$year < 2022) { | ||
| throw new \RuntimeException(); | ||
| } | ||
| assertType('int<2022, max>|numeric-string', $year); | ||
| } | ||
|
|
||
| function ban(string $year): void | ||
| { | ||
| if (!ctype_digit($year) || (int)$year < 2022) { | ||
| throw new \RuntimeException(); | ||
| } | ||
| assertType('numeric-string', $year); | ||
| } |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems to unwrap the cast unconditionnally and not only when the cast is redundant.
Wouldn't this introducing regression for non redundant cast ?
Add an assertType check on
(int) $yearin all the written example.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pushed. Here's a summary of the changes:
TypeSpecifier.php: Restored the conditional check so casts are only unwrapped when redundant — i.e., when
$scope->getType($castExpr)->equals($scope->getType($castExpr->expr)). This prevents incorrect type narrowing when the cast is meaningful (e.g.,(int)on a string variable would have incorrectly applied integer range narrowing to the string).bug-7858.php test updates:
assertTypefor(int) $year/(int) $mixedin all test functions — all correctly inferint<2022, max>(the cast expression itself gets narrowed)foo(int $year):$yearstill correctly narrows toint<2022, max>(cast is redundant, unwrapped)baz($year)andbam(int|string $year):$yeartype updated toint<48, 57>|int<256, max>|numeric-string(cast is non-redundant, not unwrapped, so the comparison narrowing applies to the cast expression rather than the variable)ban(string $year): unchanged —numeric-string(cast non-redundant, string not narrowed by int comparison)bak($mixed):$mixedtype updated tofloat|int|numeric-string(cast non-redundant)All 11,904 tests pass and PHPStan reports no errors.