Skip to content

Add new sniff to simplify count equals 0 checks#2844

Merged
Crabcyborg merged 1 commit into
masterfrom
add_new_sniff_to_simplify_count_equals_0_checks
Jan 20, 2026
Merged

Add new sniff to simplify count equals 0 checks#2844
Crabcyborg merged 1 commit into
masterfrom
add_new_sniff_to_simplify_count_equals_0_checks

Conversation

@Crabcyborg
Copy link
Copy Markdown
Contributor

@Crabcyborg Crabcyborg commented Jan 20, 2026

If the compared arrays are large, calling count can be more expensive, though it looks like it's fairly negligible.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Jan 20, 2026

📝 Walkthrough

Walkthrough

A new PHP_CodeSniffer sniff is introduced to enforce empty array comparisons using array() === $var instead of count($var) === 0 patterns. The sniff includes automated fixing capability. The sniff is registered in the ruleset and an existing codebase file is updated to comply with the new style.

Changes

Cohort / File(s) Summary
Empty Array Comparison Sniff
phpcs-sniffs/Formidable/Sniffs/CodeAnalysis/PreferEmptyArrayComparisonSniff.php
New PHPCS sniff detecting count($var) === 0 patterns and suggesting/fixing them to use strict array() comparisons. Includes token analysis and bidirectional fix logic for both count(...) === 0 and 0 === count(...) forms.
Ruleset Registration
phpcs-sniffs/Formidable/ruleset.xml
Registers the new Formidable.CodeAnalysis.PreferEmptyArrayComparison sniff rule to enable enforcement.
Applied Code Compliance
classes/models/FrmEntry.php
Updates empty array check from (is_array(...) && 0 === count(...)) to (is_array(...) && array() === ...) to comply with the new sniff preference.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

Poem

🐰 A sniff that hops through tokens bright,
Transforming counts to comparisons right,
Empty arrays in a cleaner way,
The ruleset enforces it every day,
Automated fixes, no mess to stay! 🌟

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title contains a typo ('enw' instead of 'new') and accurately describes the main change: adding a PHPCS sniff to detect and simplify count equals 0 checks.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
  • 📝 Generate docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@Crabcyborg Crabcyborg changed the title Add enw sniff to simplify count equals 0 checks Add new sniff to simplify count equals 0 checks Jan 20, 2026
Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Fix all issues with AI agents
In
`@phpcs-sniffs/Formidable/Sniffs/CodeAnalysis/PreferEmptyArrayComparisonSniff.php`:
- Around line 45-51: The sniff currently treats any T_STRING with content
'count' as the global count() function and can false-positive on method calls
like $obj->count(); update the process(File $phpcsFile, $stackPtr) logic to
ensure the token at $stackPtr is a standalone function call by verifying the
previous meaningful token is not an object operator or scope resolution operator
(e.g., T_OBJECT_OPERATOR '->' or T_DOUBLE_COLON '::')—use
$phpcsFile->findPrevious or inspect $tokens[$prev]['code'] against those token
types (and skip empty tokens) before proceeding to treat it as count().
🧹 Nitpick comments (1)
phpcs-sniffs/Formidable/Sniffs/CodeAnalysis/PreferEmptyArrayComparisonSniff.php (1)

217-227: Remove unused parameter and add changeset wrapper for consistency.

The $closeParen parameter is unused (as flagged by static analysis). Additionally, for consistency with fixZeroEqualsCount(), consider wrapping the fixer operations in a changeset.

♻️ Proposed fix
-	private function fixCountEqualsZero( File $phpcsFile, $countStart, $closeParen, $zeroToken, $varContent, $isStrict ) {
+	private function fixCountEqualsZero( File $phpcsFile, $countStart, $zeroToken, $varContent, $isStrict ) {
 		$fixer      = $phpcsFile->fixer;
 		$comparison = $isStrict ? '===' : '==';

 		// Replace from count to 0 with $var === array().
+		$fixer->beginChangeset();
+
 		for ( $i = $countStart; $i <= $zeroToken; $i++ ) {
 			$fixer->replaceToken( $i, '' );
 		}

 		$fixer->addContent( $countStart, $varContent . ' ' . $comparison . ' array()' );
+
+		$fixer->endChangeset();
 	}

Also update the call site at line 103:

-				$this->fixCountEqualsZero( $phpcsFile, $stackPtr, $closeParen, $zeroToken, $varContent, $isStrict );
+				$this->fixCountEqualsZero( $phpcsFile, $stackPtr, $zeroToken, $varContent, $isStrict );

@Crabcyborg Crabcyborg merged commit f0f6213 into master Jan 20, 2026
37 of 38 checks passed
@Crabcyborg Crabcyborg deleted the add_new_sniff_to_simplify_count_equals_0_checks branch January 20, 2026 15:05
stephywells pushed a commit that referenced this pull request Apr 4, 2026
…unt_equals_0_checks

Add new sniff to simplify count equals 0 checks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant