Update sniffs to avoid false positive issues in pro#2800
Conversation
📝 WalkthroughWalkthroughUpdates two PHPCS sniffs. One refines detection of unconditional variable assignments by distinguishing conditional contexts and adds helper methods for block ownership/conditional checks. The other adds a pre-check to skip processing when a ternary already exists in a statement, introducing a helper to detect ternaries. Changes
Estimated code review effort🎯 4 (Complex) | ⏱️ ~55 minutes Possibly related PRs
Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 inconclusive)
✅ Passed checks (2 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: 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/RedundantEmptyOnAssignedVariableSniff.php (1)
366-407: Remove unused variable$hasAnyAssignment.The variable is set at line 386 but never read. The return statement at line 407 only uses
$hasUnconditionalAssignment. The comment suggests intended usage, but the implementation doesn't use it.🧹 Proposed fix
- $hasUnconditionalAssignment = false; - $hasAnyAssignment = false; + $hasUnconditionalAssignment = false; // Search from the function start to the statement. for ( $i = $scopeOpener + 1; $i < $statementToken; $i++ ) { if ( $tokens[ $i ]['code'] !== T_VARIABLE ) { continue; } if ( $tokens[ $i ]['content'] !== $variableName ) { continue; } // Check if this variable is being assigned (has = after it). $nextToken = $phpcsFile->findNext( T_WHITESPACE, $i + 1, null, true ); if ( false === $nextToken || $tokens[ $nextToken ]['code'] !== T_EQUAL ) { continue; } - $hasAnyAssignment = true; - // Check if the assignment is at the same scope level as the statement.
📜 Review details
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (2)
phpcs-sniffs/Formidable/Sniffs/CodeAnalysis/RedundantEmptyOnAssignedVariableSniff.phpphpcs-sniffs/Formidable/Sniffs/CodeAnalysis/SimplifyDualConditionToTernarySniff.php
🧰 Additional context used
📓 Path-based instructions (1)
**/*.php
⚙️ CodeRabbit configuration file
**/*.php: - Do not make suggestions when we use x.x for a version placeholder. This is used like "@SInCE x.x" in a PHP doc comment, but also like 'x.x' when functions like _deprecated_function() are used.
- Treat these placeholders as acceptable in this codebase.
Files:
phpcs-sniffs/Formidable/Sniffs/CodeAnalysis/SimplifyDualConditionToTernarySniff.phpphpcs-sniffs/Formidable/Sniffs/CodeAnalysis/RedundantEmptyOnAssignedVariableSniff.php
🪛 PHPMD (2.15.0)
phpcs-sniffs/Formidable/Sniffs/CodeAnalysis/RedundantEmptyOnAssignedVariableSniff.php
367-367: Avoid unused local variables such as '$hasAnyAssignment'. (undefined)
(UnusedLocalVariable)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (6)
- GitHub Check: Cypress
- GitHub Check: PHP 7.4 tests in WP 6.9
- GitHub Check: PHP 8 tests in WP 6.9
- GitHub Check: PHP 8 tests in WP 6.9
- GitHub Check: PHP 7.4 tests in WP 6.9
- GitHub Check: Cypress
🔇 Additional comments (4)
phpcs-sniffs/Formidable/Sniffs/CodeAnalysis/SimplifyDualConditionToTernarySniff.php (2)
113-116: LGTM!Good addition. The early return when a ternary already exists in the statement prevents the sniff from suggesting a transformation that would create nested or redundant ternaries.
487-530: LGTM!The
statementContainsTernaryhelper correctly identifies statement boundaries and scans forT_INLINE_THEN. The fallback tocount( $tokens ) - 1when no semicolon is found handles edge cases gracefully.phpcs-sniffs/Formidable/Sniffs/CodeAnalysis/RedundantEmptyOnAssignedVariableSniff.php (2)
419-458: LGTM!The
isInsideConditionalBlockmethod correctly walks backwards to find if a token is inside anif/else/elseifblock. The containment check at line 436 properly handles brace boundaries.
460-496: LGTM!The
findBraceOwnermethod handles brace ownership detection well:
- First checks the standard PHPCS
scope_conditionattribute- Falls back to backwards traversal for direct owners (
else,do,try,finally)- Handles parenthesis-based owners via
parenthesis_owner
✏️ Tip: You can disable this entire section by setting review_details to false in your review settings.
…_positive_issues_in_pro Update sniffs to avoid false positive issues in pro
I'm planning to pull these sniffs into our other plugins so they get checked for every PR, but Pro is changing on some lines that it shouldn't.
Summary by CodeRabbit
✏️ Tip: You can customize this high-level summary in your review settings.