New sniff to simplify redundant falsey checks#2841
Conversation
📝 WalkthroughWalkthroughA 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
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
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. Comment |
There was a problem hiding this comment.
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.
| public function process( File $phpcsFile, $stackPtr ) { | ||
| $tokens = $phpcsFile->getTokens(); | ||
|
|
||
| // Find the condition boundaries (inside parentheses). |
There was a problem hiding this comment.
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.
| 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.
| private function applyFix( File $phpcsFile, $conditionStart, $conditionEnd, $simplified ) { | ||
| $tokens = $phpcsFile->getTokens(); | ||
| $fixer = $phpcsFile->fixer; | ||
|
|
There was a problem hiding this comment.
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.
| 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.
…alse_checks New sniff to simplify redundant falsey checks
Summary by CodeRabbit
New Features
Chores
✏️ Tip: You can customize this high-level summary in your review settings.