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 HaveMethod expression #226

Open
pfazzi opened this issue Jan 7, 2022 · 6 comments
Open

Add HaveMethod expression #226

pfazzi opened this issue Jan 7, 2022 · 6 comments

Comments

@pfazzi
Copy link
Collaborator

pfazzi commented Jan 7, 2022

Feature Request

Q A
New Feature yes
RFC no
BC Break no

Summary

I would like to enforce this constraint:

Rule::allClasses()
        ->that(new HaveNameMatching('App\*\Command\*Handler'))
        ->should(new HaveMethod('__invoke'))
        ->because('command handlers should be invokable');

ATM we don't have any mechanism to check stuff on class implementation, isn't it?

@micheleorselli
Copy link
Contributor

I think this is a useful addition. Would you elaborate a bit more about how is it supposed to work? In particular:

  • should the class directly implement the method or it can also inherit it?
  • should pattern matching work?

@pfazzi
Copy link
Collaborator Author

pfazzi commented Jan 8, 2022

Regarding your questions:

  • Class can implement or inherit the method.
  • Patter matching could be useful, someone could enforce rules like "Repository should have findBy* methods"; even I dislike it, there are frameworks that work this way.

@micheleorselli
Copy link
Contributor

micheleorselli commented Jan 8, 2022

I am not sure we capture inherited methods in ClassDescription objects but we can probably discuss the implementation later

For consistency we should also have a NotHaveMethod

Good to go for me

@AlessandroMinoccheri
Copy link
Member

At the moment we don't have a mechanism to check stuff on class implementation because we were focused on rules for dependencies.
We can try to understand if now it's time to develop something to create rules for methods.

@diimpp
Copy link

diimpp commented Sep 13, 2022

That's a good point with __invoke for command handlers, but isn't self validated at runtime? It sounds more of unit test territory than architect decision.

As for generic methods, shouldn't developer cover this with interfaces? Maybe only magic method expression?

@fragkp
Copy link

fragkp commented Nov 7, 2022

Would love to see this feature coming!

One of my use case would be to check for "valid" method names on a controller. Valid according to this list of actions: https://laravel.com/docs/9.x/controllers#actions-handled-by-resource-controller

Example:

Rule::allClasses()
        ->that(new ResideInOneOfTheseNamespaces('App\Http\Controllers'))
        ->should(new HaveMethod(['index', 'create', 'store', 'show'...]))
        ->because('something');

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

No branches or pull requests

5 participants