Update sniff to remove empty new lines at the start of if conditions#2773
Conversation
WalkthroughA new PHP_CodeSniffer sniff enforces removal of blank lines after if/elseif/else opening braces, with the formatting applied across twelve existing files to comply with this code standard. Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~12 minutes Possibly related PRs
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 inconclusive)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
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. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
📜 Review details
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
⛔ Files ignored due to path filters (1)
phpcs.xmlis excluded by!**/*.xml
📒 Files selected for processing (13)
classes/controllers/FrmFormsController.phpclasses/helpers/FrmXMLHelper.phpclasses/models/FrmAddon.phpclasses/models/FrmCreateFile.phpclasses/models/FrmEmail.phpclasses/models/FrmEntryFormatter.phpclasses/models/FrmEntryMeta.phpclasses/models/FrmForm.phpclasses/models/FrmHoneypot.phpclasses/models/FrmNotification.phpclasses/models/FrmTableHTMLGenerator.phpclasses/views/frm-fields/back-end/radio-field.phpphpcs-sniffs/Formidable/Sniffs/WhiteSpace/NoBlankLineAfterIfOpenSniff.php
💤 Files with no reviewable changes (12)
- classes/controllers/FrmFormsController.php
- classes/models/FrmTableHTMLGenerator.php
- classes/views/frm-fields/back-end/radio-field.php
- classes/models/FrmAddon.php
- classes/models/FrmNotification.php
- classes/models/FrmForm.php
- classes/models/FrmHoneypot.php
- classes/models/FrmEntryFormatter.php
- classes/models/FrmEntryMeta.php
- classes/models/FrmEmail.php
- classes/helpers/FrmXMLHelper.php
- classes/models/FrmCreateFile.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). (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: PHP 7.4 tests in WP 6.9
- GitHub Check: Cypress
- GitHub Check: PHP 8 tests in WP 6.9
- GitHub Check: PHP 8 tests in WP 6.9
- GitHub Check: PHP 7.4 tests in WP 6.9
- GitHub Check: Cypress
🔇 Additional comments (1)
phpcs-sniffs/Formidable/Sniffs/WhiteSpace/NoBlankLineAfterIfOpenSniff.php (1)
48-75: LGTM! Detection logic is correct.The scope check properly handles alternative syntax (without braces), empty block handling is appropriate, and the line comparison logic correctly identifies blank lines (content more than 1 line after opener).
| } elseif ( $newlineCount === 1 ) { | ||
| // Single newline here, but there might be another whitespace token creating the blank. | ||
| // Check if the next token is also whitespace with a newline. | ||
| $afterNext = $nextToken + 1; | ||
|
|
||
| if ( $afterNext < $firstContent && $tokens[ $afterNext ]['code'] === T_WHITESPACE ) { | ||
| // Remove this extra whitespace token. | ||
| $phpcsFile->fixer->replaceToken( $afterNext, '' ); | ||
| } | ||
| } |
There was a problem hiding this comment.
Potential indentation loss when fixing multi-token whitespace.
If the whitespace is split across multiple tokens (e.g., first token contains "\n" and second token contains "\n "), removing the second token entirely at line 105 will lose the indentation. The content would appear flush-left after the fix.
Consider preserving indentation by extracting it from the token closest to $firstContent:
Suggested fix
} elseif ( $newlineCount === 1 ) {
// Single newline here, but there might be another whitespace token creating the blank.
- // Check if the next token is also whitespace with a newline.
- $afterNext = $nextToken + 1;
-
- if ( $afterNext < $firstContent && $tokens[ $afterNext ]['code'] === T_WHITESPACE ) {
- // Remove this extra whitespace token.
- $phpcsFile->fixer->replaceToken( $afterNext, '' );
+ // Find the token right before content to get proper indentation.
+ $beforeContent = $firstContent - 1;
+ if ( $tokens[ $beforeContent ]['code'] === T_WHITESPACE ) {
+ $wsContent = $tokens[ $beforeContent ]['content'];
+ $lastNewline = strrpos( $wsContent, "\n" );
+ $indent = ( false !== $lastNewline ) ? substr( $wsContent, $lastNewline + 1 ) : $wsContent;
+
+ // Replace first whitespace with single newline + proper indent.
+ $phpcsFile->fixer->replaceToken( $nextToken, "\n" . $indent );
+
+ // Remove all intermediate whitespace tokens.
+ for ( $i = $nextToken + 1; $i < $firstContent; $i++ ) {
+ if ( $tokens[ $i ]['code'] === T_WHITESPACE ) {
+ $phpcsFile->fixer->replaceToken( $i, '' );
+ }
+ }
}
}📝 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.
| } elseif ( $newlineCount === 1 ) { | |
| // Single newline here, but there might be another whitespace token creating the blank. | |
| // Check if the next token is also whitespace with a newline. | |
| $afterNext = $nextToken + 1; | |
| if ( $afterNext < $firstContent && $tokens[ $afterNext ]['code'] === T_WHITESPACE ) { | |
| // Remove this extra whitespace token. | |
| $phpcsFile->fixer->replaceToken( $afterNext, '' ); | |
| } | |
| } | |
| } elseif ( $newlineCount === 1 ) { | |
| // Single newline here, but there might be another whitespace token creating the blank. | |
| // Find the token right before content to get proper indentation. | |
| $beforeContent = $firstContent - 1; | |
| if ( $tokens[ $beforeContent ]['code'] === T_WHITESPACE ) { | |
| $wsContent = $tokens[ $beforeContent ]['content']; | |
| $lastNewline = strrpos( $wsContent, "\n" ); | |
| $indent = ( false !== $lastNewline ) ? substr( $wsContent, $lastNewline + 1 ) : $wsContent; | |
| // Replace first whitespace with single newline + proper indent. | |
| $phpcsFile->fixer->replaceToken( $nextToken, "\n" . $indent ); | |
| // Remove all intermediate whitespace tokens. | |
| for ( $i = $nextToken + 1; $i < $firstContent; $i++ ) { | |
| if ( $tokens[ $i ]['code'] === T_WHITESPACE ) { | |
| $phpcsFile->fixer->replaceToken( $i, '' ); | |
| } | |
| } | |
| } | |
| } |
…_new_lines_at_the_start_of_if_conditions Update sniff to remove empty new lines at the start of if conditions
No description provided.