Skip to content

Remove more useless parentheses#2767

Merged
Crabcyborg merged 2 commits into
masterfrom
remove_more_useless_parantheses
Jan 10, 2026
Merged

Remove more useless parentheses#2767
Crabcyborg merged 2 commits into
masterfrom
remove_more_useless_parantheses

Conversation

@Crabcyborg
Copy link
Copy Markdown
Contributor

This update tweaks the redundant parentheses sniff to catch a few more.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Jan 10, 2026

Walkthrough

The PR removes redundant parentheses around boolean expressions and ternary assignments across many helpers, models, fields, and view files, and updates a PHP_CodeSniffer sniff to treat logical/comparison/coalesce/ternary expressions as simple (no parentheses required).

Changes

Cohort / File(s) Summary
Helpers — boolean expression formatting
classes/helpers/FrmAppHelper.php, classes/helpers/FrmEntriesListHelper.php, classes/helpers/FrmFieldsHelper.php, classes/helpers/FrmStylesHelper.php, classes/helpers/FrmXMLHelper.php
Removed redundant parentheses around boolean comparisons and ternary concatenations in assignments and conditionals. No behavioral changes.
Models — boolean expression formatting
classes/models/FrmCreateFile.php, classes/models/FrmDb.php, classes/models/FrmField.php, classes/models/FrmForm.php
Simplified parenthesized boolean expressions and assignments to unparenthesized equivalents (e.g., credential checks, array flags). Semantics unchanged.
Field models — formatting & ternaries
classes/models/fields/FrmFieldCaptcha.php, classes/models/fields/FrmFieldType.php, classes/models/fields/FrmFieldUserID.php
Removed redundant grouping parentheses around boolean logic and ternary expressions in validation and attribute code. No control-flow changes.
Views — conditional expression simplification
classes/views/frm-fields/back-end/settings.php
Removed outer parentheses from the initial conditional in has_validation, relying on operator precedence; behavior unchanged.
Table/HTML generator — ternary formatting
classes/models/FrmTableHTMLGenerator.php
Removed parentheses around a returned ternary expression. No behavioral changes.
Entry validation — return expression formatting
classes/models/FrmEntryValidate.php
Removed outer parentheses from returned boolean expression. Function identical.
PHPCS sniff — RedundantParenthesesSniff
phpcs-sniffs/Formidable/Sniffs/CodeAnalysis/RedundantParenthesesSniff.php
Relaxed isSimpleExpression() logic: added T_RETURN to preceding tokens, renamed token set, and simplified complexity checks so logical/comparison/coalesce/ternary operators no longer mark an expression complex—only arithmetic operators do. Consolidated early returns.

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 'Remove more useless parantheses' is directly related to the main changeset, which removes redundant parentheses across multiple PHP files and improves a PHP code sniffer.
Description check ✅ Passed The description clearly relates to the changeset, mentioning that it tweaks the redundant parentheses sniff to catch more cases, which aligns with the actual changes made.
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: 0

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
phpcs-sniffs/Formidable/Sniffs/CodeAnalysis/RedundantParenthesesSniff.php (1)

141-194: Don’t auto-fix parentheses when the expression contains and/or/xor (PHP operator precedence changes semantics).

With the new unconditional return true, this sniff can “fix” cases like $x = ( $a or $b ); into $x = $a or $b;, which is not equivalent in PHP because or/and/xor have lower precedence than assignment operators. This is a correctness bug for both = and compound assignments (+=, .= etc.).
Also, the comment at Line 192 mentions only ;/, but the code also allows ).

Proposed fix
 private function isSimpleExpression( File $phpcsFile, $openParen, $closeParen ) {
 	$tokens = $phpcsFile->getTokens();

 	// Count operators that would make this a complex expression.
 	$hasCoalesce      = false;
 	$hasTernary       = false;
-	$hasLogicalOp     = false;
+	$hasLogicalOp     = false;
+	$hasLowPrecLogic  = false; // and/or/xor
 	$hasComparisonOp  = false;
 	$hasArithmeticOp  = false;
 	$nestedParenDepth = 0;

 	for ( $i = $openParen + 1; $i < $closeParen; $i++ ) {
 		$code = $tokens[ $i ]['code'];
@@
 		// Check for various operators.
 		if ( $code === T_COALESCE ) {
 			$hasCoalesce = true;
 		} elseif ( $code === T_INLINE_THEN || $code === T_INLINE_ELSE ) {
 			$hasTernary = true;
-		} elseif ( in_array( $code, array( T_BOOLEAN_AND, T_BOOLEAN_OR, T_LOGICAL_AND, T_LOGICAL_OR, T_LOGICAL_XOR ), true ) ) {
+		} elseif ( in_array( $code, array( T_LOGICAL_AND, T_LOGICAL_OR, T_LOGICAL_XOR ), true ) ) {
+			$hasLowPrecLogic = true;
+			$hasLogicalOp    = true;
+		} elseif ( in_array( $code, array( T_BOOLEAN_AND, T_BOOLEAN_OR ), true ) ) {
 			$hasLogicalOp = true;
 		} elseif ( in_array( $code, array( T_IS_EQUAL, T_IS_NOT_EQUAL, T_IS_IDENTICAL, T_IS_NOT_IDENTICAL, T_LESS_THAN, T_GREATER_THAN, T_IS_SMALLER_OR_EQUAL, T_IS_GREATER_OR_EQUAL, T_SPACESHIP ), true ) ) {
 			$hasComparisonOp = true;
 		} elseif ( in_array( $code, array( T_PLUS, T_MINUS, T_MULTIPLY, T_DIVIDE, T_MODULUS ), true ) ) {
 			$hasArithmeticOp = true;
 		}
 	}

-	// If there are arithmetic operators, it might need parentheses for precedence.
-	if ( $hasArithmeticOp ) {
+	// If there are arithmetic operators, it might need parentheses for precedence.
+	// If there are and/or/xor operators, parentheses are required in assignments due to precedence.
+	if ( $hasArithmeticOp || $hasLowPrecLogic ) {
 		return false;
 	}

-	// Logical and comparison operators don't need parentheses in a simple assignment.
-	// The parentheses are only needed when there's an operator precedence issue outside.
-	// Since we already checked that this is followed by ; or , the parentheses are redundant.
+	// `&&`/`||` and comparisons don't need parentheses in this assignment context.
+	// Note: caller allows `;`, `,`, or `)` after the closing parenthesis.
 	return true;
 }
🧹 Nitpick comments (1)
classes/helpers/FrmXMLHelper.php (1)

955-967: Parentheses removal is safe here; consider a small readability tweak (optional).

Line 963 preserves behavior (comparisons bind tighter than &&). Optional: assign $custom_style = $form['options']['custom_style']; before the comparison to reduce repetition.

📜 Review details

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between f2a5c17 and 669e30d.

📒 Files selected for processing (14)
  • classes/helpers/FrmAppHelper.php
  • classes/helpers/FrmEntriesListHelper.php
  • classes/helpers/FrmFieldsHelper.php
  • classes/helpers/FrmStylesHelper.php
  • classes/helpers/FrmXMLHelper.php
  • classes/models/FrmCreateFile.php
  • classes/models/FrmDb.php
  • classes/models/FrmField.php
  • classes/models/FrmForm.php
  • classes/models/fields/FrmFieldCaptcha.php
  • classes/models/fields/FrmFieldType.php
  • classes/models/fields/FrmFieldUserID.php
  • classes/views/frm-fields/back-end/settings.php
  • phpcs-sniffs/Formidable/Sniffs/CodeAnalysis/RedundantParenthesesSniff.php
🧰 Additional context used
🧬 Code graph analysis (4)
classes/views/frm-fields/back-end/settings.php (1)
classes/models/FrmField.php (2)
  • FrmField (6-1619)
  • is_option_true (1399-1404)
classes/helpers/FrmFieldsHelper.php (1)
classes/helpers/FrmAppHelper.php (1)
  • checked (2417-2421)
classes/models/fields/FrmFieldType.php (2)
classes/models/FrmField.php (2)
  • FrmField (6-1619)
  • is_read_only (1369-1372)
classes/helpers/FrmAppHelper.php (2)
  • FrmAppHelper (6-5024)
  • is_admin (582-591)
classes/models/FrmForm.php (1)
classes/helpers/FrmAppHelper.php (2)
  • FrmAppHelper (6-5024)
  • is_admin (582-591)
⏰ 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). (9)
  • GitHub Check: PHP 7.4 tests in WP 6.9
  • GitHub Check: PHP 8 tests in WP 6.9
  • GitHub Check: Cypress
  • GitHub Check: PHP 7.4 tests in WP 6.9
  • GitHub Check: Cypress
  • GitHub Check: PHP 7.4 tests in WP 6.9
  • GitHub Check: PHP 8 tests in WP 6.9
  • GitHub Check: PHP 8 tests in WP 6.9
  • GitHub Check: Cypress
🔇 Additional comments (16)
classes/models/FrmDb.php (1)

98-98: Parentheses removal is safe and aligns with best practices.

The redundant parentheses around the logical expression are safely removed. The ! operator has higher precedence than &&, and multiple && operators are left-associative, so the evaluation order is unambiguous without extra grouping.

classes/models/fields/FrmFieldCaptcha.php (1)

328-328: Ternary parentheses removal is safe and improves code clarity.

The redundant parentheses around a simple ternary expression are safely removed. With no nested ternaries and a clear single-level condition, the parentheses were purely stylistic and their removal improves readability.

classes/helpers/FrmAppHelper.php (1)

3166-3166: Comparison parentheses removal is safe and maintains clarity.

The redundant parentheses around a simple comparison expression are safely removed. The !== operator has clear precedence, and the intent (checking whether a time string differs from midnight) remains immediately apparent.

classes/helpers/FrmStylesHelper.php (1)

1091-1091: LGTM! Clean removal of redundant parentheses.

The parentheses around the comparison were unnecessary. The simplified form is more readable and maintains the same behavior.

classes/helpers/FrmFieldsHelper.php (2)

697-697: LGTM! Parentheses correctly removed from boolean assignment.

The outer parentheses were redundant. The expression correctly evaluates whether a field option should be checked based on the default value.


736-736: LGTM! Clean ternary expression.

Removing parentheses around the ternary condition follows standard PHP conventions and improves readability.

classes/helpers/FrmEntriesListHelper.php (1)

250-250: LGTM! Simplified boolean assignment.

The parentheses were unnecessary for this simple comparison. The change improves readability.

classes/models/FrmCreateFile.php (1)

281-282: LGTM! Clean boolean assignments.

The outer parentheses were redundant for these boolean assignments. The && operator precedence ensures the expressions evaluate correctly without them.

classes/models/fields/FrmFieldUserID.php (1)

68-68: LGTM! Redundant parentheses removed.

The outer parentheses were unnecessary for this boolean assignment. The && operators evaluate left-to-right as expected, and the phpcs ignore comment is appropriately placed for the nonce verification check.

classes/models/FrmField.php (2)

1025-1037: LGTM: $should_get_subforms simplification keeps the same logic.

Line 1033 is clearer and precedence-safe (&& chain).


1588-1604: LGTM: is_field_type boolean flattening is equivalent and readable.

Lines 1592-1595 keep the same truth table; parentheses still make the data/lookup/product cases explicit.

classes/views/frm-fields/back-end/settings.php (1)

478-484: LGTM: $has_validation remains correct and readable.

Line 482 is precedence-safe, and the grouped ( $display['invalid'] && ! $hidden_invalid ) keeps intent clear.

classes/models/FrmForm.php (2)

368-383: LGTM: $is_settings_page parentheses removal is behavior-preserving.

Line 375 is still clear and safe (no precedence ambiguity).


1290-1294: LGTM: show_submit simplification is safe and clearer.

Line 1292’s && chain is unambiguous and matches prior intent.

classes/models/fields/FrmFieldType.php (2)

822-829: LGTM: $use_style simplification is short-circuit safe.

Line 823 won’t read custom_style when it’s unset due to || short-circuiting.


1464-1476: LGTM: $readonly simplification is behavior-preserving.

Line 1465 keeps the same condition and reads a bit cleaner.

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 (1)
phpcs-sniffs/Formidable/Sniffs/CodeAnalysis/RedundantParenthesesSniff.php (1)

186-194: Logic looks correct, minor comment suggestion.

The simplified logic is sound—arithmetic operators still require parentheses for precedence, while logical, comparison, coalesce, and ternary operators can safely omit outer parentheses in assignments and returns.

💡 Optional: Update comment to match expanded scope

Consider updating the comment on lines 191-193 to explicitly mention returns:

-// Logical and comparison operators don't need parentheses in a simple assignment.
+// Logical and comparison operators don't need parentheses in a simple assignment or return.

This would align with the addition of T_RETURN on line 69.

📜 Review details

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 669e30d and 4d5a272.

📒 Files selected for processing (4)
  • classes/models/FrmEntryValidate.php
  • classes/models/FrmField.php
  • classes/models/FrmTableHTMLGenerator.php
  • phpcs-sniffs/Formidable/Sniffs/CodeAnalysis/RedundantParenthesesSniff.php
✅ Files skipped from review due to trivial changes (1)
  • classes/models/FrmTableHTMLGenerator.php
🚧 Files skipped from review as they are similar to previous changes (1)
  • classes/models/FrmField.php
⏰ 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: PHP 8 tests in WP 6.9
  • GitHub Check: Cypress
  • GitHub Check: PHP 7.4 tests in WP 6.9
🔇 Additional comments (2)
classes/models/FrmEntryValidate.php (1)

683-683: LGTM! Clean formatting improvement.

The removal of outer parentheses around the boolean expression is correct and improves readability without changing behavior.

phpcs-sniffs/Formidable/Sniffs/CodeAnalysis/RedundantParenthesesSniff.php (1)

61-72: LGTM! Good addition to handle return statements.

Adding T_RETURN to the list of valid preceding tokens is a logical extension that will catch redundant parentheses in return statements like return ( $value );. The variable rename to $validPrecedingTokens is also more accurate.

@Crabcyborg Crabcyborg merged commit 46ab757 into master Jan 10, 2026
15 of 16 checks passed
@Crabcyborg Crabcyborg deleted the remove_more_useless_parantheses branch January 10, 2026 02:07
@Crabcyborg Crabcyborg changed the title Remove more useless parantheses Remove more useless parentheses Jan 10, 2026
stephywells pushed a commit that referenced this pull request Apr 4, 2026
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