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

Generic/UnusedFunctionParameter: improve code coverage #249

Conversation

rodrigoprimo
Copy link
Contributor

@rodrigoprimo rodrigoprimo commented Jan 11, 2024

Description

This PR improves the code coverage for the Generic.CodeAnalysis.UnusedFunctionParameter sniff.

It also includes three commits that change the sniff code directly.

The first commit removes an unused variable.

The second commit renames two $tmp variables to have more descriptive names (the new names are a bit long, so I ended up splitting two lines into multiple lines to improve the readability of the code).

The third commit removes an if condition that, as far as I could check, is not reachable. More information is in the commit message.

Happy to drop those three commits as they are outside of the scope of #146.

I might be missing something, but while working on the new tests, I noticed that the following code triggers the sniff while I believe it should not trigger it:

class InterfaceMethodNotImplement implements SomeInterface {
    public function notImplemented2($param) {
        return 'string' . 'concatenation';
    }
}

The sniff is not triggered for methods that are part of a class that implements an interface and that return on the first line. Is there are reason that I'm missing that would justify triggering this sniff for such methods when there is more than one non-empty token between the return statement and the semicolon?

Related issues/external references

Part of #146

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
    • This change is only breaking for integrators, not for external standards or end-users.
  • Documentation improvement

PR checklist

  • I have checked there is no other PR open for the same change.
  • I have read the Contribution Guidelines.
  • I grant the project the right to include and distribute the code under the BSD-3-Clause license (and I have the right to grant these rights).
  • I have added tests to cover my changes.
  • I have verified that the code complies with the projects coding standards.
  • [Required for new sniffs] I have added XML documentation for the sniff.

@jrfnl
Copy link
Member

jrfnl commented Jan 17, 2024

I might be missing something, but while working on the new tests, I noticed that the following code triggers the sniff while I believe it should not trigger it:

class InterfaceMethodNotImplement implements SomeInterface {
    public function notImplemented2($param) {
        return 'string' . 'concatenation';
    }
}

The sniff is not triggered for methods that are part of a class that implements an interface and that return on the first line. Is there are reason that I'm missing that would justify triggering this sniff for such methods when there is more than one non-empty token between the return statement and the semicolon?

@rodrigoprimo This may just have been an oversight when the sniff was originally created, though I'm not sure it is.
The code pattern being looked for is typically something like return false; when the interface method implementation does not contain any logic.
As soon as more is being done in the return statement than returning a simple value, it is an indication that the statement needs to be looked at and can not be presumed to be an interface method.
Think return is_something( $a ) ? $a : false;.

Keep in mind that the sniff cannot check whether a method being implemented is an interface method as it doesn't have access to the method names expected by the interface, it can only make assumptions based on common code patterns.

Note: might be an idea to open an issue for questions like this instead of tightly coupling them to a pull request, which can delay the pull request (as the question would need to be answered before the PR can be merged).

@jrfnl
Copy link
Member

jrfnl commented Jan 17, 2024

Pro-tip: when posting code samples, add the language after the triple backtick to allow GH to highlight the code for better readability. I.e. ```php

@jrfnl jrfnl added this to the 3.8.x Next milestone Jan 17, 2024
To add more tests with syntax errors in separate files in subsequent
commits.
I found this while working on improving code coverage for this sniff.
The value of this variable is defined again on line 112 and then only
used right below it on line 113
(https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/84acf4e56f110db8e75cb9a575c5727df637643c/src/Standards/Generic/Sniffs/CodeAnalysis/UnusedFunctionParameterSniff.php#L112-L113)
so it should be safe to remove it.
This commit renames `$tmp` variables using more descriptive names to
make it easier to understand the code.
Copy link
Member

@jrfnl jrfnl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @rodrigoprimo ! LGTM.

I'll rebase the PR (without changes) before merging it.

For the future - when making functional changes, please explain those briefly in the PR description instead of pointing to a commit message.
(or include commit message(s) in the PR description)

At this point in the code, there should always be a non-empty token
after the return statement, even if only the scope closer, and thus the
if condition will never be true.
If there is a syntax error and there is nothing after the return, the
code will never reach this point as in the beginning of the method the
code checks if `$token['scope_opener']` is set and this only happens
when there is also a scope_closer.
@jrfnl jrfnl force-pushed the test-coverage-unused-function-paremeter branch from d6998ac to e881193 Compare January 17, 2024 13:20
@jrfnl
Copy link
Member

jrfnl commented Jan 17, 2024

Actually... I'm making one small change and that's removing a comment which is redundant in the last commit.

@jrfnl jrfnl merged commit ebe02f5 into PHPCSStandards:master Jan 17, 2024
38 checks passed
@rodrigoprimo rodrigoprimo deleted the test-coverage-unused-function-paremeter branch January 18, 2024 17:56
@jrfnl jrfnl modified the milestones: 3.8.x Next, 3.9.0 Jan 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants