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
fix switch(enum) #2687
fix switch(enum) #2687
Conversation
@@ -288,6 +288,7 @@ public function specifyTypesInCondition( | |||
($leftType->isString()->yes() && $rightType->isString()->yes()) | |||
|| ($integerType->isSuperTypeOf($leftType)->yes() && $integerType->isSuperTypeOf($rightType)->yes()) | |||
|| ($floatType->isSuperTypeOf($leftType)->yes() && $floatType->isSuperTypeOf($rightType)->yes()) | |||
|| ($leftType->isEnum()->yes() && $rightType->isEnum()->yes()) |
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.
Hi, I'm sorry I haven't reviewed this yet, but I saw this and have been thinking about it. I mostly like it but since the last time this code was touched, I introduced Type::getFiniteTypes()
which is for these situations - when getFiniteTypes return a single object.
It works for enums, scalars, constant arrays. I wonder if this condition could be simplified with this method.
I'm also worried about many of the failures with the memory limit, but maybe it's not fault of this PR.
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.
Hi, thanks for the feedback - I didn't know about getFiniteTypes
. However, I'm not sure that it is applicable here.
What I'm trying to express by this code is that $a == $b
is equivalent to $a === $b
, when both $a
and $b
are enums (actually it could be more general - https://3v4l.org/Huc7t). It doesn't matter whether it's an EnumCaseObjectType
or ObjectType
(maybe I'm just misunderstanding "when getFiniteTypes return a single object"). Furthermore, I want to avoid scalar types (e.g. 5 == "5"
).
I see that count($t->getFiniteTypes()) === 1
is already used inside resolveIdentical
. So my change does indirectly benefit from it.
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.
As for the memory limit: as far as I can see the memory limit is only exceeded for static analysis on PHP 7.2 (the other failures are due to a patch not applying on windows which I assume cannot be my fault). My guess is that it was borderline before and my adding a bit of code pushed it over the edge? I tried it with 500M and it worked: https://github.com/schlndh/phpstan-src/actions/runs/6603424059/job/17936514043
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.
You're right, it'd be bad for 5 == '5'
.
Thank you very much! |
Fixes https://phpstan.org/r/da3c1077-5bcd-4ed7-80da-f8ce588f788e
Also fixes phpstan/phpstan#3488 (comment) and phpstan/phpstan#9806