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

feat: add ShouldHaveOnlyOnePublicMethod rule #237

Merged
merged 3 commits into from
Oct 5, 2023
Merged

feat: add ShouldHaveOnlyOnePublicMethod rule #237

merged 3 commits into from
Oct 5, 2023

Conversation

rgomezcasas
Copy link
Contributor

@rgomezcasas rgomezcasas commented Oct 2, 2023

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.

Copy link
Owner

@carlosas carlosas left a 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 use getNativeReflection (see ClassAttributeExtractor)
  • 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 :)

@rgomezcasas
Copy link
Contributor Author

rgomezcasas commented Oct 3, 2023

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 ReflectionMethod, but I did a guarradita. 😳

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

@carlosas
Copy link
Owner

carlosas commented Oct 3, 2023

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 \ReflectionMethod constant is legit, I will allow native classes as dependency there.

@rgomezcasas
Copy link
Contributor Author

Awesome!! 🙌

@carlosas carlosas merged commit 37bcb35 into carlosas:master Oct 5, 2023
9 of 12 checks passed
@carlosas
Copy link
Owner

carlosas commented Oct 5, 2023

Released with v0.10.10 🚀

@rgomezcasas
Copy link
Contributor Author

Graaaaandeeee!!! 🔝

In two weeks we'll publish the YT video! Already recorded. 🙂

@rgomezcasas rgomezcasas deleted the add-has_one_public_method branch October 6, 2023 06:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants