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

Ignore return values when a filter callback is abstract #581

Merged

Conversation

kevinfodness
Copy link
Contributor

If a filter is defined in the constructor of an abstract class, and uses an abstract method as its callback, relying on implementing classes to define the return value, the WordPressVIPMinimum.Hooks.AlwaysReturnInFilter.MissingReturnStatement sniff causes phpcs to crash.

This PR adds a unit test to confirm the issue, and implements a fix using the same code that is used by phpcs itself.

Fixes #580

GaryJones and others added 2 commits September 9, 2020 09:22
* Adjust the "abstract method" test to:
    1. Expect a warning.
    2. Prevent the hook in getting confused with other functions in the same test file.
* Remove the "abstract method implementation" test as it wasn't testing anything.
    1. The sniff does not look for child classes, so wouldn't examine that code snippet anyway.
        Adding this functionality is not that useful either as in most cases, the child class will not be in the same file as the abstract parent class.
    2. And even if the sniff did examine it, it would still not recognize it as a child class as the class doesn't `extend` the abstract.
* Add a test for a typical case where declared functions do not have a scope opener/closer due to a tokenizer bug.
    PR squizlabs/PHP_CodeSniffer 3066 is open upstream to fix this.
* Add a "live coding" test case.
@jrfnl
Copy link
Collaborator

jrfnl commented Sep 9, 2020

I've added two additional commits to this PR, updating the unit tests and implementing my suggestion from #580 (comment)

@GaryJones
Copy link
Contributor

Noting that the new Warning will now appear when there was no warning previously (which was a false negative due to the lack of support for short array syntax).

@jrfnl
Copy link
Collaborator

jrfnl commented Sep 9, 2020

Noting that the new Warning will now appear when there was no warning previously (which was a false negative due to the lack of support for short array syntax).

Correct. I've also given it a separate error code on purpose with that in mind. Semver-wise, it should probably go into a minor rather than a patch version as it can be seen as a new feature.

… error

This implements the suggestion made in Automattic#580 (comment)

If the method a filter callback points to is an abstract method, a warning will be thrown asking for manual inspection of the child class implementations of the abstract method.

In case of parse or tokenizer error, the sniff will bow out and stay silent.
@jrfnl jrfnl force-pushed the hotfix/abstract-filter-callback branch from 14c9d9b to 4356274 Compare September 9, 2020 16:27
@GaryJones GaryJones dismissed rebeccahum’s stale review September 13, 2020 09:37

Merged suggested change.

@rebeccahum rebeccahum merged commit 200dec1 into Automattic:develop Sep 14, 2020
@GaryJones GaryJones added this to the 2.3.0 milestone Sep 22, 2020
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

Successfully merging this pull request may close these issues.

Sniff WordPressVIPMinimum.Hooks.AlwaysReturnInFilter.MissingReturnStatement fails on abstract methods
4 participants