-
-
Notifications
You must be signed in to change notification settings - Fork 492
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2052 from WordPress/feature/1465-new-deprecationh…
…elper 3.0: Move "deprecation" related utilities to dedicated DeprecationHelper + handle PHP 8.0 attributes
- Loading branch information
Showing
6 changed files
with
107 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
<?php | ||
/** | ||
* WordPress Coding Standard. | ||
* | ||
* @package WPCS\WordPressCodingStandards | ||
* @link https://github.com/WordPress/WordPress-Coding-Standards | ||
* @license https://opensource.org/licenses/MIT MIT | ||
*/ | ||
|
||
namespace WordPressCS\WordPress\Helpers; | ||
|
||
use PHP_CodeSniffer\Files\File; | ||
use PHP_CodeSniffer\Util\Tokens; | ||
|
||
/** | ||
* Helper utilities for checking whether something has been marked as deprecated. | ||
* | ||
* {@internal The functionality in this class will likely be replaced at some point in | ||
* the future by functions from PHPCSUtils.} | ||
* | ||
* @package WPCS\WordPressCodingStandards | ||
* @since 3.0.0 The method in this class was previously contained in the | ||
* `WordPressCS\WordPress\Sniff` class and has been moved here. | ||
*/ | ||
final class DeprecationHelper { | ||
|
||
/** | ||
* Check whether a function has been marked as deprecated via a @deprecated tag | ||
* in the function docblock. | ||
* | ||
* @since 2.2.0 | ||
* @since 3.0.0 Moved from the Sniff class to this class. | ||
* | ||
* @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(); | ||
$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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters