Update empty on param sniff to catch all params#2838
Conversation
📝 WalkthroughWalkthroughReplaced several Changes
Sequence Diagram(s)(omitted — changes are not a new multi-component feature nor a substantial control-flow interaction that benefits from a sequence diagram) 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 |
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #2838 +/- ##
============================================
- Coverage 27.05% 27.04% -0.01%
Complexity 8857 8857
============================================
Files 145 145
Lines 29817 29812 -5
============================================
- Hits 8066 8063 -3
+ Misses 21751 21749 -2 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Actionable comments posted: 0
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
phpcs-sniffs/Formidable/Sniffs/CodeAnalysis/RedundantEmptyOnParameterSniff.php (1)
240-309: Nested parentheses prevent detection in calls likeif ( foo( empty( $param ) ) ).The backward scan stops at the first unmatched open parenthesis and immediately returns
falseif it doesn't belong toif/elseif, never checking any parent parentheses. Forif ( foo( empty( $param ) ) ), the scan hitsempty(first and fails.Use
nested_parenthesisto check all enclosing parentheses instead of stopping at the first one.🔧 Suggested fix (use nested_parenthesis)
private function isInIfCondition( File $phpcsFile, $stackPtr ) { $tokens = $phpcsFile->getTokens(); - // Track parenthesis nesting to find the outermost condition parenthesis. - $parenDepth = 0; - - for ( $i = $stackPtr - 1; $i >= 0; $i-- ) { - $code = $tokens[ $i ]['code']; - - // Track parenthesis nesting. - if ( $code === T_CLOSE_PARENTHESIS ) { - ++$parenDepth; - continue; - } - - if ( $code === T_OPEN_PARENTHESIS ) { - if ( $parenDepth > 0 ) { - --$parenDepth; - continue; - } - - // This is an unmatched open paren - check if it belongs to if/elseif. - $beforeParen = $phpcsFile->findPrevious( T_WHITESPACE, $i - 1, null, true ); - - if ( false !== $beforeParen ) { - $beforeCode = $tokens[ $beforeParen ]['code']; - - if ( $beforeCode === T_IF || $beforeCode === T_ELSEIF ) { - return true; - } - } - - // This parenthesis doesn't belong to if/elseif. - return false; - } - - // Skip tokens that are valid inside an if condition. - if ( $code === T_WHITESPACE - || $code === T_BOOLEAN_NOT - || $code === T_BOOLEAN_AND - || $code === T_BOOLEAN_OR - || $code === T_LOGICAL_AND - || $code === T_LOGICAL_OR - || $code === T_VARIABLE - || $code === T_STRING - || $code === T_LNUMBER - || $code === T_DNUMBER - || $code === T_CONSTANT_ENCAPSED_STRING - || $code === T_TRUE - || $code === T_FALSE - || $code === T_NULL - || $code === T_ISSET - || $code === T_EMPTY - || $code === T_IS_EQUAL - || $code === T_IS_NOT_EQUAL - || $code === T_IS_IDENTICAL - || $code === T_IS_NOT_IDENTICAL - || $code === T_GREATER_THAN - || $code === T_LESS_THAN - || $code === T_IS_GREATER_OR_EQUAL - || $code === T_IS_SMALLER_OR_EQUAL - || $code === T_OBJECT_OPERATOR - || $code === T_NULLSAFE_OBJECT_OPERATOR - || $code === T_OPEN_SQUARE_BRACKET - || $code === T_CLOSE_SQUARE_BRACKET - || $code === T_DOUBLE_COLON - ) { - continue; - } - - // Hit something unexpected, stop searching. - return false; - } + $nested = $tokens[ $stackPtr ]['nested_parenthesis'] ?? array(); + if ( empty( $nested ) ) { + return false; + } + + foreach ( array_reverse( array_keys( $nested ) ) as $openParen ) { + $beforeParen = $phpcsFile->findPrevious( T_WHITESPACE, $openParen - 1, null, true ); + if ( false !== $beforeParen && in_array( $tokens[ $beforeParen ]['code'], array( T_IF, T_ELSEIF ), true ) ) { + return true; + } + } return false; }
…to_catch_all_params Update empty on param sniff to catch all params
Summary by CodeRabbit
Refactor
Chores
✏️ Tip: You can customize this high-level summary in your review settings.