-
-
Notifications
You must be signed in to change notification settings - Fork 57
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
Generic/UselessOverridingMethod: small improvements to the sniff code #366
Generic/UselessOverridingMethod: small improvements to the sniff code #366
Conversation
@rodrigoprimo I haven't looked at the code yet, but thought I'd respond to this comment straight away. Have you considered nested function declarations ? i.e. https://3v4l.org/f34jJ
|
bc8279f
to
c998827
Compare
Thanks for your questions, @jrfnl.
Good point. I haven't considered nested functions when working on this. I did a few tests and modified the original commit to also bail early when it finds a nested function, but now I'm worried that the condition that I introduced might caught more than just nested functions, and I might be introducing a bug. Is there another way to differentiate methods and functions? Maybe the risks of the change that I'm introducing are not worth the benefits? I am happy to follow your advice on this as you have way more experience than I do.
Sorry, my original message could have been more clear. What I meant to say is that a method in an interface, enum, or trait cannot call a parent method as far as I could test. That being said, I can see that I made a mistake when testing traits and that a trait method can actually call a parent method. Thanks for pointing that out. I have updated the original commit to not bail early for traits, and I included some tests covering trait methods. |
19b397d
to
d4ebdc7
Compare
@jrfnl, I made some changes to the code that checks if a given function should be checked by the sniff or not after our conversation yesterday, and now this PR is ready for review. Thanks again for your help. |
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.
Hi @rodrigoprimo, thanks for working on this. Looking good! Just needs some dotting of the i's and crossing of the t's.
P.S.: Sorry, I made a small edit in the PR description as the quoted text didn't line up with the comment in the other PR (which was confusing me).
src/Standards/Generic/Sniffs/CodeAnalysis/UselessOverridingMethodSniff.php
Show resolved
Hide resolved
src/Standards/Generic/Sniffs/CodeAnalysis/UselessOverridingMethodSniff.php
Outdated
Show resolved
Hide resolved
src/Standards/Generic/Tests/CodeAnalysis/UselessOverridingMethodUnitTest.1.inc
Show resolved
Hide resolved
src/Standards/Generic/Sniffs/CodeAnalysis/UselessOverridingMethodSniff.php
Outdated
Show resolved
Hide resolved
src/Standards/Generic/Tests/CodeAnalysis/UselessOverridingMethodUnitTest.1.inc
Outdated
Show resolved
Hide resolved
src/Standards/Generic/Tests/CodeAnalysis/UselessOverridingMethodUnitTest.1.inc
Outdated
Show resolved
Hide resolved
src/Standards/Generic/Tests/CodeAnalysis/UselessOverridingMethodUnitTest.1.inc
Show resolved
Hide resolved
src/Standards/Generic/Tests/CodeAnalysis/UselessOverridingMethodUnitTest.1.inc
Outdated
Show resolved
Hide resolved
@rodrigoprimo No need. The way you've done it now with atomic commits for each change is exactly how I like it and allows for reviewing each change individually 👍🏻 |
fd1083e
to
6d1eb17
Compare
@jrfnl, thanks for all your comments. This PR is ready for another review. |
src/Standards/Generic/Sniffs/CodeAnalysis/UselessOverridingMethodSniff.php
Outdated
Show resolved
Hide resolved
f81f12f
to
d11e893
Compare
This PR is ready for another review. |
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.
Thanks @rodrigoprimo ! This looks good to me now. Nice clean up and improvements for this sniff.
I will rebase the PR interactively to get back to the original atomic commits and then merge the PR once the build has passed.
…d names Method names in PHP are case insensitive for ASCII characters. This commit changes the sniff to compare the name of the declared method with the name of the called method in a case-insensitive manner for the ASCII characters. The sniff will now flag methods as useless when PHP would consider the called method the same as the declared method, even when the called method name is in a different case as the name used in the function declaration. Includes unit tests.
This commit improves how the sniff handles code that is missing the closing parenthesis in the parent method call. Now, the code will intentionally bail early in such cases. Before, the sniff would unintentionally bail when such cases were found at a later point and would incorrectly consider everything after the opening parenthesis to be the first parameter of the parent method. The commit includes a test.
This commit simply replaces a call to `count($token)` with `$phpcsFile->numTokens`.
Early on in the sniff code, it is checked if the method has a scope opener (which is never true if there is no scope closer). So there always will be at least one token after the scope opener, the scope closer. This means that `$next === false` checks are not necessary. `$next` will never be false. To be extra careful, a check to confirm that the scope closer is present was added to the beginning of the sniff.
The UselessOverridingMethod is triggered when the T_FUNCTION token is found. This token is used for regular functions and also methods. The sniff should run only for class and trait methods (including abstract and anonymous classes). Those are the only methods that can have a parent. So this commit changes the sniff to bail early when dealing with a regular functions, nested functions, interfaces methods and enum methods.
d11e893
to
7071004
Compare
Description
This PR implements the following improvements to the Generic.CodeAnalysis.UselessOverridingMethod sniff, which were suggested by @jrfnl in this comment in another PR:
Item number 10 was implemented in two commits. One for removing the redundantAs for handling non-OO function declarations, I changed the sniff code to bail early when the T_FUNCTION does not correspond to a class or anonymous class method. I used$next === false
condition and another to handle non-OO function declarations.$phpcsFile->hasCondition()
for that. This is the first time that I'm using this method, please let me know if you see a problem with this approach or if there is something that I missed. My assumption is that functions and also trait, interface, and enum methods cannot have a parent so the sniff can bail early in those cases.Please let me know if you prefer that I split all those fixes into multiple PRs.
Suggested changelog entry
Generic/UselessOverridingMethod: account for method and parent methods with the same name but different case
I'm unsure if the other changes should be mentioned in the changelog or not. Maybe a second entry referencing "minor performance improvements" (which is most of the other changes but not all other changes)?
Related issues/external references
Discussed in #250 (review)
Types of changes
PR checklist