Add new sniffs to help with strict comparison issues that are easy to fix#2791
Conversation
|
Note
|
| Cohort / File(s) | Summary |
|---|---|
Code Updateclasses/models/FrmEntryMeta.php |
Changed equality operator from loose (==) to strict (===) in cache retrieval logic for limit checks; removed associated phpcs ignore comment. |
New PHPCS Sniff: Strict Comparisonphpcs-sniffs/Formidable/Sniffs/CodeAnalysis/PreferStrictComparisonSniff.php |
New sniff that detects loose equality (==, !=) operators with non-empty, non-numeric string literals and recommends strict equivalents (===, !==). Includes logic to skip lines with phpcs ignore directives and validates string safety before reporting errors. |
New PHPCS Sniff: Strict In-Arrayphpcs-sniffs/Formidable/Sniffs/CodeAnalysis/PreferStrictInArraySniff.php |
New sniff that detects in_array() calls without strict mode and enforces adding a third argument (true) when the haystack is an array of safe string literals. Includes depth-aware argument parsing and validation of array element types. |
Configuration Updatephpcs.xml |
Registers two new sniff rules in the Formidable ruleset: Formidable.CodeAnalysis.PreferStrictComparison and Formidable.CodeAnalysis.PreferStrictInArray. |
Estimated code review effort
🎯 4 (Complex) | ⏱️ ~50 minutes
Possibly related PRs
- Use strict for more array checks #2712: Both PRs apply strict comparison changes to the FrmEntryMeta class, suggesting coordinated enforcement of strict comparison standards.
- Treat strict in array WP phpcs rule as error #2738: Both PRs address strict-comparison rule enforcement and interact with the FrmEntryMeta class implementation.
Poem
🐰 With
===instead of==, the code now stands so tall,
In arrays we demand strictness,trueforin_arraycalls,
The sniff detects our slip-ups, string by string with care,
Formidable grows more rigorous—comparison beyond compare! ✨
🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
| Check name | Status | Explanation |
|---|---|---|
| Description Check | ✅ Passed | Check skipped - CodeRabbit’s high-level summary is enabled. |
| Docstring Coverage | ✅ Passed | Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%. |
| Title check | ✅ Passed | The title accurately summarizes the main change: adding new PHP_CodeSniffer sniffs to enforce strict comparison practices (PreferStrictComparison and PreferStrictInArray). |
✏️ Tip: You can configure your own custom pre-merge checks in the settings.
✨ Finishing touches
- 📝 Generate docstrings
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 @coderabbitai help to get the list of available commands and usage tips.
There was a problem hiding this comment.
Actionable comments posted: 0
🧹 Nitpick comments (2)
phpcs-sniffs/Formidable/Sniffs/CodeAnalysis/PreferStrictInArraySniff.php (2)
129-147: Asymmetric depth tracking may cause issues in edge cases.The depth is incremented conditionally (only when
parenthesis_closer/bracket_closeris set), but decremented unconditionally. In malformed or edge-case token streams, this could cause$depthto become negative.Consider making the decrement symmetric:
♻️ Suggested fix
if ( $code === T_CLOSE_PARENTHESIS ) { - --$depth; - continue; + if ( $depth > 0 ) { + --$depth; + } + continue; } if ( $code === T_OPEN_SHORT_ARRAY && isset( $tokens[ $i ]['bracket_closer'] ) ) { ++$depth; continue; } if ( $code === T_CLOSE_SHORT_ARRAY ) { - --$depth; - continue; + if ( $depth > 0 ) { + --$depth; + } + continue; } if ( $code === T_OPEN_SQUARE_BRACKET ) { ++$depth; continue; } if ( $code === T_CLOSE_SQUARE_BRACKET ) { - --$depth; - continue; + if ( $depth > 0 ) { + --$depth; + } + continue; }
274-289: Consider extracting shared helper methods into a trait.Both
isSafeString()andhasIgnoreComment()are nearly identical to their counterparts inPreferStrictComparisonSniff. Consider extracting these into a shared trait (e.g.,StrictComparisonHelperTrait) to reduce duplication and ease future maintenance.Also applies to: 299-323
📜 Review details
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (4)
classes/models/FrmEntryMeta.phpphpcs-sniffs/Formidable/Sniffs/CodeAnalysis/PreferStrictComparisonSniff.phpphpcs-sniffs/Formidable/Sniffs/CodeAnalysis/PreferStrictInArraySniff.phpphpcs.xml
🧰 Additional context used
🧬 Code graph analysis (2)
phpcs-sniffs/Formidable/Sniffs/CodeAnalysis/PreferStrictInArraySniff.php (1)
phpcs-sniffs/Formidable/Sniffs/CodeAnalysis/PreferStrictComparisonSniff.php (3)
register(35-37)process(47-101)hasIgnoreComment(139-163)
phpcs-sniffs/Formidable/Sniffs/CodeAnalysis/PreferStrictComparisonSniff.php (1)
phpcs-sniffs/Formidable/Sniffs/CodeAnalysis/PreferStrictInArraySniff.php (3)
register(33-35)process(45-108)hasIgnoreComment(299-323)
⏰ 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). (3)
- GitHub Check: Cypress
- GitHub Check: PHP 7.4 tests in WP 6.9
- GitHub Check: PHP 8 tests in WP 6.9
🔇 Additional comments (6)
classes/models/FrmEntryMeta.php (1)
396-396: LGTM!The switch from loose (
==) to strict (===) comparison is appropriate here since$limitis typed as a string and the comparison target is a string literal. This ensures type-safe comparison and aligns with the new strict comparison sniffs being introduced.phpcs-sniffs/Formidable/Sniffs/CodeAnalysis/PreferStrictComparisonSniff.php (3)
35-37: LGTM!The sniff correctly registers for
T_IS_EQUALandT_IS_NOT_EQUALtokens to detect loose comparisons.
47-101: LGTM!The process logic is well-structured:
- Early returns for ignore comments and non-string comparisons.
- Correctly identifies string literals on either side of the operator.
- Validates string safety before suggesting strict comparison.
- The fixer correctly replaces the loose operator with its strict equivalent.
114-129: LGTM!Good safety checks for string validation—excluding empty strings and numeric strings prevents false positives where type coercion semantics might actually be intended.
phpcs-sniffs/Formidable/Sniffs/CodeAnalysis/PreferStrictInArraySniff.php (1)
45-108: LGTM!The process logic correctly identifies
in_array()calls without a strict mode argument and suggests addingtrueas the third parameter when the haystack is an array of safe strings.phpcs.xml (1)
254-255: LGTM!The new sniff rules are correctly registered in the phpcs configuration. They're appropriately placed in the Code Analysis section alongside related sniffs.
…strict_comparison_issues Add new sniffs to help with strict comparison issues that are easy to fix
Most cases of this have already been fixed in Lite. But this was useful when applied to Pro.
Summary by CodeRabbit
✏️ Tip: You can customize this high-level summary in your review settings.