Improve redundant paren sniff and remove is_array checks before stric…#2845
Conversation
…t array comparison
📝 WalkthroughWalkthroughAdds a new PHPCS sniff that detects and auto-fixes redundant Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 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/RedundantIsArrayBeforeEmptyArrayCheckSniff.php`:
- Around line 45-105: The sniff currently matches any T_STRING with content
"is_array" including method/namespace usages; before proceeding in process( File
$phpcsFile, $stackPtr ) add a guard that fetches the previous non-whitespace
token (use $phpcsFile->findPrevious with $stackPtr - 1) and returns early if
that token code is T_OBJECT_OPERATOR, T_DOUBLE_COLON or T_NS_SEPARATOR (and also
return if previous token is T_STRING followed by T_NS_SEPARATOR to cover
namespaced qualifiers) so only the global function call is handled; keep the
rest of the logic unchanged and reference $stackPtr, $tokens and process() when
implementing the check.
In `@phpcs-sniffs/Formidable/Sniffs/CodeAnalysis/RedundantParenthesesSniff.php`:
- Around line 235-242: The logical operator detection in
RedundantParenthesesSniff.php is missing T_LOGICAL_XOR, so add T_LOGICAL_XOR to
the in_array list that increments $logicalCount (the block that checks for
T_BOOLEAN_AND, T_BOOLEAN_OR, T_LOGICAL_AND, T_LOGICAL_OR) to ensure `xor` is
treated as a logical operator and prevents unsafe removal of parentheses in
methods handling redundant parentheses detection.
🧹 Nitpick comments (1)
phpcs-sniffs/Formidable/Sniffs/CodeAnalysis/RedundantIsArrayBeforeEmptyArrayCheckSniff.php (1)
300-319: Drop unused parameters inreportAndFixto satisfy PHPMD.
$isArrayEndand$varContentare unused; tightening the signature removes static-analysis noise.♻️ Proposed refactor
- $this->reportAndFix( $phpcsFile, $stackPtr, $closeParen, $afterClose, $varContent ); + $this->reportAndFix( $phpcsFile, $stackPtr, $afterClose ); @@ - $this->reportAndFix( $phpcsFile, $stackPtr, $closeParen, $afterClose, $varContent ); + $this->reportAndFix( $phpcsFile, $stackPtr, $afterClose ); @@ - private function reportAndFix( File $phpcsFile, $isArrayStart, $isArrayEnd, $andToken, $varContent ) { + private function reportAndFix( File $phpcsFile, $isArrayStart, $andToken ) {
…f_and_remove_is_array_checks_before_strict_array_comparison Improve redundant paren sniff and remove is_array checks before stric…
…t array comparison
Summary by CodeRabbit
New Features
Refactor
Chores
✏️ Tip: You can customize this high-level summary in your review settings.