-
-
Notifications
You must be signed in to change notification settings - Fork 66
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: improve code coverage #250
Generic/UselessOverridingMethod: improve code coverage #250
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.
@rodrigoprimo Thanks for working on this. My apologies it took me a while before reviewing this PR.
This PR review is a mix of a review of the changes you've made in combination with a critical look at the sniff and what (other) tests are missing, which brings to light some bugs.
So after reviewing, these are my findings:
- I believe the "skip function without body" block is now covered via a parse error code sample in test case file 2.
What about the following non-parse error situations, which would also hit that code block and are currently not covered by tests ?- an abstract method declaration.
- an interface method declaration.
- The "Any token except 'parent'" block is now covered via the
ignoreNoParent()
code snippet.
What about adding an additional test with valid code, but without a return statement ? (void method) - The "Skip for ... other method." block is now covered via the
differentParentMethod()
code snippet.
I can see a bug in the condition though - function/method names in PHP are case-insensitive.
There is currently no test where the function declaration name is not in the same case as the name used in the function call.
Such a test should be added and the code should be flagged as potentially useless override, which would need a small fix in the condition of that code block.
This would be a functional change and could be made in a separate commit in this PR or via a separate PR. - I'm missing a test where the function call has an open parenthesis, but no close parenthesis.
Adding this test would highlight that some extra defensive coding would be helpful.
The "Skip for invalid code." block, which checks for the open parenthesis, should probably also check for a matching close parenthesis viaisset($tokens[$next]['parenthesis_closer']) !== false
.
The redundant function call on line 118 -count($tokens)
- can then be also removed and the$count
in thefor
condition replaced. (That function call was redundant anyhow and should have used$phpcsFile->numTokens
instead)
This would be a functional change and could be made in a separate commit in this PR or via a separate PR. - I'm missing a test with a call to
parent::methodName()
and something else between that and the semicolon.
Something like:return parent::methodName($a, $b) + $b;
- I'm missing a test with a function call with a trailing comma as allowed since PHP 7.3.
Looks like the sniff would handle this correctly due to thearray_filter()
, but it should still be tested. - I'm missing a test with a call to
parent::methodName()
using the same parameters as in the method signature, but in a different order.
Looks like the sniff would handle this correctly, but I think it should still be covered by a test. - I'm missing a test with a call to
parent::methodName()
with the same number of parameters, but different names.
Looks like the sniff would handle this correctly, but I think it should still be covered by a test.
Something likereturn parent::methodName($this->prop[$a], $this->{$b});
maybe ? (to make it valid code) - Looking critically, the
$next === false
condition seems redundant in nearly all conditions (and there are no tests conceivable which could ever hit the condition).
If the function declaration would not have a scope closer, the scope opener would not be set, so$next
can never befalse
as there will always be a close curly after whatever code we're looking at.
Having said that, if the$next === false
bits would be removed, it might be prudent to update the "Skip function without body." block to also verify there is a scope closer. - I'm missing a test with non-OO function declaration. Use of the
parent
keyword in the body of such a function would be invalid no matter what, however, for the purposes of this sniff, I think it would be more appropriate to ignore such functions as they are, for sure, not a "method override".
What do you think ? This would be a functional change and could be made in a separate commit in this PR or via a separate PR.
The test-only changes should be included in this PR. As for the rest, it's up to you to decide whether you want to work on those changes too. If not, we can open an issue for the other points to be addressed separately at a later point in time.
Thank you very much for the detailed review, @jrfnl! I pushed a commit adding the new tests that you suggested. I will create a separate PR for the functional changes.
I also added a trait method declaration. This PR is ready for another review. |
Excellent! In the mean time, should we open an issue about those other observations to make sure they don't get lost ? (as this PR will hopefully be merged & closed after the next review) |
To add more tests with syntax errors in separate files in subsequent commits.
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.
@rodrigoprimo Thanks for the update. All good.
I'll rebase (without changes) and will merge this once the build has passed.
For the record, of the list from my previous review, the following points still need addressing in a follow-up PR: 3, 4, 9 and 10.
449a180
to
d179487
Compare
Thanks for merging this PR.
I just opened a PR addressing all the remaining points that you raised: #366 |
Description
This PR improves code coverage for the Generic.CodeAnalysis.UselessOverridingMethod sniff.
Related issues/external references
Part of #146
Types of changes
PR checklist