-
Notifications
You must be signed in to change notification settings - Fork 665
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
improve support for enum_exists #7404
Conversation
// We're using class_exists here because enum_exists doesn't exist on old versions of PHP | ||
// Not sure what happens if we try to autoload or reflect on an enum on an old version of PHP though... | ||
if ($string_value && class_exists($string_value)) { | ||
$reflection_class = new ReflectionClass($string_value); |
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.
Given a ReflectionClass
in constructed anyway why not check (in an php <= 8.1 safe way) if it is an actual enum using:
array_search ('UnitEnum', $c->getInterfaceNames(), true);
As per https://www.php.net/manual/en/class.unitenum.php all Enums implement the UnitEnum
interface
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.
Well, it will only work if PHP was able to autoload the file and parse it and detect it is indeed an Enum. Which mean it will only work on PHP >8.1. At this point, we might as well use enum_exists as long as we checked we're in 8.1.
Thinking about it however, I don't see a point preventing scanning of a possible class that would have been pushed in an enum_exists. Either it exists and it's useful and it will end up scanned at one point or it's useless and it's possible Psalm will actually report it unused
Anyway, the real issue is for older versions of PHP, I have no idea what will happen if we run Psalm on 8.0 and try to analyze an enum. It will probably crash? But I don't think I can prevent it
I'll put this in draft. I'll probably start this again from scratch on master to avoid conflicts with #7410 |
Hi @orklah anything I can to to assist either with this PR (#7404) or with #7410 ? |
#7410 is a pretty complex refactoring of a pretty complex module of Psalm. That's why I don't want to get in the way because I can't wait to see it happen :) This PR in comparison is simple and I'm not worried I'll be able to do it again quickly once #4710 is merged. You should be able to test Psalm with this PR if you want, it should work OK and if you detect bugs, I can fix them. This will be added to Psalm 5 that should came out in a few weeks |
This is not blocked anymore, I'll work on it tomorrow :) |
@DannyvdSluijs it should be ok on master now |
I've checked it and it seems to be working very nicely, thanks for your efforts. To summarise the results:
|
the ParserError comes from our dependency (PHP-Parser) when it encounters a syntax it doesn't recognize for a given php version. The rest of the errors is the effect of the first because Psalm couldn't see there is an enum here. Those errors are indeed expected to happen on 7.4 :) Great to know it works for you! |
This introduces:
This is built upon the work of @DannyvdSluijs started here: #6431 (comment)
This should fix #6431 but I'm pretty sure there are still some support missing, it will have to be reported in the future so we can know there are issues