Skip to content

Add new sniffs to help with strict comparison issues that are easy to fix#2791

Merged
Crabcyborg merged 3 commits into
masterfrom
add_new_sniffs_to_help_with_strict_comparison_issues
Jan 13, 2026
Merged

Add new sniffs to help with strict comparison issues that are easy to fix#2791
Crabcyborg merged 3 commits into
masterfrom
add_new_sniffs_to_help_with_strict_comparison_issues

Conversation

@Crabcyborg

@Crabcyborg Crabcyborg commented Jan 13, 2026

Copy link
Copy Markdown
Contributor

Most cases of this have already been fixed in Lite. But this was useful when applied to Pro.

Summary by CodeRabbit

  • Chores
    • Added automated code quality checks to improve code reliability and prevent comparison-related bugs throughout the application.

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

@coderabbitai

coderabbitai Bot commented Jan 13, 2026

Copy link
Copy Markdown
Contributor

Note

.coderabbit.yaml has unrecognized properties

CodeRabbit is using all valid settings from your configuration. Unrecognized properties (listed below) have been ignored and may indicate typos or deprecated fields that can be removed.

⚠️ Parsing warnings (1)
Validation error: Unrecognized key(s) in object: 'path_instructions'
⚙️ Configuration instructions
  • Please see the configuration documentation for more information.
  • You can also validate your configuration using the online YAML validator.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json
📝 Walkthrough

Walkthrough

This pull request introduces two new PHP_CodeSniffer sniffs to enforce strict comparisons (PreferStrictComparisonSniff and PreferStrictInArraySniff) and updates the phpcs configuration to register them. The existing FrmEntryMeta::getAll method is updated to use strict equality comparison, and the sniffs are configured in the Formidable ruleset.

Changes

Cohort / File(s) Summary
Code Update
classes/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 Comparison
phpcs-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-Array
phpcs-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 Update
phpcs.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

Poem

🐰 With === instead of ==, the code now stands so tall,
In arrays we demand strictness, true for in_array calls,
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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@Crabcyborg Crabcyborg changed the title Add new sniffs to help with strict comparison issues Add new sniffs to help with strict comparison issues that are easy to fix Jan 13, 2026

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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_closer is set), but decremented unconditionally. In malformed or edge-case token streams, this could cause $depth to 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() and hasIgnoreComment() are nearly identical to their counterparts in PreferStrictComparisonSniff. 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

📥 Commits

Reviewing files that changed from the base of the PR and between 8949158 and d6c298c.

📒 Files selected for processing (4)
  • classes/models/FrmEntryMeta.php
  • phpcs-sniffs/Formidable/Sniffs/CodeAnalysis/PreferStrictComparisonSniff.php
  • phpcs-sniffs/Formidable/Sniffs/CodeAnalysis/PreferStrictInArraySniff.php
  • phpcs.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 $limit is 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_EQUAL and T_IS_NOT_EQUAL tokens 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 adding true as 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.

@Crabcyborg
Crabcyborg merged commit bc631fb into master Jan 13, 2026
15 of 16 checks passed
@Crabcyborg
Crabcyborg deleted the add_new_sniffs_to_help_with_strict_comparison_issues branch January 13, 2026 20:54
stephywells pushed a commit that referenced this pull request Apr 4, 2026
…strict_comparison_issues

Add new sniffs to help with strict comparison issues that are easy to fix
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