Skip to content

AbstractFunctionParameterSniff: don't ignore first class callables #2544

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

Merged
Merged
Changes from all 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
39 changes: 29 additions & 10 deletions WordPress/AbstractFunctionParameterSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,19 @@ public function process_matched_token( $stackPtr, $group_name, $matched_content
$parameters = PassedParameters::getParameters( $this->phpcsFile, $stackPtr );

if ( empty( $parameters ) ) {
return $this->process_no_parameters( $stackPtr, $group_name, $matched_content );
/*
* Check if this is a first class callable.
*
* No need for extensive defensive coding as the `is_targetted_token()` method has already
* validated the open and close parentheses exist.
*/
$openParens = $this->phpcsFile->findNext( Tokens::$emptyTokens, ( $stackPtr + 1 ), null, true );
$firstNonEmpty = $this->phpcsFile->findNext( Tokens::$emptyTokens, ( $openParens + 1 ), null, true );
if ( \T_ELLIPSIS === $this->tokens[ $firstNonEmpty ]['code'] ) {
return $this->process_first_class_callable( $stackPtr, $group_name, $matched_content );
} else {
return $this->process_no_parameters( $stackPtr, $group_name, $matched_content );
}
} else {
return $this->process_parameters( $stackPtr, $group_name, $matched_content, $parameters );
}
Expand Down Expand Up @@ -104,15 +116,6 @@ public function is_targetted_token( $stackPtr ) {
return false;
}

// First class callable.
$firstNonEmpty = $this->phpcsFile->findNext( Tokens::$emptyTokens, ( $next + 1 ), null, true );
if ( \T_ELLIPSIS === $this->tokens[ $firstNonEmpty ]['code'] ) {
$secondNonEmpty = $this->phpcsFile->findNext( Tokens::$emptyTokens, ( $firstNonEmpty + 1 ), null, true );
if ( \T_CLOSE_PARENTHESIS === $this->tokens[ $secondNonEmpty ]['code'] ) {
return false;
}
}

return true;
}

Expand Down Expand Up @@ -147,4 +150,20 @@ abstract public function process_parameters( $stackPtr, $group_name, $matched_co
* normal file processing.
*/
public function process_no_parameters( $stackPtr, $group_name, $matched_content ) {}

/**
* Process the function if it is used as a first class callable.
*
* Defaults to doing nothing. Can be overloaded in child classes to implement specific checks
* on first class callable use of the function.
*
* @param int $stackPtr The position of the current token in the stack.
* @param string $group_name The name of the group which was matched.
* @param string $matched_content The token content (function name) which was matched
* in lowercase.
*
* @return int|void Integer stack pointer to skip forward or void to continue
* normal file processing.
*/
public function process_first_class_callable( $stackPtr, $group_name, $matched_content ) {}
}
Loading