Skip to content
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

Merged
merged 1 commit into from Oct 22, 2023
Merged

fix switch(enum) #2687

merged 1 commit into from Oct 22, 2023

Conversation

schlndh
Copy link
Contributor

@schlndh schlndh commented Oct 20, 2023

@@ -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())
Copy link
Member

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.

Copy link
Contributor Author

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.

Copy link
Contributor Author

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

Copy link
Member

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'.

@ondrejmirtes ondrejmirtes merged commit cb91818 into phpstan:1.10.x Oct 22, 2023
400 of 418 checks passed
@ondrejmirtes
Copy link
Member

Thank you very much!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants