-
Notifications
You must be signed in to change notification settings - Fork 41
Add new sniff to simplify count equals 0 checks #2844
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Crabcyborg
merged 1 commit into
master
from
add_new_sniff_to_simplify_count_equals_0_checks
Jan 20, 2026
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
254 changes: 254 additions & 0 deletions
254
phpcs-sniffs/Formidable/Sniffs/CodeAnalysis/PreferEmptyArrayComparisonSniff.php
This file contains hidden or 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,254 @@ | ||
| <?php | ||
| /** | ||
| * Formidable_Sniffs_CodeAnalysis_PreferEmptyArrayComparisonSniff | ||
| * | ||
| * Detects count($var) === 0 and suggests using $var === array() instead. | ||
| * | ||
| * @package Formidable\Sniffs | ||
| */ | ||
|
|
||
| namespace Formidable\Sniffs\CodeAnalysis; | ||
|
|
||
| use PHP_CodeSniffer\Sniffs\Sniff; | ||
| use PHP_CodeSniffer\Files\File; | ||
|
|
||
| /** | ||
| * Detects count($var) === 0 and converts to $var === array(). | ||
| * | ||
| * Bad: | ||
| * if ( count( $items ) === 0 ) {} | ||
| * if ( 0 === count( $items ) ) {} | ||
| * | ||
| * Good: | ||
| * if ( $items === array() ) {} | ||
| * if ( array() === $items ) {} | ||
| */ | ||
| class PreferEmptyArrayComparisonSniff implements Sniff { | ||
|
|
||
| /** | ||
| * Returns an array of tokens this test wants to listen for. | ||
| * | ||
| * @return array | ||
| */ | ||
| public function register() { | ||
| return array( T_STRING ); | ||
| } | ||
|
|
||
| /** | ||
| * Processes this test, 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 passed in $tokens. | ||
| * | ||
| * @return void | ||
| */ | ||
| public function process( File $phpcsFile, $stackPtr ) { | ||
| $tokens = $phpcsFile->getTokens(); | ||
|
|
||
| // Check if this is a count() function call. | ||
| if ( strtolower( $tokens[ $stackPtr ]['content'] ) !== 'count' ) { | ||
| return; | ||
| } | ||
|
|
||
| // Find the opening parenthesis. | ||
| $openParen = $phpcsFile->findNext( T_WHITESPACE, $stackPtr + 1, null, true ); | ||
|
|
||
| if ( false === $openParen || $tokens[ $openParen ]['code'] !== T_OPEN_PARENTHESIS ) { | ||
| return; | ||
| } | ||
|
|
||
| // Find the closing parenthesis. | ||
| if ( ! isset( $tokens[ $openParen ]['parenthesis_closer'] ) ) { | ||
| return; | ||
| } | ||
|
|
||
| $closeParen = $tokens[ $openParen ]['parenthesis_closer']; | ||
|
|
||
| // Get the variable inside count(). | ||
| $varStart = $phpcsFile->findNext( T_WHITESPACE, $openParen + 1, $closeParen, true ); | ||
|
|
||
| if ( false === $varStart ) { | ||
| return; | ||
| } | ||
|
|
||
| // Build the variable content (could be $var, $var['key'], $this->prop, etc.). | ||
| $varContent = $this->getVariableContent( $phpcsFile, $varStart, $closeParen ); | ||
|
|
||
| if ( empty( $varContent ) ) { | ||
| return; | ||
| } | ||
|
|
||
| // Check for comparison with 0 after count(). | ||
| // Pattern: count($var) === 0 or count($var) == 0. | ||
| $afterClose = $phpcsFile->findNext( T_WHITESPACE, $closeParen + 1, null, true ); | ||
|
|
||
| if ( false !== $afterClose && $this->isComparisonWithZero( $phpcsFile, $afterClose ) ) { | ||
| $comparisonToken = $afterClose; | ||
| $zeroToken = $phpcsFile->findNext( T_WHITESPACE, $comparisonToken + 1, null, true ); | ||
| $isStrict = $tokens[ $comparisonToken ]['code'] === T_IS_IDENTICAL; | ||
|
|
||
| $fix = $phpcsFile->addFixableError( | ||
| 'Use "%s %s array()" instead of "count( %s ) %s 0".', | ||
| $stackPtr, | ||
| 'CountEqualsZero', | ||
| array( | ||
| $varContent, | ||
| $isStrict ? '===' : '==', | ||
| $varContent, | ||
| $isStrict ? '===' : '==', | ||
| ) | ||
| ); | ||
|
|
||
| if ( true === $fix ) { | ||
| $this->fixCountEqualsZero( $phpcsFile, $stackPtr, $closeParen, $zeroToken, $varContent, $isStrict ); | ||
| } | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| // Check for comparison with 0 before count(). | ||
| // Pattern: 0 === count($var) or 0 == count($var). | ||
| $beforeCount = $phpcsFile->findPrevious( T_WHITESPACE, $stackPtr - 1, null, true ); | ||
|
|
||
| if ( false !== $beforeCount && $this->isComparisonOperator( $phpcsFile, $beforeCount ) ) { | ||
| $comparisonToken = $beforeCount; | ||
| $zeroToken = $phpcsFile->findPrevious( T_WHITESPACE, $comparisonToken - 1, null, true ); | ||
|
|
||
| if ( false !== $zeroToken && $tokens[ $zeroToken ]['code'] === T_LNUMBER && $tokens[ $zeroToken ]['content'] === '0' ) { | ||
| $isStrict = $tokens[ $comparisonToken ]['code'] === T_IS_IDENTICAL; | ||
|
|
||
| $fix = $phpcsFile->addFixableError( | ||
| 'Use "array() %s %s" instead of "0 %s count( %s )".', | ||
| $stackPtr, | ||
| 'ZeroEqualsCount', | ||
| array( | ||
| $isStrict ? '===' : '==', | ||
| $varContent, | ||
| $isStrict ? '===' : '==', | ||
| $varContent, | ||
| ) | ||
| ); | ||
|
|
||
| if ( true === $fix ) { | ||
| $this->fixZeroEqualsCount( $phpcsFile, $zeroToken, $closeParen, $varContent, $isStrict ); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Get the variable content from inside count(). | ||
| * | ||
| * @param File $phpcsFile The file being scanned. | ||
| * @param int $varStart The start position of the variable. | ||
| * @param int $closeParen The closing parenthesis position. | ||
| * | ||
| * @return string The variable content. | ||
| */ | ||
| private function getVariableContent( File $phpcsFile, $varStart, $closeParen ) { | ||
| $tokens = $phpcsFile->getTokens(); | ||
| $content = ''; | ||
|
|
||
| for ( $i = $varStart; $i < $closeParen; $i++ ) { | ||
| if ( $tokens[ $i ]['code'] === T_WHITESPACE ) { | ||
| // Skip leading/trailing whitespace but keep internal whitespace. | ||
| if ( empty( $content ) ) { | ||
| continue; | ||
| } | ||
| } | ||
|
|
||
| $content .= $tokens[ $i ]['content']; | ||
| } | ||
|
|
||
| return rtrim( $content ); | ||
| } | ||
|
|
||
| /** | ||
| * Check if the token is a comparison with zero. | ||
| * | ||
| * @param File $phpcsFile The file being scanned. | ||
| * @param int $stackPtr The position to check. | ||
| * | ||
| * @return bool | ||
| */ | ||
| private function isComparisonWithZero( File $phpcsFile, $stackPtr ) { | ||
| $tokens = $phpcsFile->getTokens(); | ||
|
|
||
| if ( ! $this->isComparisonOperator( $phpcsFile, $stackPtr ) ) { | ||
| return false; | ||
| } | ||
|
|
||
| // Check if the next non-whitespace token is 0. | ||
| $nextToken = $phpcsFile->findNext( T_WHITESPACE, $stackPtr + 1, null, true ); | ||
|
|
||
| if ( false === $nextToken ) { | ||
| return false; | ||
| } | ||
|
|
||
| return $tokens[ $nextToken ]['code'] === T_LNUMBER && $tokens[ $nextToken ]['content'] === '0'; | ||
| } | ||
|
|
||
| /** | ||
| * Check if the token is a comparison operator (=== or ==). | ||
| * | ||
| * @param File $phpcsFile The file being scanned. | ||
| * @param int $stackPtr The position to check. | ||
| * | ||
| * @return bool | ||
| */ | ||
| private function isComparisonOperator( File $phpcsFile, $stackPtr ) { | ||
| $tokens = $phpcsFile->getTokens(); | ||
|
|
||
| return in_array( $tokens[ $stackPtr ]['code'], array( T_IS_IDENTICAL, T_IS_EQUAL ), true ); | ||
| } | ||
|
|
||
| /** | ||
| * Fix count($var) === 0 to $var === array(). | ||
| * | ||
| * @param File $phpcsFile The file being scanned. | ||
| * @param int $countStart The count function start position. | ||
| * @param int $closeParen The closing parenthesis position. | ||
| * @param int $zeroToken The zero token position. | ||
| * @param string $varContent The variable content. | ||
| * @param bool $isStrict Whether the comparison is strict. | ||
| * | ||
| * @return void | ||
| */ | ||
| private function fixCountEqualsZero( File $phpcsFile, $countStart, $closeParen, $zeroToken, $varContent, $isStrict ) { | ||
| $fixer = $phpcsFile->fixer; | ||
| $comparison = $isStrict ? '===' : '=='; | ||
|
|
||
| // Replace from count to 0 with $var === array(). | ||
| for ( $i = $countStart; $i <= $zeroToken; $i++ ) { | ||
| $fixer->replaceToken( $i, '' ); | ||
| } | ||
|
|
||
| $fixer->addContent( $countStart, $varContent . ' ' . $comparison . ' array()' ); | ||
| } | ||
|
|
||
| /** | ||
| * Fix 0 === count($var) to array() === $var. | ||
| * | ||
| * @param File $phpcsFile The file being scanned. | ||
| * @param int $zeroToken The zero token position. | ||
| * @param int $closeParen The closing parenthesis position. | ||
| * @param string $varContent The variable content. | ||
| * @param bool $isStrict Whether the comparison is strict. | ||
| * | ||
| * @return void | ||
| */ | ||
| private function fixZeroEqualsCount( File $phpcsFile, $zeroToken, $closeParen, $varContent, $isStrict ) { | ||
| $fixer = $phpcsFile->fixer; | ||
| $comparison = $isStrict ? '===' : '=='; | ||
|
|
||
| // Replace from 0 to closing paren with array() === $var. | ||
| $fixer->beginChangeset(); | ||
|
|
||
| for ( $i = $zeroToken; $i <= $closeParen; $i++ ) { | ||
| $fixer->replaceToken( $i, '' ); | ||
| } | ||
|
|
||
| $fixer->replaceToken( $zeroToken, 'array() ' . $comparison . ' ' . $varContent ); | ||
| $fixer->endChangeset(); | ||
| } | ||
| } | ||
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.