diff --git a/WordPress/Helpers/DeprecationHelper.php b/WordPress/Helpers/DeprecationHelper.php new file mode 100644 index 0000000000..dc6a992755 --- /dev/null +++ b/WordPress/Helpers/DeprecationHelper.php @@ -0,0 +1,74 @@ +getTokens(); + $ignore = Tokens::$methodPrefixes; + $ignore[ \T_WHITESPACE ] = \T_WHITESPACE; + + for ( $comment_end = ( $stackPtr - 1 ); $comment_end >= 0; $comment_end-- ) { + if ( isset( $ignore[ $tokens[ $comment_end ]['code'] ] ) === true ) { + continue; + } + + if ( \T_ATTRIBUTE_END === $tokens[ $comment_end ]['code'] + && isset( $tokens[ $comment_end ]['attribute_opener'] ) === true + ) { + $comment_end = $tokens[ $comment_end ]['attribute_opener']; + continue; + } + + break; + } + + if ( \T_DOC_COMMENT_CLOSE_TAG !== $tokens[ $comment_end ]['code'] ) { + // Function doesn't have a doc comment or is using the wrong type of comment. + return false; + } + + $comment_start = $tokens[ $comment_end ]['comment_opener']; + foreach ( $tokens[ $comment_start ]['comment_tags'] as $tag ) { + if ( '@deprecated' === $tokens[ $tag ]['content'] ) { + return true; + } + } + + return false; + } +} diff --git a/WordPress/Sniff.php b/WordPress/Sniff.php index f61931d558..0e38885671 100644 --- a/WordPress/Sniff.php +++ b/WordPress/Sniff.php @@ -2345,39 +2345,4 @@ protected function get_list_variables( $stackPtr, $list_open_close = array() ) { return $var_pointers; } - - /** - * Check whether a function has been marked as deprecated via a @deprecated tag - * in the function docblock. - * - * {@internal This method is static to allow the ValidFunctionName class to use it.}} - * - * @since 2.2.0 - * - * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned. - * @param int $stackPtr The position of a T_FUNCTION - * token in the stack. - * - * @return bool - */ - public static function is_function_deprecated( File $phpcsFile, $stackPtr ) { - $tokens = $phpcsFile->getTokens(); - $find = Tokens::$methodPrefixes; - $find[] = \T_WHITESPACE; - - $comment_end = $phpcsFile->findPrevious( $find, ( $stackPtr - 1 ), null, true ); - if ( \T_DOC_COMMENT_CLOSE_TAG !== $tokens[ $comment_end ]['code'] ) { - // Function doesn't have a doc comment or is using the wrong type of comment. - return false; - } - - $comment_start = $tokens[ $comment_end ]['comment_opener']; - foreach ( $tokens[ $comment_start ]['comment_tags'] as $tag ) { - if ( '@deprecated' === $tokens[ $tag ]['content'] ) { - return true; - } - } - - return false; - } } diff --git a/WordPress/Sniffs/NamingConventions/PrefixAllGlobalsSniff.php b/WordPress/Sniffs/NamingConventions/PrefixAllGlobalsSniff.php index 1d0eceb444..d2817ede48 100644 --- a/WordPress/Sniffs/NamingConventions/PrefixAllGlobalsSniff.php +++ b/WordPress/Sniffs/NamingConventions/PrefixAllGlobalsSniff.php @@ -16,6 +16,7 @@ use PHPCSUtils\Utils\Scopes; use PHPCSUtils\Utils\TextStrings; use WordPressCS\WordPress\AbstractFunctionParameterSniff; +use WordPressCS\WordPress\Helpers\DeprecationHelper; use WordPressCS\WordPress\Helpers\IsUnitTestTrait; /** @@ -386,7 +387,7 @@ public function process_token( $stackPtr ) { return; } - if ( $this->is_function_deprecated( $this->phpcsFile, $stackPtr ) === true ) { + if ( DeprecationHelper::is_function_deprecated( $this->phpcsFile, $stackPtr ) === true ) { /* * Deprecated functions don't have to comply with the naming conventions, * otherwise functions deprecated in favour of a function with a compliant diff --git a/WordPress/Sniffs/NamingConventions/ValidFunctionNameSniff.php b/WordPress/Sniffs/NamingConventions/ValidFunctionNameSniff.php index 84a3267718..034613b72a 100644 --- a/WordPress/Sniffs/NamingConventions/ValidFunctionNameSniff.php +++ b/WordPress/Sniffs/NamingConventions/ValidFunctionNameSniff.php @@ -9,11 +9,12 @@ namespace WordPressCS\WordPress\Sniffs\NamingConventions; -use WordPressCS\WordPress\Sniff; use PHPCSUtils\BackCompat\BCTokens; use PHPCSUtils\Utils\FunctionDeclarations; use PHPCSUtils\Utils\ObjectDeclarations; use PHPCSUtils\Utils\Scopes; +use WordPressCS\WordPress\Helpers\DeprecationHelper; +use WordPressCS\WordPress\Sniff; /** * Enforces WordPress function name and method name format, based upon Squiz code. @@ -55,7 +56,7 @@ public function register() { */ public function process_token( $stackPtr ) { - if ( Sniff::is_function_deprecated( $this->phpcsFile, $stackPtr ) === true ) { + if ( DeprecationHelper::is_function_deprecated( $this->phpcsFile, $stackPtr ) === true ) { /* * Deprecated functions don't have to comply with the naming conventions, * otherwise functions deprecated in favour of a function with a compliant diff --git a/WordPress/Tests/NamingConventions/PrefixAllGlobalsUnitTest.1.inc b/WordPress/Tests/NamingConventions/PrefixAllGlobalsUnitTest.1.inc index d2bbe40147..2c1242788e 100644 --- a/WordPress/Tests/NamingConventions/PrefixAllGlobalsUnitTest.1.inc +++ b/WordPress/Tests/NamingConventions/PrefixAllGlobalsUnitTest.1.inc @@ -482,6 +482,20 @@ function acronym_lists_in_function_scope() { */ function deprecated_function() {} +/** + * Function description. + * + * @since 1.2.3 + * @deprecated 2.3.4 + * + * @return void + */ +#[MyAttribute([ + 'something', + 'something else', +])] +function deprecated_function_with_attribute() {} + /* * Bad: Issue https://github.com/WordPress/WordPress-Coding-Standards/issues/1733. * diff --git a/WordPress/Tests/NamingConventions/ValidFunctionNameUnitTest.inc b/WordPress/Tests/NamingConventions/ValidFunctionNameUnitTest.inc index e40fdde8d6..e7f2da4e56 100644 --- a/WordPress/Tests/NamingConventions/ValidFunctionNameUnitTest.inc +++ b/WordPress/Tests/NamingConventions/ValidFunctionNameUnitTest.inc @@ -163,3 +163,17 @@ function ___triple_underscore() {} // OK. class Triple { function ___triple_underscore() {} // OK. } + +class DeprecatedWithAttribute { + /** + * Function description. + * + * @since 1.2.3 + * @deprecated 2.3.4 + * + * @return void + */ + #[SomeAttribute] + #[AnotherAttribute] + public static function __deprecatedMethod() {} +}