Skip to content

New sniff to prefer positive ternaries#2864

Merged
Crabcyborg merged 4 commits into
masterfrom
new_sniff_to_prefer_positive_ternaries
Jan 22, 2026
Merged

New sniff to prefer positive ternaries#2864
Crabcyborg merged 4 commits into
masterfrom
new_sniff_to_prefer_positive_ternaries

Conversation

@Crabcyborg
Copy link
Copy Markdown
Contributor

@Crabcyborg Crabcyborg commented Jan 22, 2026

Summary by CodeRabbit

  • Refactor

    • Improved consistency of conditional logic patterns across the codebase for better maintainability
  • Chores

    • Added new code quality enforcement tool to detect and standardize ternary condition patterns for developers

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

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Jan 22, 2026

Warning

Rate limit exceeded

@Crabcyborg has exceeded the limit for the number of commits that can be reviewed per hour. Please wait 2 minutes and 56 seconds before requesting another review.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

📝 Walkthrough

Walkthrough

The PR introduces a new PHP_CodeSniffer sniff that detects and automatically fixes negated ternary conditions by suggesting a positive form. The sniff is then systematically applied across the codebase, flipping multiple instances of negated ternary operators and conditional logic to improve code readability.

Changes

Cohort / File(s) Summary
PHP_CodeSniffer Sniff (New)
phpcs-sniffs/Formidable/Sniffs/CodeAnalysis/PreferPositiveTernaryConditionSniff.php, phpcs-sniffs/Formidable/ruleset.xml
Added new sniff that detects patterns like ! condition ? A : B and proposes positive form condition ? B : A with auto-fix support. Included in ruleset.
Business Logic Files
classes/factories/FrmFieldFactory.php, classes/helpers/FrmAppHelper.php, classes/models/FrmEntryValidate.php, classes/views/styles/components/FrmSliderStyleComponent.php
Inverted negated ternary conditions and logical checks. Examples: return expression flipped, option value extraction reversed, unit-detection logic inverted for float casting.
View Files – Entry & Form Actions
classes/views/frm-entries/list.php, classes/views/frm-form-actions/_action_icon.php, classes/views/frm-form-actions/form_action.php, stripe/views/lists/list.php
Flipped publish/visibility conditions and CSS class conditionals. Now uses positive checks (e.g., should_show ? true : false instead of ! should_show ? false : true).
Settings & UI Views
classes/views/frm-settings/misc.php, classes/views/shared/admin-cta.php
Inverted class assignment and visibility toggle logic to use positive conditions.

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~60 minutes

Possibly related PRs

Poem

🐰 A negation's flip, a sniff so fine,
Transforms the twisted ! to something divine.
From ! this ? that : there we dance away,
To this ? there : that in cleaner array!
✨ Positive vibes through every file,
Making the code smile all the while! 🌟

🚥 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 PR title accurately describes the main change: a new PHP CodeSniffer sniff to prefer positive ternary conditions, which is the primary addition in this changeset.
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.


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: 4

🤖 Fix all issues with AI agents
In `@classes/helpers/FrmAppHelper.php`:
- Line 1395: The ternary branches were reversed when the negation was removed
for computing $class using str_contains($icon, ' '): locate the assignment that
sets $class (uses str_contains and $icon in FrmAppHelper.php) and swap the
ternary outcomes so icons that do NOT contain a space get the prefixed class
(i.e., preserve the original behavior by returning ' ' . $icon for the no-space
case and '' for the space-containing case); alternatively, restore the original
logical sense by reinserting the negation on str_contains and keep the current
branch ordering.

In
`@phpcs-sniffs/Formidable/Sniffs/CodeAnalysis/PreferPositiveTernaryConditionSniff.php`:
- Around line 1-10: Add the missing blank line before the first statement
following the file-level docblock in the
Formidable_Sniffs_CodeAnalysis_PreferPositiveTernaryConditionSniff file: ensure
there's a blank line between the closing */ of the header comment and the next
statement (e.g., the class declaration or any php statement that follows) or
simply run php-cs-fixer fix to apply the blank_line_before_statement rule
automatically for the
Formidable_Sniffs_CodeAnalysis_PreferPositiveTernaryConditionSniff code.
- Line 473: The method applyFix currently declares an unused parameter
$conditionStart; remove $conditionStart from the applyFix signature so it
becomes applyFix(File $phpcsFile, $notToken, $ternaryOperator, $colonOperator,
$ternaryEnd, $thenContent, $elseContent) and update every call site that passes
$conditionStart to stop supplying that argument (ensure calls to applyFix match
the new parameter list). Also update any related docblocks or PHPDoc for
applyFix to reflect the removed parameter.

In `@stripe/views/action-settings/payments-options.php`:
- Around line 103-106: The visibility logic for gateways in the foreach loop is
inverted causing all gateways to be hidden for one-time payments; update the
construction of $gateway_classes (in the loop over $gateways where
$gateway_name/$gateway and $gateway_id are used) so that 'frm_hidden' is only
appended when the form action type is 'recurring' AND the gateway does NOT
support recurring payments (i.e. check $form_action->post_content['type'] ===
'recurring' && !$gateway['recurring']), leaving it visible for one-time
payments.
🧹 Nitpick comments (2)
phpcs-sniffs/Formidable/Sniffs/CodeAnalysis/PreferPositiveTernaryConditionSniff.php (2)

316-316: Avoid calling count() in loop condition.

Calling count($tokens) on every iteration is inefficient. Cache the count before the loop.

Proposed fix
+		$tokenCount = count( $tokens );
 
-		for ( $i = $start; $i < count( $tokens ); $i++ ) {
+		for ( $i = $start; $i < $tokenCount; $i++ ) {

The same issue exists at line 372 in findTernaryEnd.


376-390: Redundant condition checks inside parenthesis tracking.

Inside the if ( $code === T_OPEN_PARENTHESIS ) block (line 376), the check $tokens[ $i ]['code'] !== T_WHITESPACE at line 378 is always true because $code is T_OPEN_PARENTHESIS. Similarly for lines 384-390.

Proposed fix
 			if ( $code === T_OPEN_PARENTHESIS ) {
 				++$parenDepth;
-				if ( $tokens[ $i ]['code'] !== T_WHITESPACE ) {
-					$end = $i;
-				}
+				$end = $i;
 				continue;
 			}
 
 			if ( $code === T_CLOSE_PARENTHESIS ) {
 				if ( $parenDepth > 0 ) {
 					--$parenDepth;
-					if ( $tokens[ $i ]['code'] !== T_WHITESPACE ) {
-						$end = $i;
-					}
+					$end = $i;
 					continue;
 				}

Comment thread classes/helpers/FrmAppHelper.php
Comment thread stripe/views/action-settings/payments-options.php
@Crabcyborg Crabcyborg merged commit c94ccee into master Jan 22, 2026
14 of 15 checks passed
@Crabcyborg Crabcyborg deleted the new_sniff_to_prefer_positive_ternaries branch January 22, 2026 13:58
stephywells pushed a commit that referenced this pull request Apr 4, 2026
…_ternaries

New sniff to prefer positive ternaries
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