-
-
Notifications
You must be signed in to change notification settings - Fork 468
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 #2226 from WordPress/feature/move-is-class-object-…
…call-to-helper-class Move is_class_object_call() utility method to dedicated `ContextHelper` + support nullsafe object operator
- Loading branch information
Showing
7 changed files
with
71 additions
and
36 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
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,51 @@ | ||
<?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; | ||
use PHPCSUtils\Tokens\Collections; | ||
|
||
/** | ||
* Helper utilities for checking the context in which a token is used. | ||
* | ||
* --------------------------------------------------------------------------------------------- | ||
* This class is only intended for internal use by WordPressCS and is not part of the public API. | ||
* This also means that it has no promise of backward compatibility. Use at your own risk. | ||
* --------------------------------------------------------------------------------------------- | ||
* | ||
* @package WPCS\WordPressCodingStandards | ||
* @since 3.0.0 The methods in this class were previously contained in the | ||
* `WordPressCS\WordPress\Sniff` class and have been moved here. | ||
*/ | ||
final class ContextHelper { | ||
|
||
/** | ||
* Check if a particular token acts - statically or non-statically - on an object. | ||
* | ||
* @internal Note: this may still mistake a namespaced function imported via a `use` statement for | ||
* a global function! | ||
* | ||
* @since 2.1.0 | ||
* @since 3.0.0 - Moved from the Sniff class to this class. | ||
* - The method visibility was changed from `protected` to `public static`. | ||
* | ||
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned. | ||
* @param int $stackPtr The index of the token in the stack. | ||
* | ||
* @return bool | ||
*/ | ||
public static function has_object_operator_before( File $phpcsFile, $stackPtr ) { | ||
$tokens = $phpcsFile->getTokens(); | ||
$before = $phpcsFile->findPrevious( Tokens::$emptyTokens, ( $stackPtr - 1 ), null, true ); | ||
|
||
return isset( Collections::objectOperators()[ $tokens[ $before ]['code'] ] ); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,13 @@ | ||
<?php | ||
|
||
query_posts(); // Warning, use WP_Query instead. | ||
|
||
wp_reset_query(); // Warning, use wp_reset_postdata instead. | ||
|
||
/* | ||
* Tests which are more specifically for the AbstractFunctionRestrictionsSniff class and Helper methods. | ||
*/ | ||
|
||
// Ensure the sniff doesn't act on methods calls. | ||
$obj->query_posts(); // OK, not the global function. | ||
MyClass::wp_reset_query(); // OK, not the global function. | ||
$obj?->query_posts(); // OK, not the global function. |
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