-
Notifications
You must be signed in to change notification settings - Fork 106
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add custom sniff to warn about
empty()
Signed-off-by: Shyamsundar Gadde <[email protected]>
- Loading branch information
1 parent
b6e5c5a
commit f9eb45e
Showing
2 changed files
with
48 additions
and
0 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,40 @@ | ||
<?php | ||
/** | ||
* Sniff to restrict usage of the empty() construct. | ||
* | ||
* @package performance | ||
*/ | ||
|
||
namespace WPPCS\WPP\Sniffs\PHP; | ||
|
||
use PHP_CodeSniffer\Sniffs\Sniff; | ||
use PHP_CodeSniffer\Files\File; | ||
|
||
// phpcs:ignoreFile WordPress.NamingConventions.ValidVariableName -- required by the interface. | ||
|
||
/** | ||
* Restricts usage of empty(). | ||
*/ | ||
class RestrictedEmptyConstructSniff implements Sniff { | ||
|
||
/** | ||
* Registers the tokens that this sniff wants to listen for. | ||
*/ | ||
public function register(): array { | ||
return array( \T_EMPTY ); | ||
} | ||
|
||
/** | ||
* Processes this sniff, when one of its tokens is encountered. | ||
* | ||
* @param File $phpcsFile The file being scanned. | ||
* @param int $stackPtr The position of the current token in the stack. | ||
*/ | ||
public function process( File $phpcsFile, $stackPtr ): void { // phpcs:ignore SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint -- required by the interface. | ||
$phpcsFile->addWarning( | ||
'Usage of empty() can mask code problems. Consider explicit checks instead.', | ||
$stackPtr, | ||
'EmptyConstructUsage' | ||
); | ||
} | ||
} |
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