-
-
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.
Move
is_class_object_call()
utility method to dedicated `ContextHel…
…per` The `is_class_object_call()` utility method is only used by a small set of sniffs, so is better placed in a dedicated class. This commit moves the `is_class_object_call()` method to the new `WordPressCS\WordPress\Helpers\ContextHelper` class and starts using that class in the relevant sniffs. The method has also been renamed to `has_object_operator_before()` as it doesn't actually check if something is a (method) _call_, just that something OO (method, constant, property) is being accessed. Related to 1465 This method is tested via the `WordPress.WP.DiscouragedFunctions` sniff.
- Loading branch information
Showing
7 changed files
with
78 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,59 @@ | ||
<?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 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 ) { | ||
$before = $phpcsFile->findPrevious( Tokens::$emptyTokens, ( $stackPtr - 1 ), null, true, null, true ); | ||
if ( false === $before ) { | ||
return false; | ||
} | ||
|
||
$tokens = $phpcsFile->getTokens(); | ||
if ( \T_OBJECT_OPERATOR !== $tokens[ $before ]['code'] | ||
&& \T_DOUBLE_COLON !== $tokens[ $before ]['code'] | ||
) { | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
} |
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,12 @@ | ||
<?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. |
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