Skip to content

Commit 76782dc

Browse files
committed
Add new sniff to prevent useless empty line at the start of loops
1 parent d702eae commit 76782dc

File tree

10 files changed

+114
-10
lines changed

10 files changed

+114
-10
lines changed

classes/controllers/FrmFormActionsController.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -677,7 +677,6 @@ public static function trigger_actions( $event, $form, $entry, $type = 'all', $a
677677
}
678678

679679
foreach ( $form_actions as $action ) {
680-
681680
$skip_this_action = ! in_array( $this_event, $action->post_content['event'], true ) || FrmOnSubmitAction::$slug === $action->post_excerpt;
682681
$skip_this_action = apply_filters( 'frm_skip_form_action', $skip_this_action, compact( 'action', 'entry', 'form', 'event' ) );
683682

classes/controllers/FrmFormsController.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2349,7 +2349,6 @@ private static function add_forms_to_admin_bar( $actions ) {
23492349
asort( $actions );
23502350

23512351
foreach ( $actions as $form_id => $name ) {
2352-
23532352
$wp_admin_bar->add_node(
23542353
array(
23552354
'parent' => 'frm-forms',

classes/helpers/FrmFormsListHelper.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,6 @@ public function get_views() {
201201
);
202202

203203
foreach ( $statuses as $status => $name ) {
204-
205204
$class = $status == $form_type ? ' class="current"' : ''; // phpcs:ignore Universal.Operators.StrictComparisons
206205

207206
if ( $counts->{$status} || 'draft' !== $status ) {

classes/helpers/FrmXMLHelper.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2177,7 +2177,6 @@ private static function migrate_notifications_to_action( $form_options, $form_id
21772177

21782178
if ( isset( $form_options['notification'] ) && is_array( $form_options['notification'] ) ) {
21792179
foreach ( $form_options['notification'] as $email_key => $notification ) {
2180-
21812180
$atts = array(
21822181
'email_to' => '',
21832182
'reply_to' => '',
@@ -2263,7 +2262,6 @@ private static function format_email_to_data( &$atts, $notification ) {
22632262
}
22642263

22652264
foreach ( $atts['email_to'] as $key => $email_field ) {
2266-
22672265
if ( is_numeric( $email_field ) ) {
22682266
$atts['email_to'][ $key ] = '[' . $email_field . ']';
22692267
}

classes/views/frm-fields/back-end/radio-field.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
} elseif ( is_array( $field['options'] ) ) {
1010

1111
foreach ( $field['options'] as $opt_key => $opt ) {
12-
1312
$field_val = FrmFieldsHelper::get_value_from_array( $opt, $opt_key, $field );
1413
$opt = FrmFieldsHelper::get_label_from_array( $opt, $opt_key, $field );
1514
?>

classes/views/frm-forms/add_field_links.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,6 @@
8585
$field_sections = array();
8686

8787
foreach ( $pro_fields as $field_key => $field_type ) {
88-
8988
if ( isset( $field_type['section'] ) ) {
9089
if ( ! isset( $field_sections[ $field_type['section'] ] ) ) {
9190
$field_sections[ $field_type['section'] ] = array();

classes/views/xml/xml.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
do_action( 'frm_xml_export_before_types_loop' );
1818

1919
foreach ( $type as $tb_type ) {
20-
2120
if ( ! isset( $tables[ $tb_type ] ) ) {
2221
do_action( 'frm_xml_import_' . $tb_type, $args );
2322
continue;
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
<?php
2+
/**
3+
* Formidable_Sniffs_WhiteSpace_NoBlankLineAfterLoopOpenSniff
4+
*
5+
* Ensures there is no blank line immediately after the opening brace of a loop.
6+
*
7+
* @package Formidable\Sniffs
8+
*/
9+
10+
namespace Formidable\Sniffs\WhiteSpace;
11+
12+
use PHP_CodeSniffer\Sniffs\Sniff;
13+
use PHP_CodeSniffer\Files\File;
14+
15+
/**
16+
* Ensures there is no blank line immediately after the opening brace of a loop.
17+
*
18+
* Bad:
19+
* foreach ( $items as $item ) {
20+
*
21+
* do_something( $item );
22+
* }
23+
*
24+
* Good:
25+
* foreach ( $items as $item ) {
26+
* do_something( $item );
27+
* }
28+
*/
29+
class NoBlankLineAfterLoopOpenSniff implements Sniff {
30+
31+
/**
32+
* Returns an array of tokens this test wants to listen for.
33+
*
34+
* @return array
35+
*/
36+
public function register() {
37+
return array( T_FOREACH, T_FOR, T_WHILE, T_DO );
38+
}
39+
40+
/**
41+
* Processes this test, when one of its tokens is encountered.
42+
*
43+
* @param File $phpcsFile The file being scanned.
44+
* @param int $stackPtr The position of the current token in the stack passed in $tokens.
45+
*
46+
* @return void
47+
*/
48+
public function process( File $phpcsFile, $stackPtr ) {
49+
$tokens = $phpcsFile->getTokens();
50+
51+
// Make sure this loop has a scope (curly braces).
52+
if ( ! isset( $tokens[ $stackPtr ]['scope_opener'] ) ) {
53+
return;
54+
}
55+
56+
$scopeOpener = $tokens[ $stackPtr ]['scope_opener'];
57+
$scopeCloser = $tokens[ $stackPtr ]['scope_closer'];
58+
59+
// Find the first non-whitespace token after the opening brace.
60+
$firstContent = $phpcsFile->findNext( T_WHITESPACE, $scopeOpener + 1, $scopeCloser, true );
61+
62+
if ( false === $firstContent ) {
63+
// Empty loop body.
64+
return;
65+
}
66+
67+
$openerLine = $tokens[ $scopeOpener ]['line'];
68+
$contentLine = $tokens[ $firstContent ]['line'];
69+
70+
// Check if there's a blank line between the opener and first content.
71+
// A blank line means the content is more than 1 line after the opener.
72+
if ( $contentLine <= $openerLine + 1 ) {
73+
// No blank line, content is on the next line or same line.
74+
return;
75+
}
76+
77+
$fix = $phpcsFile->addFixableError(
78+
'No blank line should follow the opening brace of a loop.',
79+
$scopeOpener,
80+
'BlankLineAfterLoopOpen'
81+
);
82+
83+
if ( true === $fix ) {
84+
$phpcsFile->fixer->beginChangeset();
85+
86+
// Find the whitespace token right after the opener that contains the blank line.
87+
$nextToken = $scopeOpener + 1;
88+
89+
if ( $tokens[ $nextToken ]['code'] === T_WHITESPACE ) {
90+
$content = $tokens[ $nextToken ]['content'];
91+
$newlineCount = substr_count( $content, "\n" );
92+
93+
if ( $newlineCount >= 2 ) {
94+
// Multiple newlines - reduce to single newline + indentation.
95+
$lastNewline = strrpos( $content, "\n" );
96+
$indent = substr( $content, $lastNewline + 1 );
97+
$phpcsFile->fixer->replaceToken( $nextToken, "\n" . $indent );
98+
} elseif ( $newlineCount === 1 ) {
99+
// Single newline here, but there might be another whitespace token creating the blank.
100+
// Check if the next token is also whitespace with a newline.
101+
$afterNext = $nextToken + 1;
102+
103+
if ( $afterNext < $firstContent && $tokens[ $afterNext ]['code'] === T_WHITESPACE ) {
104+
// Remove this extra whitespace token.
105+
$phpcsFile->fixer->replaceToken( $afterNext, '' );
106+
}
107+
}
108+
}
109+
110+
$phpcsFile->fixer->endChangeset();
111+
}
112+
}
113+
}

phpcs.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,4 +292,5 @@
292292
<rule ref="Formidable.CodeAnalysis.SimplifyIfReturn" />
293293
<rule ref="Formidable.CodeAnalysis.PreferObGetClean" />
294294
<rule ref="Formidable.WhiteSpace.NoBlankLineInShortIf" />
295+
<rule ref="Formidable.WhiteSpace.NoBlankLineAfterLoopOpen" />
295296
</ruleset>

tests/phpunit/entries/test_FrmShowEntryShortcode.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1053,7 +1053,6 @@ protected function get_expected_default_shortcodes( $type, $atts ) {
10531053
}
10541054

10551055
foreach ( $fields as $field ) {
1056-
10571056
if ( in_array( $field->type, array( 'html', 'captcha' ), true ) ) {
10581057
continue;
10591058
}
@@ -1096,7 +1095,6 @@ protected function expected_default_array( $atts ) {
10961095
$expected = array();
10971096

10981097
foreach ( $fields as $field ) {
1099-
11001098
if ( in_array( $field->type, array( 'html', 'captcha' ), true ) ) {
11011099
continue;
11021100
}

0 commit comments

Comments
 (0)