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

Improved Enum support with regard to comparisons #7052

Closed
Seldaek opened this issue Apr 17, 2022 · 4 comments
Closed

Improved Enum support with regard to comparisons #7052

Seldaek opened this issue Apr 17, 2022 · 4 comments

Comments

@Seldaek
Copy link
Contributor

Seldaek commented Apr 17, 2022

Feature request

I am in the process of converting existing code to use enums in Monolog, and I noticed that PHPStan wasn't being as helpful as it could be.

See https://phpstan.org/r/920189df-c1f3-4b59-a816-02b8369fb16f for some expected errors which are missing.

See also https://3v4l.org/I2MUn#v8.1.5 for actual output.

The TL;DR is that when you have code like if ($foo < $bar) and then you convert it to enums and suddenly $foo and $bar are enum cases instead of int values, your code breaks but PHPStan does not tell you that this will now always return false.

@ondrejmirtes
Copy link
Member

Wow, check how easy this was :) phpstan/phpstan-src@f7d80d6

As for the other situations, one of them is covered by strict-rules (or enabling https://phpstan.org/config-reference#checkalwaystruestrictcomparison): https://phpstan.org/r/01b303cf-85b5-48fb-a3b7-06b814595682

The loose comparison == is a hands-off thing for PHPStan because it's really messy for different types (I don't want to code the famous "loose comparisons" table from https://www.php.net/manual/en/types.comparisons.php).

@ondrejmirtes
Copy link
Member

We should probably do the same thing for all objects, only some of them can be compared with < etc. like DateTimes or GMP.

@Seldaek
Copy link
Contributor Author

Seldaek commented Apr 19, 2022

Wow, check how easy this was :) phpstan/phpstan-src@f7d80d6

Cool, thanks!

As for the other situations, one of them is covered by strict-rules (or enabling https://phpstan.org/config-reference#checkalwaystruestrictcomparison): https://phpstan.org/r/01b303cf-85b5-48fb-a3b7-06b814595682

Ahh I didn't realize, will make sure to add strict rules on monolog repo 👍🏻

The loose comparison == is a hands-off thing for PHPStan because it's really messy for different types (I don't want to code the famous "loose comparisons" table from https://www.php.net/manual/en/types.comparisons.php).

I'd argue it should be doable for specific cases where the types are known and the same on left/right, but up to you. I added it mostly for completeness.

@github-actions
Copy link

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 21, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants