Fix types after ctype_digit() with casted parameter#2189
Fix types after ctype_digit() with casted parameter#2189ondrejmirtes merged 2 commits intophpstan:1.9.xfrom
Conversation
ondrejmirtes
left a comment
There was a problem hiding this comment.
Can you please explain the changes and your findings since the last PR?
|
|
||
| if (ctype_digit((int) $mixed)) { | ||
| assertType('int<48, 57>|int<256, max>|numeric-string', $mixed); | ||
| assertType('mixed', $mixed); // could be *NEVER* |
There was a problem hiding this comment.
as can be seen in https://3v4l.org/k3Qce casting the parameter for ctype-digit to int beforehand does not yield a usefull result at all.
therefore I adjusted the extension to only narrow on (string) call.
I think its not worth to add logic into the extension which turns all "bad uses" of casted parameters into a NeverType.
There was a problem hiding this comment.
phpstan/phpstan#8736 is about (string) cast, not (int) cast, so please explain what changes for (string) that makes 1.9.13 not work.
There was a problem hiding this comment.
in 1.9.13 we assumed a parameter type before casting would be the same as the one the ctype_digit function narrows.
this is not entirely correct. you can actually pass in e.g. a 1 which casted to string is a numeric-string, but is not contained within the
type.
what we actually need is a type which represents all values which after casted with (string) will lead in a numeric-string. thats also the reason why e.g. bool is narrowed to true
| $specifiedTypes = $this->typeSpecifier->create($exprArg, $unionType, $context, false, $scope); | ||
|
|
||
| if ($context->true() && $exprArg instanceof Cast) { | ||
| if ($context->true() && $exprArg instanceof Cast\String_) { |
There was a problem hiding this comment.
with this change, I just turn the narrowing for non-string casts back to what it was before the recent PR #2186
|
This pull request has been marked as ready for review. |
|
This pull request has been marked as ready for review. |
|
Thank you. |
closes phpstan/phpstan#8736