Skip to content

New sniff to remove unnecessary white space in small if conditions#2769

Merged
Crabcyborg merged 1 commit into
masterfrom
new_sniff_to_remove_unnecessary_whitespace_in_small_if_conditions
Jan 10, 2026
Merged

New sniff to remove unnecessary white space in small if conditions#2769
Crabcyborg merged 1 commit into
masterfrom
new_sniff_to_remove_unnecessary_whitespace_in_small_if_conditions

Conversation

@Crabcyborg
Copy link
Copy Markdown
Contributor

No description provided.

@Crabcyborg Crabcyborg added this to the 6.27 milestone Jan 10, 2026
@Crabcyborg Crabcyborg merged commit 4f30f58 into master Jan 10, 2026
35 of 38 checks passed
@Crabcyborg Crabcyborg deleted the new_sniff_to_remove_unnecessary_whitespace_in_small_if_conditions branch January 10, 2026 02:56
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Jan 10, 2026

Walkthrough

This pull request introduces a new PHP_CodeSniffer sniff that enforces removal of blank lines within short if blocks, while simultaneously applying whitespace cleanup across multiple existing files to remove extraneous blank lines. The changes are entirely whitespace-focused with no modifications to runtime logic or control flow.

Changes

Cohort / File(s) Summary
Whitespace cleanup in controller and helper classes
classes/controllers/FrmAddonsController.php, classes/controllers/FrmFormsController.php, classes/helpers/FrmAppHelper.php, classes/helpers/FrmEntriesListHelper.php, classes/helpers/FrmFormsHelper.php, classes/helpers/FrmXMLHelper.php
Removed extraneous blank lines from various methods; no behavioral changes to logic or control flow
Whitespace cleanup in model classes
classes/models/FrmAddon.php, classes/models/FrmCreateFile.php, classes/models/FrmEntryMeta.php, classes/models/FrmEntryValidate.php, classes/models/FrmFieldValue.php, classes/models/FrmStyle.php
Removed extraneous blank lines and adjusted whitespace alignment within methods; no changes to functional behavior
New PHPCS sniff for blank line enforcement
phpcs-sniffs/Formidable/Sniffs/WhiteSpace/NoBlankLineInShortIfSniff.php
New sniff class (+137 lines) that detects and fixes blank lines inside short if/elseif/else blocks; implements token listening and automatic fixing via newline removal while preserving indentation

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Possibly related PRs

🚥 Pre-merge checks | ✅ 2 | ❌ 1
❌ Failed checks (1 inconclusive)
Check name Status Explanation Resolution
Description check ❓ Inconclusive No pull request description was provided by the author, making it impossible to evaluate whether the description relates to the changeset. Add a pull request description explaining the purpose and impact of the new sniff, including why this whitespace enforcement is necessary.
✅ Passed checks (2 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately describes the main change: introducing a new PHP_CodeSniffer sniff to enforce removal of blank lines in short if conditions.
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/WhiteSpace/NoBlankLineInShortIfSniff.php:
- Around line 113-117: The error text is inconsistent with the check in
NoBlankLineInShortIfSniff: the sniff only triggers when $bodyLines === 3 but the
message says "3 lines or less"; update either the condition or the message to
match. Easiest fix: change the message passed to $phpcsFile->addFixableError in
NoBlankLineInShortIfSniff to "Remove blank line inside short if block (exactly 3
lines)." Alternatively, if the intention is to cover blocks with up to 3 lines,
adjust the $bodyLines check (currently comparing $bodyLines !== 3) to $bodyLines
<= 3 so the behavior matches the original message.
- Around line 125-128: The fix fails when the whitespace token contains a blank
line with indentation because substr_count( $content, "\n" ) detects 2+ newlines
but the preg_replace( "/\n\n/", "\n", $content, 1 ) only matches consecutive
newlines; update the preg_replace call in NoBlankLineInShortIfSniff (operating
on $content and $blankLineToken) to match a newline, any intervening
spaces/tabs, then another newline (use a regex like newline + optional [ \t]* +
newline), capture the indentation and replace with a single newline plus the
captured indentation (still with a limit of 1) so indented blank lines are
collapsed correctly.
🧹 Nitpick comments (1)
phpcs-sniffs/Formidable/Sniffs/WhiteSpace/NoBlankLineInShortIfSniff.php (1)

92-107: Potential edge case: blank lines with indentation may be missed.

If PHPCS tokenizes a blank line as separate tokens with indentation whitespace between them (e.g., \n \n), the intermediate spaces would reset lastWasNewline to false, causing the blank line to go undetected.

Consider checking if the intermediate token is only horizontal whitespace (spaces/tabs) rather than resetting on any non-newline:

♻️ Suggested improvement
 		for ( $i = $scopeOpener + 1; $i < $scopeCloser; $i++ ) {
 			if ( $tokens[ $i ]['code'] === T_WHITESPACE && $tokens[ $i ]['content'] === "\n" ) {
 				if ( $lastWasNewline ) {
 					$blankLineToken = $i;
 					break;
 				}
 				$lastWasNewline = true;
-			} else {
+			} elseif ( $tokens[ $i ]['code'] !== T_WHITESPACE || strpos( $tokens[ $i ]['content'], "\n" ) !== false ) {
+				// Reset only if it's not horizontal-only whitespace
 				$lastWasNewline = false;
 			}
 		}
📜 Review details

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 0a1a156 and 533e20d.

⛔ Files ignored due to path filters (1)
  • phpcs.xml is excluded by !**/*.xml
📒 Files selected for processing (13)
  • classes/controllers/FrmAddonsController.php
  • classes/controllers/FrmFormsController.php
  • classes/helpers/FrmAppHelper.php
  • classes/helpers/FrmEntriesListHelper.php
  • classes/helpers/FrmFormsHelper.php
  • classes/helpers/FrmXMLHelper.php
  • classes/models/FrmAddon.php
  • classes/models/FrmCreateFile.php
  • classes/models/FrmEntryMeta.php
  • classes/models/FrmEntryValidate.php
  • classes/models/FrmFieldValue.php
  • classes/models/FrmStyle.php
  • phpcs-sniffs/Formidable/Sniffs/WhiteSpace/NoBlankLineInShortIfSniff.php
💤 Files with no reviewable changes (9)
  • classes/controllers/FrmAddonsController.php
  • classes/models/FrmAddon.php
  • classes/models/FrmCreateFile.php
  • classes/models/FrmEntryMeta.php
  • classes/helpers/FrmXMLHelper.php
  • classes/helpers/FrmAppHelper.php
  • classes/helpers/FrmFormsHelper.php
  • classes/helpers/FrmEntriesListHelper.php
  • classes/controllers/FrmFormsController.php
🧰 Additional context used
🧬 Code graph analysis (1)
classes/models/FrmFieldValue.php (1)
classes/factories/FrmFieldFactory.php (2)
  • FrmFieldFactory (9-141)
  • get_field_object (59-65)
⏰ 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 8 tests in WP 6.9
  • GitHub Check: PHP 7.4 tests in WP 6.9
  • GitHub Check: Cypress
  • GitHub Check: Cypress
  • GitHub Check: PHP 8 tests in WP 6.9
  • GitHub Check: PHP 7.4 tests in WP 6.9
  • GitHub Check: PHP 7.4 tests in WP 6.9
  • GitHub Check: PHP 8 tests in WP 6.9
  • GitHub Check: Cypress
🔇 Additional comments (6)
classes/models/FrmFieldValue.php (1)

225-229: LGTM – whitespace-only cleanup in short if block.

The change removes unnecessary blank lines within the conditional block, aligning with the PR's intent to enforce cleaner formatting in short if statements.

classes/models/FrmStyle.php (1)

568-571: LGTM – whitespace alignment in short if block.

The spacing adjustment improves visual alignment with the subsequent line that accesses $default_style->ID. Consistent with the PR's whitespace cleanup objective.

classes/models/FrmEntryValidate.php (1)

1128-1132: LGTM – whitespace alignment for name field string conversion.

The spacing adjustment improves visual alignment when converting the name field's first/last array to a string. Consistent with the PR's whitespace cleanup pattern and matches the similar pattern at lines 1102–1104.

phpcs-sniffs/Formidable/Sniffs/WhiteSpace/NoBlankLineInShortIfSniff.php (3)

1-31: LGTM!

The file structure, namespace, imports, and class documentation follow PHP_CodeSniffer conventions. The docblock clearly illustrates the intended behavior with good and bad examples.


38-40: LGTM!

The register method correctly returns the tokens for if/elseif/else blocks.


50-73: LGTM!

The scope validation and body line calculation are correct. The exact 3-line constraint aligns with the sniff's purpose of targeting short if blocks.

Comment on lines +113 to +117
$fix = $phpcsFile->addFixableError(
'Remove blank line inside short if block (3 lines or less).',
$blankLineToken,
'BlankLineInShortIf'
);
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.

⚠️ Potential issue | 🟡 Minor

Error message inconsistency.

The message says "3 lines or less" but the sniff only triggers for exactly 3-line blocks (line 71 checks $bodyLines !== 3).

📝 Suggested fix
 		$fix = $phpcsFile->addFixableError(
-			'Remove blank line inside short if block (3 lines or less).',
+			'Remove blank line inside short if block (exactly 3 body lines).',
 			$blankLineToken,
 			'BlankLineInShortIf'
 		);
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
$fix = $phpcsFile->addFixableError(
'Remove blank line inside short if block (3 lines or less).',
$blankLineToken,
'BlankLineInShortIf'
);
$fix = $phpcsFile->addFixableError(
'Remove blank line inside short if block (exactly 3 body lines).',
$blankLineToken,
'BlankLineInShortIf'
);
🤖 Prompt for AI Agents
In @phpcs-sniffs/Formidable/Sniffs/WhiteSpace/NoBlankLineInShortIfSniff.php
around lines 113 - 117, The error text is inconsistent with the check in
NoBlankLineInShortIfSniff: the sniff only triggers when $bodyLines === 3 but the
message says "3 lines or less"; update either the condition or the message to
match. Easiest fix: change the message passed to $phpcsFile->addFixableError in
NoBlankLineInShortIfSniff to "Remove blank line inside short if block (exactly 3
lines)." Alternatively, if the intention is to cover blocks with up to 3 lines,
adjust the $bodyLines check (currently comparing $bodyLines !== 3) to $bodyLines
<= 3 so the behavior matches the original message.

Comment on lines +125 to +128
if ( substr_count( $content, "\n" ) >= 2 ) {
// Replace double newline with single newline, preserving indentation.
$newContent = preg_replace( "/\n\n/", "\n", $content, 1 );
$phpcsFile->fixer->replaceToken( $blankLineToken, $newContent );
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.

⚠️ Potential issue | 🟠 Major

Fix may fail when newlines aren't consecutive in the token content.

The detection at line 85 triggers when a whitespace token contains 2+ newlines anywhere (substr_count), but the fix regex /\n\n/ only matches consecutive newlines. If the blank line has indentation (e.g., content is "\n \n"), the regex won't match and $newContent equals $content, leaving the blank line unfixed.

🐛 Proposed fix
 			// Remove one newline from the whitespace.
 			if ( substr_count( $content, "\n" ) >= 2 ) {
-				// Replace double newline with single newline, preserving indentation.
-				$newContent = preg_replace( "/\n\n/", "\n", $content, 1 );
+				// Remove the first blank line (newline + any following whitespace up to next newline).
+				$newContent = preg_replace( "/\n[ \t]*\n/", "\n", $content, 1 );
 				$phpcsFile->fixer->replaceToken( $blankLineToken, $newContent );
🤖 Prompt for AI Agents
In @phpcs-sniffs/Formidable/Sniffs/WhiteSpace/NoBlankLineInShortIfSniff.php
around lines 125 - 128, The fix fails when the whitespace token contains a blank
line with indentation because substr_count( $content, "\n" ) detects 2+ newlines
but the preg_replace( "/\n\n/", "\n", $content, 1 ) only matches consecutive
newlines; update the preg_replace call in NoBlankLineInShortIfSniff (operating
on $content and $blankLineToken) to match a newline, any intervening
spaces/tabs, then another newline (use a regex like newline + optional [ \t]* +
newline), capture the indentation and replace with a single newline plus the
captured indentation (still with a limit of 1) so indented blank lines are
collapsed correctly.

stephywells pushed a commit that referenced this pull request Apr 4, 2026
…ary_whitespace_in_small_if_conditions

New sniff to remove unnecessary white space in small if conditions
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