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

Add ConstantsInTraitsRule #2718

Merged
merged 2 commits into from Nov 8, 2023

Conversation

paulbalandan
Copy link
Contributor

This PR adds a rule to flag usage of constants inside traits, which feature is only available on PHP 8.2 and later.

Ref: https://php.watch/versions/8.2/constants-in-traits

Copy link
Member

@ondrejmirtes ondrejmirtes left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, this really does not have to be a Collector+Rule, just a simple Rule.

@paulbalandan
Copy link
Contributor Author

Hi, this really does not have to be a Collector+Rule, just a simple Rule.

Initially, I tried using rule only targeting the ClassConst node but not getting any luck. Upon dumping the nodes, I found the node for traits is CollectedDataNode so I tried working on that direction. Did I miss anything?

@ondrejmirtes
Copy link
Member

Yes, ClassConst rule should be fine, please push your earlier attempt and I'll see what's wrong.

@ondrejmirtes
Copy link
Member

You need to use the trait by a class in order to have it inspected.

Alternatively (maybe a better option) is to hook your rule onto Trait_ node and inspect its stmts.

@ondrejmirtes
Copy link
Member

Looks pretty okay now. FYI there are dozens of similar rules that could be implemented that PHPStan does not check but PHP crashes on it. Would you be interested in looking at the list and maybe implementing some of them? Thank you!

@paulbalandan
Copy link
Contributor Author

Looks pretty okay now. FYI there are dozens of similar rules that could be implemented that PHPStan does not check but PHP crashes on it. Would you be interested in looking at the list and maybe implementing some of them? Thank you!

Sure. Maybe you have a list or something?

@paulbalandan
Copy link
Contributor Author

The larastan tests failures are not mine as I see from the larastan repo their build is failing. Not sure for phpstan-doctrine, though.

@ondrejmirtes ondrejmirtes merged commit 2cab7ec into phpstan:1.10.x Nov 8, 2023
68 checks passed
@ondrejmirtes
Copy link
Member

Thank you very much. The failures are not your fault, you certainly couldn't break that with a new rule :)

As for the other rule ideas. Here's one list:

And I also have a pretty comprehensive list in a private Trello board. If you give me your email address, I can invite you to it.

@paulbalandan paulbalandan deleted the constants-in-traits branch November 8, 2023 13:05
@paulbalandan
Copy link
Contributor Author

Thanks! Here's my email: paulbalandan[at]gmail.com

@ondrejmirtes
Copy link
Member

Just invited you, thanks!

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