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
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions WordPressVIPMinimum/Sniffs/Hooks/AlwaysReturnInFilterSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,21 @@ private function processFunction( $stackPtr, $start = 0, $end = null ) {
*/
private function processFunctionBody( $stackPtr ) {

$filterName = $this->tokens[ $this->filterNamePtr ]['content'];

$methodProps = $this->phpcsFile->getMethodProperties( $stackPtr );
if ( $methodProps['is_abstract'] === true ) {
$message = 'The callback for the `%s` filter hook-in points to an abstract method. Please, make sure that all child class implementations of this abstract method always return a value.';
GaryJones marked this conversation as resolved.
Show resolved Hide resolved
$data = [ $filterName ];
$this->phpcsFile->addWarning( $message, $stackPtr, 'AbstractMethod', $data );
return;
}

if ( isset( $this->tokens[ $stackPtr ]['scope_opener'], $this->tokens[ $stackPtr ]['scope_closer'] ) === false ) {
// Live coding, parse or tokenizer error.
return;
}

$argPtr = $this->phpcsFile->findNext(
array_merge( Tokens::$emptyTokens, [ T_STRING, T_OPEN_PARENTHESIS ] ),
$stackPtr + 1,
Expand All @@ -189,8 +204,6 @@ private function processFunctionBody( $stackPtr ) {
return;
}

$filterName = $this->tokens[ $this->filterNamePtr ]['content'];

$functionBodyScopeStart = $this->tokens[ $stackPtr ]['scope_opener'];
$functionBodyScopeEnd = $this->tokens[ $stackPtr ]['scope_closer'];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,3 +172,26 @@ class bad_example_class_short_array { // Error.
}
}

abstract class warning_filter_points_to_abstract_method {
public function __construct() {
add_filter( 'good_example_abstract_class', [ $this, 'abstract_method' ] );
}

abstract public function abstract_method( $param ); // Warning.
}

class tokenizer_bug_test {
public function __construct() {
add_filter( 'tokenizer_bug', [ $this, 'tokenizer_bug' ] );
}

public function tokenizer_bug( $param ): namespace\Foo {} // Ok (tokenizer bug in PHPCS < 3.5.7/3.6.0).
}

// Intentional parse error. This has to be the last test in the file!
class parse_error_test {
public function __construct() {
add_filter( 'parse_error', [ $this, 'parse_error' ] );
}

public function parse_error( $param ) // Ok, parse error ignored.
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ public function getErrorList() {
* @return array <int line number> => <int number of warnings>
*/
public function getWarningList() {
return [];
return [
180 => 1,
];
}

}