Skip to content

Make new sniffs better at negating conditions#2806

Merged
Crabcyborg merged 2 commits into
masterfrom
make_new_sniffers_better_at_negating_conditions
Jan 15, 2026
Merged

Make new sniffs better at negating conditions#2806
Crabcyborg merged 2 commits into
masterfrom
make_new_sniffers_better_at_negating_conditions

Conversation

@Crabcyborg
Copy link
Copy Markdown
Contributor

@Crabcyborg Crabcyborg commented Jan 15, 2026

The code in Pro was being refactored funnily. 0 !== strpos would become ! 0 !== strpos instead of 0 === strpos.

Summary by CodeRabbit

  • Improvements
    • Enhanced code analysis sniffs with improved condition handling and operator transformation logic in early return and foreach analyses.

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

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Jan 15, 2026

📝 Walkthrough

Walkthrough

Two code analysis sniffs are enhanced with a new flipComparisonOperator() helper method. This method flips simple comparison operators (e.g., > to <=, === to !==) when negating conditions, with guards preventing flips when logical connectors are present. The negateCondition() logic is updated to attempt operator flipping before falling back to traditional negation approaches.

Changes

Cohort / File(s) Summary
Comparison Operator Flipping Enhancement
phpcs-sniffs/Formidable/Sniffs/CodeAnalysis/FlipForeachIfToContinueSniff.php, phpcs-sniffs/Formidable/Sniffs/CodeAnalysis/FlipIfToEarlyReturnSniff.php
Added private method flipComparisonOperator() to both sniff classes with operator mapping (!=====, !===, ><=, etc.). Enhanced negateCondition() to attempt operator flipping before falling back to existing negation logic. Guards prevent flipping when && or || are present.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

Poem

🐰 With whiskers twitched and nose in code,
We flip our operators down the road,
No more negations wrapped with care,
Just simple flips, a breathless pair!
From > to <=, how fair and neat,
Our early returns now feel complete!

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'Make new sniffs better at negating conditions' directly addresses the main change: improving negation logic in the new sniffs by adding operator-flipping helpers to correctly negate comparison operators instead of wrapping them in negation.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
  • 📝 Generate docstrings

🧹 Recent nitpick comments
phpcs-sniffs/Formidable/Sniffs/CodeAnalysis/FlipForeachIfToContinueSniff.php (1)

271-299: Consider extracting shared logic to a trait.

The flipComparisonOperator() implementation is identical to the one in FlipIfToEarlyReturnSniff.php (see lines 279-306 in that file). Consider extracting this into a shared trait to reduce duplication and ensure both sniffs stay synchronized.

Example trait extraction
// In a new file: ConditionNegationTrait.php
trait ConditionNegationTrait {
    private function flipComparisonOperator( $condition ) {
        $operatorMap = array(
            '!==' => '===',
            '===' => '!==',
            '!='  => '==',
            '=='  => '!=',
            '>='  => '<',
            '<='  => '>',
            '>'   => '<=',
            '<'   => '>=',
        );

        foreach ( $operatorMap as $op => $opposite ) {
            $pos = strpos( $condition, $op );

            if ( $pos !== false ) {
                if ( strpos( $condition, '&&' ) !== false || strpos( $condition, '||' ) !== false ) {
                    return false;
                }
                return substr( $condition, 0, $pos ) . $opposite . substr( $condition, $pos + strlen( $op ) );
            }
        }

        return false;
    }
}

📜 Recent review details

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between cd42e05 and afa2063.

📒 Files selected for processing (2)
  • phpcs-sniffs/Formidable/Sniffs/CodeAnalysis/FlipForeachIfToContinueSniff.php
  • phpcs-sniffs/Formidable/Sniffs/CodeAnalysis/FlipIfToEarlyReturnSniff.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/FlipIfToEarlyReturnSniff.php
  • phpcs-sniffs/Formidable/Sniffs/CodeAnalysis/FlipForeachIfToContinueSniff.php
🧬 Code graph analysis (2)
phpcs-sniffs/Formidable/Sniffs/CodeAnalysis/FlipIfToEarlyReturnSniff.php (1)
phpcs-sniffs/Formidable/Sniffs/CodeAnalysis/FlipForeachIfToContinueSniff.php (1)
  • flipComparisonOperator (271-299)
phpcs-sniffs/Formidable/Sniffs/CodeAnalysis/FlipForeachIfToContinueSniff.php (1)
phpcs-sniffs/Formidable/Sniffs/CodeAnalysis/FlipIfToEarlyReturnSniff.php (1)
  • flipComparisonOperator (279-307)
⏰ 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). (4)
  • GitHub Check: Cypress
  • GitHub Check: PHP 7.4 tests in WP 6.9
  • GitHub Check: PHP 8 tests in WP 6.9
  • GitHub Check: Run Rector inspection
🔇 Additional comments (2)
phpcs-sniffs/Formidable/Sniffs/CodeAnalysis/FlipForeachIfToContinueSniff.php (1)

243-252: LGTM! The negation logic fix is correct.

The added guard on line 243 prevents incorrectly stripping ! from !=/!== operators, and the new flow correctly attempts operator flipping before falling back to prefix negation. This properly addresses the PR objective of transforming 0 !== strpos to 0 === strpos instead of ! 0 !== strpos.

phpcs-sniffs/Formidable/Sniffs/CodeAnalysis/FlipIfToEarlyReturnSniff.php (1)

251-260: LGTM! Consistent implementation with the sibling sniff.

The logic correctly handles the negation edge cases:

  1. Preserves !=/!== operators by checking before stripping leading !
  2. Attempts operator flipping for cleaner negations (e.g., ===!==)
  3. Falls back to prefix negation only when flipping isn't applicable

✏️ Tip: You can disable this entire section by setting review_details to false in your review settings.


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 99f0354 into master Jan 15, 2026
16 checks passed
@Crabcyborg Crabcyborg deleted the make_new_sniffers_better_at_negating_conditions branch January 15, 2026 17:45
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