Skip to content

Add new custom sniffs for catching redundant truthy and empty checks#2785

Merged
Crabcyborg merged 2 commits into
masterfrom
new_sniffs
Jan 13, 2026
Merged

Add new custom sniffs for catching redundant truthy and empty checks#2785
Crabcyborg merged 2 commits into
masterfrom
new_sniffs

Conversation

@Crabcyborg
Copy link
Copy Markdown
Contributor

@Crabcyborg Crabcyborg commented Jan 13, 2026

The empty sniff isn't caught in Lite, but it helped catch some code in Pro (https://github.com/Strategy11/formidable-pro/pull/6193).

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Jan 13, 2026

Walkthrough

This pull request introduces two new PHP_CodeSniffer sniffs for detecting and auto-fixing redundant code patterns, then applies the simplifications to two existing codebase files. The sniffs detect redundant truthiness checks before string comparisons and redundant isset/empty patterns, offering automatic fixes. Two controller/helper methods are simplified to remove explicit truthiness checks.

Changes

Cohort / File(s) Summary
Code Simplifications Applied
classes/controllers/FrmUsageController.php, classes/helpers/FrmAppHelper.php
Removed redundant explicit truthiness checks before string comparisons in is_forms_list_page() and is_preview_page() methods; control flow and behavior unchanged.
New Sniff: Redundant Truthy Before String Comparison
phpcs-sniffs/Formidable/Sniffs/CodeAnalysis/RedundantTruthyBeforeStringComparisonSniff.php
Implements sniff to detect patterns like $var && $var === 'string' and auto-fix to $var === 'string'. Includes token parsing, variable validation, and expression extraction helpers.
New Sniff: Simplify isset/empty Checks
phpcs-sniffs/Formidable/Sniffs/CodeAnalysis/SimplifyIssetEmptyCheckSniff.php
Implements sniff to detect isset($var) && empty($var) patterns and auto-fix to isset($var) && ! $var. Includes parentheses content extraction helper.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately describes the main changes: adding new custom sniffs for detecting redundant truthy and empty checks, which aligns with the two new sniff classes introduced.
Description check ✅ Passed The description explains the context for the changes by referencing how the empty sniff helped catch code in Pro, which is related to the sniff implementations in this PR.
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.

@Crabcyborg Crabcyborg merged commit aac521e into master Jan 13, 2026
15 of 16 checks passed
@Crabcyborg Crabcyborg deleted the new_sniffs branch January 13, 2026 02:51
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: 0

🧹 Nitpick comments (2)
phpcs-sniffs/Formidable/Sniffs/CodeAnalysis/RedundantTruthyBeforeStringComparisonSniff.php (2)

48-49: Remove unused variable.

The $tokens variable is declared but never used in the process() method. Each helper method fetches its own $tokens from $phpcsFile->getTokens().

Suggested fix
 	public function process( File $phpcsFile, $stackPtr ) {
-		$tokens = $phpcsFile->getTokens();
-
 		// Find the expression before &&.
 		$beforeAnd = $this->getExpressionBefore( $phpcsFile, $stackPtr );

247-247: Consider caching the token count.

count($tokens) is evaluated on every loop iteration. While performance impact is minimal for PHPCS sniffs, caching the count is a cleaner pattern.

Suggested fix
-		for ( $i = $start; $i < count( $tokens ); $i++ ) {
+		$tokenCount = count( $tokens );
+		for ( $i = $start; $i < $tokenCount; $i++ ) {
📜 Review details

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 567a4be and ccd2b50.

⛔ Files ignored due to path filters (1)
  • phpcs.xml is excluded by !**/*.xml
📒 Files selected for processing (4)
  • classes/controllers/FrmUsageController.php
  • classes/helpers/FrmAppHelper.php
  • phpcs-sniffs/Formidable/Sniffs/CodeAnalysis/RedundantTruthyBeforeStringComparisonSniff.php
  • phpcs-sniffs/Formidable/Sniffs/CodeAnalysis/SimplifyIssetEmptyCheckSniff.php
🧰 Additional context used
🧬 Code graph analysis (1)
phpcs-sniffs/Formidable/Sniffs/CodeAnalysis/SimplifyIssetEmptyCheckSniff.php (1)
phpcs-sniffs/Formidable/Sniffs/CodeAnalysis/RedundantTruthyBeforeStringComparisonSniff.php (2)
  • register (36-38)
  • process (48-115)
🪛 PHPMD (2.15.0)
phpcs-sniffs/Formidable/Sniffs/CodeAnalysis/RedundantTruthyBeforeStringComparisonSniff.php

49-49: Avoid unused local variables such as '$tokens'. (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). (2)
  • GitHub Check: PHP 7.4 tests in WP 6.9
  • GitHub Check: Cypress
🔇 Additional comments (7)
classes/controllers/FrmUsageController.php (1)

121-122: LGTM!

The simplification is correct. When $form_type is falsy (empty string, null, etc.), it cannot equal 'published', so the explicit truthy check was redundant. This aligns with the new RedundantTruthyBeforeStringComparisonSniff being introduced in this PR.

classes/helpers/FrmAppHelper.php (1)

542-547: LGTM!

The simplification correctly removes the redundant truthy check. If $pagenow is falsy, the strict comparison to 'admin-ajax.php' will return false anyway.

phpcs-sniffs/Formidable/Sniffs/CodeAnalysis/SimplifyIssetEmptyCheckSniff.php (3)

1-36: LGTM - Well-structured sniff implementation.

The sniff correctly detects the isset($x) && empty($x) pattern. The semantic equivalence is valid because after isset() confirms the variable exists, empty() only checks for falsiness, which is equivalent to ! $x.


46-129: LGTM!

The process method is well-structured with proper guard clauses and early returns. The fix logic correctly replaces empty($var) with ! $var by substituting the empty token and removing the parentheses content.


140-168: LGTM!

The getParenthesesContent helper correctly normalizes content by stripping whitespace, enabling accurate comparison of variables with different spacing patterns.

phpcs-sniffs/Formidable/Sniffs/CodeAnalysis/RedundantTruthyBeforeStringComparisonSniff.php (2)

430-440: Good edge case handling.

The sniff correctly skips empty strings and numeric strings. For empty string comparisons ($var === ''), the truthy check is semantically meaningful. For numeric strings like '0', PHP's type juggling makes truthy checks potentially relevant.


396-409: LGTM - Property/array access detection.

The sniff correctly ensures only simple variables are matched by checking for object operators (->, ?->), array brackets ([), and static access (::) after the variable. This prevents false positives on patterns like $var['key'] && $var['key'] === 'string' where the truthy check may not be redundant.

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