-
Notifications
You must be signed in to change notification settings - Fork 534
Support type narrowing with casted parameters #2186
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
Conversation
5ce5fce
to
c11e12f
Compare
c11e12f
to
8f54f75
Compare
8f54f75
to
e3ce1ee
Compare
$unionType = TypeCombinator::union(...$types); | ||
$specifiedTypes = $this->typeSpecifier->create($exprArg, $unionType, $context, false, $scope); | ||
|
||
if ($context->true() && $exprArg instanceof Cast) { |
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.
my initial attempt was to do this directly within the TypeSpecifier
... but I then got the feeling that the type narrowing of a cast-expression is not a concept which can be applied to all specifying extensions.
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.
Yeah, well, is_string((string) $int)
is always true, but it doesn't make $int
a string :D
This pull request has been marked as ready for review. |
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.
Also please test the negative context.
$castOriginType = TypeCombinator::intersect($scope->getType($exprArg->expr), $unionType); | ||
|
||
$specifiedTypes = $specifiedTypes->unionWith( | ||
$this->typeSpecifier->create($exprArg->expr, $castOriginType, $context, false, $scope) |
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.
Are you sure you can't just use $unionType
instead of $castOriginType
here in the 2nd arg?
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.
good point. fixed.
Thank you. |
1 similar comment
Thank you. |
closes phpstan/phpstan#8714
https://3v4l.org/LPe72