Skip to content

New sniff to simplify redundant falsey checks#2841

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

New sniff to simplify redundant falsey checks#2841
Crabcyborg merged 1 commit into
masterfrom
new_sniff_to_fix_redundant_false_checks

Conversation

@Crabcyborg

@Crabcyborg Crabcyborg commented Jan 20, 2026

Copy link
Copy Markdown
Contributor

Summary by CodeRabbit

  • New Features

    • Added a new code analysis rule that detects redundant falsy checks in conditional OR expressions and automatically provides simplified alternatives. Handles patterns where variables are checked for both emptiness and general falsiness simultaneously.
  • Chores

    • Updated test utilities to reflect simplified conditional logic patterns.

✏️ Tip: You can customize this high-level summary in your review settings.

@Crabcyborg Crabcyborg added this to the 6.28 milestone Jan 20, 2026
@Crabcyborg Crabcyborg changed the title New sniff to fix redundant falsey checks New sniff to simplify redundant falsey checks Jan 20, 2026
@coderabbitai

coderabbitai Bot commented Jan 20, 2026

Copy link
Copy Markdown
Contributor
📝 Walkthrough

Walkthrough

A new PHP_CodeSniffer sniff is introduced that detects and auto-fixes redundant falsy comparisons combined with negations in OR expressions. The sniff is registered in the ruleset configuration, and a test case is updated to align with the simplified logic pattern.

Changes

Cohort / File(s) Summary
Code Analysis Sniff Implementation
phpcs-sniffs/Formidable/Sniffs/CodeAnalysis/SimplifyRedundantFalsyCheckSniff.php
New sniff class (+382 lines) that detects two patterns of redundant falsy checks: $var == '' || ! $var and ! $var || $var == ''. Includes pattern matching, variable extraction, and auto-fix logic to replace redundant expressions with simplified forms.
Sniff Configuration
phpcs-sniffs/Formidable/ruleset.xml
Registers the new Formidable.CodeAnalysis.SimplifyRedundantFalsyCheck rule in the ruleset.
Test Updates
tests/phpunit/entries/test_FrmShowEntryShortcode.php
Simplifies expected output array by removing explicit empty-string check, relying on single falsiness evaluation.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

Poem

Hop! A sniff that finds redundant checks with glee,
Where ! $var and == '' dance in OR's decree,
Now simplified to one—the pattern's set free! 🐰✨

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
Title check ✅ Passed The title accurately describes the main change: introducing a new PHP_CodeSniffer sniff that detects and fixes redundant falsy checks in OR expressions.

✏️ 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 merged commit e3947a1 into master Jan 20, 2026
36 of 38 checks passed
@Crabcyborg
Crabcyborg deleted the new_sniff_to_fix_redundant_false_checks branch January 20, 2026 02:10

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 2

🤖 Fix all issues with AI agents
In
`@phpcs-sniffs/Formidable/Sniffs/CodeAnalysis/SimplifyRedundantFalsyCheckSniff.php`:
- Around line 360-363: The local variable $tokens in the applyFix method is
unused and should be removed to satisfy PHPMD; locate the applyFix(File
$phpcsFile, $conditionStart, $conditionEnd, $simplified) function and delete the
assignment "$tokens = $phpcsFile->getTokens();" (or remove the $tokens variable
if initialized elsewhere) so only $fixer = $phpcsFile->fixer; remains—ensure no
other code in applyFix references $tokens before committing.
- Around line 49-52: In the process method remove the unused assignment to
$tokens (currently "$tokens = $phpcsFile->getTokens();") to eliminate the PHPMD
unused variable warning, or if tokens are intended later, replace usages to
reference that variable; locate the process(File $phpcsFile, $stackPtr) function
in SimplifyRedundantFalsyCheckSniff and either delete the $tokens assignment or
use $tokens where getTokens() results are required.

Comment on lines +49 to +52
public function process( File $phpcsFile, $stackPtr ) {
$tokens = $phpcsFile->getTokens();

// Find the condition boundaries (inside parentheses).

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟡 Minor

Remove unused $tokens to avoid PHPMD warnings.

$tokens is assigned but never used in process.

🧹 Proposed cleanup
-		$tokens = $phpcsFile->getTokens();
-
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
public function process( File $phpcsFile, $stackPtr ) {
$tokens = $phpcsFile->getTokens();
// Find the condition boundaries (inside parentheses).
public function process( File $phpcsFile, $stackPtr ) {
// Find the condition boundaries (inside parentheses).
🧰 Tools
🪛 PHPMD (2.15.0)

50-50: Avoid unused local variables such as '$tokens'. (undefined)

(UnusedLocalVariable)

🤖 Prompt for AI Agents
In
`@phpcs-sniffs/Formidable/Sniffs/CodeAnalysis/SimplifyRedundantFalsyCheckSniff.php`
around lines 49 - 52, In the process method remove the unused assignment to
$tokens (currently "$tokens = $phpcsFile->getTokens();") to eliminate the PHPMD
unused variable warning, or if tokens are intended later, replace usages to
reference that variable; locate the process(File $phpcsFile, $stackPtr) function
in SimplifyRedundantFalsyCheckSniff and either delete the $tokens assignment or
use $tokens where getTokens() results are required.

Comment on lines +360 to +363
private function applyFix( File $phpcsFile, $conditionStart, $conditionEnd, $simplified ) {
$tokens = $phpcsFile->getTokens();
$fixer = $phpcsFile->fixer;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟡 Minor

Drop unused $tokens in applyFix.

This local is not referenced and will be flagged by PHPMD.

🧹 Proposed cleanup
-		$tokens = $phpcsFile->getTokens();
-		$fixer  = $phpcsFile->fixer;
+		$fixer = $phpcsFile->fixer;
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
private function applyFix( File $phpcsFile, $conditionStart, $conditionEnd, $simplified ) {
$tokens = $phpcsFile->getTokens();
$fixer = $phpcsFile->fixer;
private function applyFix( File $phpcsFile, $conditionStart, $conditionEnd, $simplified ) {
$fixer = $phpcsFile->fixer;
🧰 Tools
🪛 PHPMD (2.15.0)

361-361: Avoid unused local variables such as '$tokens'. (undefined)

(UnusedLocalVariable)

🤖 Prompt for AI Agents
In
`@phpcs-sniffs/Formidable/Sniffs/CodeAnalysis/SimplifyRedundantFalsyCheckSniff.php`
around lines 360 - 363, The local variable $tokens in the applyFix method is
unused and should be removed to satisfy PHPMD; locate the applyFix(File
$phpcsFile, $conditionStart, $conditionEnd, $simplified) function and delete the
assignment "$tokens = $phpcsFile->getTokens();" (or remove the $tokens variable
if initialized elsewhere) so only $fixer = $phpcsFile->fixer; remains—ensure no
other code in applyFix references $tokens before committing.

stephywells pushed a commit that referenced this pull request Apr 4, 2026
…alse_checks

New sniff to simplify redundant falsey 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