diff --git a/WordPress/AbstractFunctionRestrictionsSniff.php b/WordPress/AbstractFunctionRestrictionsSniff.php index fbde666ae0..9c42343292 100644 --- a/WordPress/AbstractFunctionRestrictionsSniff.php +++ b/WordPress/AbstractFunctionRestrictionsSniff.php @@ -11,6 +11,7 @@ use PHP_CodeSniffer\Util\Tokens; use PHPCSUtils\Utils\MessageHelper; +use WordPressCS\WordPress\Helpers\ContextHelper; use WordPressCS\WordPress\Helpers\RulesetPropertyHelper; use WordPressCS\WordPress\Sniff; @@ -219,7 +220,7 @@ public function is_targetted_token( $stackPtr ) { } // Exclude function definitions, class methods, and namespaced calls. - if ( $this->is_class_object_call( $stackPtr ) === true ) { + if ( ContextHelper::has_object_operator_before( $this->phpcsFile, $stackPtr ) === true ) { return false; } diff --git a/WordPress/Helpers/ContextHelper.php b/WordPress/Helpers/ContextHelper.php new file mode 100644 index 0000000000..dae6af6755 --- /dev/null +++ b/WordPress/Helpers/ContextHelper.php @@ -0,0 +1,51 @@ +getTokens(); + $before = $phpcsFile->findPrevious( Tokens::$emptyTokens, ( $stackPtr - 1 ), null, true ); + + return isset( Collections::objectOperators()[ $tokens[ $before ]['code'] ] ); + } +} diff --git a/WordPress/Sniff.php b/WordPress/Sniff.php index b316aae525..90366447b8 100644 --- a/WordPress/Sniff.php +++ b/WordPress/Sniff.php @@ -16,6 +16,7 @@ use PHPCSUtils\Utils\PassedParameters; use PHPCSUtils\Utils\Scopes; use PHPCSUtils\Utils\TextStrings; +use WordPressCS\WordPress\Helpers\ContextHelper; use WordPressCS\WordPress\Helpers\VariableHelper; /** @@ -507,34 +508,6 @@ protected function is_in_isset_or_empty( $stackPtr ) { return false; } - /** - * Check if a particular token is a (static or non-static) call to a class method or property. - * - * @internal Note: this may still mistake a namespaced function imported via a `use` statement for - * a global function! - * - * @since 2.1.0 - * - * @param int $stackPtr The index of the token in the stack. - * - * @return bool - */ - protected function is_class_object_call( $stackPtr ) { - $before = $this->phpcsFile->findPrevious( Tokens::$emptyTokens, ( $stackPtr - 1 ), null, true, null, true ); - - if ( false === $before ) { - return false; - } - - if ( \T_OBJECT_OPERATOR !== $this->tokens[ $before ]['code'] - && \T_DOUBLE_COLON !== $this->tokens[ $before ]['code'] - ) { - return false; - } - - return true; - } - /** * Check if a particular token is prefixed with a namespace. * @@ -637,7 +610,7 @@ protected function is_in_function_call( $stackPtr, $valid_functions, $global_fun /* * Now, make sure it is a global function. */ - if ( $this->is_class_object_call( $prev_non_empty ) === true ) { + if ( ContextHelper::has_object_operator_before( $this->phpcsFile, $prev_non_empty ) === true ) { continue; } @@ -1026,7 +999,7 @@ protected function is_validated( $stackPtr, $array_keys = array(), $in_condition continue 2; } - if ( $this->is_class_object_call( $i ) === true ) { + if ( ContextHelper::has_object_operator_before( $this->phpcsFile, $i ) === true ) { // Method call. continue 2; } diff --git a/WordPress/Sniffs/Security/NonceVerificationSniff.php b/WordPress/Sniffs/Security/NonceVerificationSniff.php index ed8d1fc6a5..7bc5ecdb9d 100644 --- a/WordPress/Sniffs/Security/NonceVerificationSniff.php +++ b/WordPress/Sniffs/Security/NonceVerificationSniff.php @@ -11,6 +11,7 @@ use PHP_CodeSniffer\Util\Tokens; use PHPCSUtils\Utils\MessageHelper; +use WordPressCS\WordPress\Helpers\ContextHelper; use WordPressCS\WordPress\Helpers\RulesetPropertyHelper; use WordPressCS\WordPress\Helpers\VariableHelper; use WordPressCS\WordPress\Sniff; @@ -269,7 +270,7 @@ private function has_nonce_check( $stackPtr ) { /* * Now, make sure it is a call to a global function. */ - if ( $this->is_class_object_call( $i ) === true ) { + if ( ContextHelper::has_object_operator_before( $this->phpcsFile, $i ) === true ) { continue; } diff --git a/WordPress/Sniffs/WP/GlobalVariablesOverrideSniff.php b/WordPress/Sniffs/WP/GlobalVariablesOverrideSniff.php index 35d2538bb7..fcb7a8273d 100644 --- a/WordPress/Sniffs/WP/GlobalVariablesOverrideSniff.php +++ b/WordPress/Sniffs/WP/GlobalVariablesOverrideSniff.php @@ -17,6 +17,7 @@ use PHPCSUtils\Utils\Parentheses; use PHPCSUtils\Utils\Scopes; use PHPCSUtils\Utils\TextStrings; +use WordPressCS\WordPress\Helpers\ContextHelper; use WordPressCS\WordPress\Helpers\IsUnitTestTrait; use WordPressCS\WordPress\Helpers\VariableHelper; use WordPressCS\WordPress\Helpers\WPGlobalVariablesHelper; @@ -393,7 +394,7 @@ protected function process_global_statement( $stackPtr, $in_function_scope ) { } // Don't throw false positives for static class properties. - if ( $this->is_class_object_call( $ptr ) === true ) { + if ( ContextHelper::has_object_operator_before( $this->phpcsFile, $ptr ) === true ) { continue; } diff --git a/WordPress/Tests/WP/DiscouragedFunctionsUnitTest.inc b/WordPress/Tests/WP/DiscouragedFunctionsUnitTest.inc index bc2d09b250..2693377ea4 100644 --- a/WordPress/Tests/WP/DiscouragedFunctionsUnitTest.inc +++ b/WordPress/Tests/WP/DiscouragedFunctionsUnitTest.inc @@ -1,5 +1,13 @@ query_posts(); // OK, not the global function. +MyClass::wp_reset_query(); // OK, not the global function. +$obj?->query_posts(); // OK, not the global function. diff --git a/WordPress/Tests/WP/DiscouragedFunctionsUnitTest.php b/WordPress/Tests/WP/DiscouragedFunctionsUnitTest.php index 93827ee355..d30f11cc10 100644 --- a/WordPress/Tests/WP/DiscouragedFunctionsUnitTest.php +++ b/WordPress/Tests/WP/DiscouragedFunctionsUnitTest.php @@ -20,6 +20,7 @@ * @since 0.13.0 Class name changed: this class is now namespaced. * * @covers \WordPressCS\WordPress\AbstractFunctionRestrictionsSniff + * @covers \WordPressCS\WordPress\Helpers\ContextHelper::has_object_operator_before * @covers \WordPressCS\WordPress\Sniffs\WP\DiscouragedFunctionsSniff */ final class DiscouragedFunctionsUnitTest extends AbstractSniffUnitTest { @@ -41,8 +42,7 @@ public function getErrorList() { public function getWarningList() { return array( 3 => 1, - 5 => 1, + 4 => 1, ); } - }