Skip to content

Commit

Permalink
Don't false detect objects as function calls.
Browse files Browse the repository at this point in the history
  • Loading branch information
anton-vlasenko authored and Anton Vlasenko committed Apr 17, 2024
1 parent af17dd9 commit 9416c9f
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,7 @@ protected static function find_hook_docblock( File $phpcs_file, $stack_pointer )
* Determines if a T_STRING token represents a function call.
*
* @param File $phpcs_file The file being scanned.
* @param int $stack_pointer The position to start looking for the docblock. *
* @param int $stack_pointer The position of the T_STRING token in question.
* @return bool True if the token represents a function call, false otherwise.
*/
protected static function is_function_call( File $phpcs_file, $stack_pointer ) {
Expand All @@ -476,7 +476,7 @@ protected static function is_function_call( File $phpcs_file, $stack_pointer ) {
return false;
}

if ( isset( $tokens[ $open_bracket ]['parenthesis_closer'] ) === false ) {
if ( false === isset( $tokens[ $open_bracket ]['parenthesis_closer'] ) ) {
// Not a function call.
return false;
}
Expand All @@ -485,12 +485,14 @@ protected static function is_function_call( File $phpcs_file, $stack_pointer ) {
$search = Tokens::$emptyTokens;
$search[] = T_BITWISE_AND;
$previous = $phpcs_file->findPrevious( $search, ( $stack_pointer - 1 ), null, true );
if ( T_FUNCTION === $tokens[ $previous ]['code'] ) {
// It's a function definition, not a function call.
return false;
}

return true;
$previous_tokens_to_ignore = array(
T_FUNCTION, // Function declaration.
T_NEW, // Creating an object.
T_OBJECT_OPERATOR, // Calling an object.
);

return ! in_array( $tokens[ $previous ]['code'], $previous_tokens_to_ignore, true );
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -612,3 +612,16 @@ if ( ! function_exists( 'apply_filters_deprecated' ) ) {
function apply_filters_deprecated() {
}
}

$some_object = new do_action();
$some_object = $my_object->do_action();
$some_object = new do_action_ref_array();
$some_object = $my_object->do_action_ref_array();
$some_object = new do_action_deprecated();
$some_object = $my_object->do_action_deprecated();
$some_object = new apply_filters();
$some_object = $my_object->apply_filters();
$some_object = new apply_filters_ref_array();
$some_object = $my_object->apply_filters_ref_array();
$some_object = new apply_filters_deprecated();
$some_object = $my_object->apply_filters_deprecated();

0 comments on commit 9416c9f

Please sign in to comment.