-
Notifications
You must be signed in to change notification settings - Fork 42
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
feat: add ShouldHaveOnlyOnePublicMethod rule #237
Conversation
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.
Hello Mr Rafita, thank you for the pull request!
This assertion is definitely something I wanted to address at some point. Right on target 🙌
There are a couple of things that need some fixing:
- the assertion should be final
- the reflection in the extractor should be retrieved through
$node->getClassReflection()
, which returns the BetterReflection wrapper. And if the wrapper is not enough, you can then usegetNativeReflection
(seeClassAttributeExtractor
) - Also PHP-CS-Fixer needs a ride (I just realized that the step is not reporting as failure, I will change it)
Regarding functionality:
- is the constructor being taken into account? should the target class be allowed to have public constructor plus one public method?
- in the future, the assertion can be made more configurable (like
shouldHaveNPublicMethods(int)
) but I guess the most common use case is one. I will probably add some extra assertions in the future going that way. This one is good enough 👍
Thank you again. Let me know if you have any doubt or concern :)
All changes made! Good catch on the constructor one. 🙂 It was done with the intention of having only one public method, but feel free to change it as you consider better! Also, to get the public methods we have to depend on Thanks!! 🙌 P.S. We're going to record a course about static analysis in PHP, and this library will be showcased as a way to enforce good architecture. We'll also upload a YouTube video about it. 🙂 |
Thanks! Psalm is complaining because it doesn't understand the tricks BetterReflection does to be compatible with PHP 7 and 8, but it works well in reality. I will merge, fix the CI config problems, and release a new version during this week :D Thanks for the contribution, and happy to hear that PHPat is going to be part of one of your amazing courses 🙌 PS: the usage of the |
Awesome!! 🙌 |
Released with v0.10.10 🚀 |
Graaaaandeeee!!! 🔝 In two weeks we'll publish the YT video! Already recorded. 🙂 |
Useful for ensuring that Application Services, Controllers… have only one public method in order to enforce SRP. 🙂
I've tried to follow all the code practices in the repo, feel free to make any changes.
PHPMD offers this feature as well, but imho, it has more sense as an architectural layer.