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

"shouldExtend" assertion not applying to classes that do not have parent class #232

Closed
lorenzodalaqua opened this issue Aug 23, 2023 · 3 comments · Fixed by #233
Closed
Labels
bug Something isn't working

Comments

@lorenzodalaqua
Copy link

Question
Hi, I am unsure whether this is a bug or intended behavior, so I decided to post this as a question first.

I am planning to use PHPat for enforcing classes in certain namespaces in the application I am working on extend a common base class. (e.g. all controllers should extend BaseController, all requests should extend BaseRequest, etc)

The rule I am using is this, where $namespace is a namespace where we want to enforce a common base class for all classes, and $baseClass is the class which should be extended:

PHPat::rule()
    ->classes(Selector::namespace($namespace))
    ->excluding(Selector::classname($baseClass))
    ->shouldExtend()
    ->classes(Selector::classname($baseClass));

This works fine and PHPat correctly identifies an error for classes that extend an invalid base class, e.g. like the class below:

class InvalidRequest extends NotTheExpectedBaseRequest {}

But no errors are reported if we run PHPAt for classes with no extends statement such as this one:

class InvalidRequest {}

Now I am left wondering, is this the intended behavior, a bug, or have I missed something when configuring the rule for enforcing this? 🤔

Thanks in advance for your time! 😄

@carlosas
Copy link
Owner

carlosas commented Aug 23, 2023

This is definitely a bug 😱

I made a quick fix in the branch fix-should-empty that should be accessible via:

"require-dev": {
    "phpat/phpat": "dev-fix-should-empty",
...

plus composer update phpat/phpat

If you can check the fix against your code, it would be very helpful. Also I will double check tomorrow ASAP and release it.

Thanks for the report

@lorenzodalaqua
Copy link
Author

Hi, thanks for your quick response to this.

I tested using the fix-should-empty branch and errors are identified when the class has no parent now 👍

I will just wait for the release so I can upgrade the package then, thanks!

@carlosas
Copy link
Owner

Released in https://github.com/carlosas/phpat/releases/tag/0.10.8 🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants