Skip to content

Improve redundant paren sniff and remove is_array checks before stric…#2845

Merged
Crabcyborg merged 2 commits into
masterfrom
improve_redundant_paran_sniff_and_remove_is_array_checks_before_strict_array_comparison
Jan 20, 2026
Merged

Improve redundant paren sniff and remove is_array checks before stric…#2845
Crabcyborg merged 2 commits into
masterfrom
improve_redundant_paran_sniff_and_remove_is_array_checks_before_strict_array_comparison

Conversation

@Crabcyborg
Copy link
Copy Markdown
Contributor

@Crabcyborg Crabcyborg commented Jan 20, 2026

…t array comparison

Summary by CodeRabbit

  • New Features

    • Added a code analysis rule that detects and auto-fixes redundant array-check patterns.
  • Refactor

    • Simplified conditional logic in field entry handling and styling.
    • Removed unnecessary parentheses from comparison checks for clearer logic.
  • Chores

    • Improved code-analysis tooling to provide safer, atomic fixes and broader redundancy detection.

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

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Jan 20, 2026

📝 Walkthrough

Walkthrough

Adds a new PHPCS sniff that detects and auto-fixes redundant is_array(...) checks before empty-array comparisons, updates the ruleset to register it, enhances redundant-parentheses detection with a helper, and applies three small code edits removing redundant checks/parentheses.

Changes

Cohort / File(s) Summary
New PHPCS Sniff
phpcs-sniffs/Formidable/Sniffs/CodeAnalysis/RedundantIsArrayBeforeEmptyArrayCheckSniff.php
New 322-line sniff implementing register() and process() plus helpers to detect patterns like is_array($v) && $v === array() / is_array($v) && array() === $v, report fixable errors, and remove the redundant is_array(...) && via fixer.
Sniff Enhancement
phpcs-sniffs/Formidable/Sniffs/CodeAnalysis/RedundantParenthesesSniff.php
Added private isSimpleComparisonExpression() helper and integrated it into isRedundantNegatedFunctionCall() to treat simple comparison expressions as redundant-parentheses candidates.
Rule Registration
phpcs-sniffs/Formidable/ruleset.xml
Added Formidable.CodeAnalysis.RedundantIsArrayBeforeEmptyArrayCheck to the Code Analysis ruleset.
Small Source Edits
classes/models/FrmEntry.php, classes/models/FrmStyle.php, classes/views/frm-fields/front-end/radio-field.php
Three one-line edits: removed redundant is_array() check in FrmEntry.php and simplified parenthesis in conditional checks in FrmStyle.php and radio-field.php.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Possibly related PRs

Poem

🐰 I hopped through tokens, sniffed a line,

"is_array? nah — the array check'll shine!"
I nibbled parentheses, pruned a bit tidy,
Now code hops light and bankers bite kindly. 🥕✨

🚥 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 accurately summarizes the main changes: improving the redundant parentheses sniff and removing redundant is_array checks before strict array comparisons across multiple files.
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

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.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 2

🤖 Fix all issues with AI agents
In
`@phpcs-sniffs/Formidable/Sniffs/CodeAnalysis/RedundantIsArrayBeforeEmptyArrayCheckSniff.php`:
- Around line 45-105: The sniff currently matches any T_STRING with content
"is_array" including method/namespace usages; before proceeding in process( File
$phpcsFile, $stackPtr ) add a guard that fetches the previous non-whitespace
token (use $phpcsFile->findPrevious with $stackPtr - 1) and returns early if
that token code is T_OBJECT_OPERATOR, T_DOUBLE_COLON or T_NS_SEPARATOR (and also
return if previous token is T_STRING followed by T_NS_SEPARATOR to cover
namespaced qualifiers) so only the global function call is handled; keep the
rest of the logic unchanged and reference $stackPtr, $tokens and process() when
implementing the check.

In `@phpcs-sniffs/Formidable/Sniffs/CodeAnalysis/RedundantParenthesesSniff.php`:
- Around line 235-242: The logical operator detection in
RedundantParenthesesSniff.php is missing T_LOGICAL_XOR, so add T_LOGICAL_XOR to
the in_array list that increments $logicalCount (the block that checks for
T_BOOLEAN_AND, T_BOOLEAN_OR, T_LOGICAL_AND, T_LOGICAL_OR) to ensure `xor` is
treated as a logical operator and prevents unsafe removal of parentheses in
methods handling redundant parentheses detection.
🧹 Nitpick comments (1)
phpcs-sniffs/Formidable/Sniffs/CodeAnalysis/RedundantIsArrayBeforeEmptyArrayCheckSniff.php (1)

300-319: Drop unused parameters in reportAndFix to satisfy PHPMD.
$isArrayEnd and $varContent are unused; tightening the signature removes static-analysis noise.

♻️ Proposed refactor
-				$this->reportAndFix( $phpcsFile, $stackPtr, $closeParen, $afterClose, $varContent );
+				$this->reportAndFix( $phpcsFile, $stackPtr, $afterClose );
@@
-			$this->reportAndFix( $phpcsFile, $stackPtr, $closeParen, $afterClose, $varContent );
+			$this->reportAndFix( $phpcsFile, $stackPtr, $afterClose );
@@
-	private function reportAndFix( File $phpcsFile, $isArrayStart, $isArrayEnd, $andToken, $varContent ) {
+	private function reportAndFix( File $phpcsFile, $isArrayStart, $andToken ) {

@Crabcyborg Crabcyborg merged commit 18c6300 into master Jan 20, 2026
15 of 16 checks passed
@Crabcyborg Crabcyborg deleted the improve_redundant_paran_sniff_and_remove_is_array_checks_before_strict_array_comparison branch January 20, 2026 16:38
stephywells pushed a commit that referenced this pull request Apr 4, 2026
…f_and_remove_is_array_checks_before_strict_array_comparison

Improve redundant paren sniff and remove is_array checks before stric…
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