Skip to content

Commit ebb9d61

Browse files
committed
Arrays/ArrayDeclarationSpacing: replace whitespace checks with PHPCSExtra sniff
This commit removes the brace related spacing checks from the `WordPress.Arrays.ArrayDeclarationSpacing` sniff in favour of using the PHPCSExtra `NormalizedArrays.Arrays.ArrayBraceSpacing` sniff. Includes removing tests which were only testing the brace spacing related issue. Includes minor updates to the `fixed` file for the remaining tests to account for a fix which will no longer be made by this sniff. Note: in effect, the combined sniffs will do the same as before, though a few more notices will be thrown. This is due to the original set-up not throwing errors for "space after opener" and "space before closer", when a "single line array with associative keys must be multi-line" error would be thrown, as the fix would be the same. These "space after opener" and "space before closer" errors will now show together with the "single line must be multi-line" error and the fixers of the combined sniffs will sort it out between them. Refs: * PHPCSStandards/PHPCSExtra 12 * https://github.com/PHPCSStandards/PHPCSExtra/pulls?q=is%3Apr+arraybracespacing+is%3Aclosed
1 parent 8765b74 commit ebb9d61

File tree

7 files changed

+60
-376
lines changed

7 files changed

+60
-376
lines changed

WordPress-Core/ruleset.xml

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -256,10 +256,18 @@
256256

257257
<!-- Generic array layout check. -->
258258
<!-- Covers rule: For associative arrays, each item should start on a new line
259-
when the array contains more than one item.
260-
Also covers various single-line array whitespace issues. -->
259+
when the array contains more than one item. -->
261260
<rule ref="WordPress.Arrays.ArrayDeclarationSpacing"/>
262261

262+
<!-- Covers various array whitespace issues. -->
263+
<rule ref="NormalizedArrays.Arrays.ArrayBraceSpacing">
264+
<properties>
265+
<property name="spacesWhenEmpty" value="0"/>
266+
<property name="spacesSingleLine" value="1"/>
267+
<property name="spacesMultiLine" value="newline"/>
268+
</properties>
269+
</rule>
270+
263271
<!-- Covers rule: Note the comma after the last array item: this is recommended. -->
264272
<rule ref="WordPress.Arrays.CommaAfterArrayItem"/>
265273

WordPress/Sniffs/Arrays/ArrayDeclarationSpacingSniff.php

Lines changed: 3 additions & 150 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,8 @@
1919
/**
2020
* Enforces WordPress array spacing format.
2121
*
22-
* - Check for no space between array keyword and array opener.
23-
* - Check for no space between the parentheses of an empty array.
24-
* - Checks for one space after the array opener / before the array closer in single-line arrays.
2522
* - Checks that associative arrays are multi-line.
2623
* - Checks that each array item in a multi-line array starts on a new line.
27-
* - Checks that the array closer in a multi-line array is on a new line.
2824
*
2925
* @link https://developer.wordpress.org/coding-standards/wordpress-coding-standards/php/#indentation
3026
*
@@ -42,6 +38,8 @@
4238
* @since 0.14.0 Single item associative arrays are now by default exempt from the
4339
* "must be multi-line" rule. This behaviour can be changed using the
4440
* `allow_single_item_single_line_associative_arrays` property.
41+
* @since 3.0.0 Removed various whitespace related checks and fixers in favour of the PHPCSExtra
42+
* `NormalizedArrays.Arrays.ArrayBraceSpacing` sniff.
4543
*/
4644
final class ArrayDeclarationSpacingSniff extends Sniff {
4745

@@ -97,64 +95,6 @@ public function process_token( $stackPtr ) {
9795
$closer = $array_open_close['closer'];
9896
unset( $array_open_close );
9997

100-
/*
101-
* Long arrays only: Check for space between the array keyword and the open parenthesis.
102-
*/
103-
if ( \T_ARRAY === $this->tokens[ $stackPtr ]['code'] ) {
104-
105-
if ( ( $stackPtr + 1 ) !== $opener ) {
106-
$error = 'There must be no space between the "array" keyword and the opening parenthesis';
107-
$error_code = 'SpaceAfterKeyword';
108-
109-
$nextNonWhitespace = $this->phpcsFile->findNext( \T_WHITESPACE, ( $stackPtr + 1 ), ( $opener + 1 ), true );
110-
if ( $nextNonWhitespace !== $opener ) {
111-
// Don't auto-fix: Something other than whitespace found between keyword and open parenthesis.
112-
$this->phpcsFile->addError( $error, $stackPtr, $error_code );
113-
} else {
114-
115-
$fix = $this->phpcsFile->addFixableError( $error, $stackPtr, $error_code );
116-
117-
if ( true === $fix ) {
118-
$this->phpcsFile->fixer->beginChangeset();
119-
for ( $i = ( $stackPtr + 1 ); $i < $opener; $i++ ) {
120-
$this->phpcsFile->fixer->replaceToken( $i, '' );
121-
}
122-
$this->phpcsFile->fixer->endChangeset();
123-
unset( $i );
124-
}
125-
}
126-
unset( $error, $error_code, $nextNonWhitespace, $fix );
127-
}
128-
}
129-
130-
/*
131-
* Check for empty arrays.
132-
*/
133-
$nextNonWhitespace = $this->phpcsFile->findNext( \T_WHITESPACE, ( $opener + 1 ), ( $closer + 1 ), true );
134-
if ( $nextNonWhitespace === $closer ) {
135-
136-
if ( ( $opener + 1 ) !== $closer ) {
137-
$fix = $this->phpcsFile->addFixableError(
138-
'Empty array declaration must have no space between the parentheses',
139-
$stackPtr,
140-
'SpaceInEmptyArray'
141-
);
142-
143-
if ( true === $fix ) {
144-
$this->phpcsFile->fixer->beginChangeset();
145-
for ( $i = ( $opener + 1 ); $i < $closer; $i++ ) {
146-
$this->phpcsFile->fixer->replaceToken( $i, '' );
147-
}
148-
$this->phpcsFile->fixer->endChangeset();
149-
unset( $i );
150-
}
151-
}
152-
153-
// This array is empty, so the below checks aren't necessary.
154-
return;
155-
}
156-
unset( $nextNonWhitespace );
157-
15898
// Pass off to either the single line or multi-line array analysis.
15999
if ( $this->tokens[ $opener ]['line'] === $this->tokens[ $closer ]['line'] ) {
160100
$this->process_single_line_array( $stackPtr, $opener, $closer );
@@ -164,7 +104,7 @@ public function process_token( $stackPtr ) {
164104
}
165105

166106
/**
167-
* Process a single-line array.
107+
* Check that associative arrays are always multi-line.
168108
*
169109
* @since 0.13.0 The actual checks contained in this method used to
170110
* be in the `process()` method.
@@ -176,9 +116,6 @@ public function process_token( $stackPtr ) {
176116
* @return void
177117
*/
178118
protected function process_single_line_array( $stackPtr, $opener, $closer ) {
179-
/*
180-
* Check that associative arrays are always multi-line.
181-
*/
182119
$array_items = PassedParameters::getParameters( $this->phpcsFile, $stackPtr );
183120

184121
if ( ( false === $this->allow_single_item_single_line_associative_arrays
@@ -216,62 +153,6 @@ protected function process_single_line_array( $stackPtr, $opener, $closer ) {
216153
'AssociativeArrayFound',
217154
'error'
218155
);
219-
220-
// No need to check for spacing around opener/closer as this array should be multi-line.
221-
return;
222-
}
223-
}
224-
225-
/*
226-
* Check that there is a single space after the array opener and before the array closer.
227-
*/
228-
if ( \T_WHITESPACE !== $this->tokens[ ( $opener + 1 ) ]['code'] ) {
229-
230-
$fix = $this->phpcsFile->addFixableError(
231-
'Missing space after array opener.',
232-
$opener,
233-
'NoSpaceAfterArrayOpener'
234-
);
235-
236-
if ( true === $fix ) {
237-
$this->phpcsFile->fixer->addContent( $opener, ' ' );
238-
}
239-
} elseif ( ' ' !== $this->tokens[ ( $opener + 1 ) ]['content'] ) {
240-
241-
$fix = $this->phpcsFile->addFixableError(
242-
'Expected 1 space after array opener, found %s.',
243-
$opener,
244-
'SpaceAfterArrayOpener',
245-
array( \strlen( $this->tokens[ ( $opener + 1 ) ]['content'] ) )
246-
);
247-
248-
if ( true === $fix ) {
249-
$this->phpcsFile->fixer->replaceToken( ( $opener + 1 ), ' ' );
250-
}
251-
}
252-
253-
if ( \T_WHITESPACE !== $this->tokens[ ( $closer - 1 ) ]['code'] ) {
254-
255-
$fix = $this->phpcsFile->addFixableError(
256-
'Missing space before array closer.',
257-
$closer,
258-
'NoSpaceBeforeArrayCloser'
259-
);
260-
261-
if ( true === $fix ) {
262-
$this->phpcsFile->fixer->addContentBefore( $closer, ' ' );
263-
}
264-
} elseif ( ' ' !== $this->tokens[ ( $closer - 1 ) ]['content'] ) {
265-
266-
$fix = $this->phpcsFile->addFixableError(
267-
'Expected 1 space before array closer, found %s.',
268-
$closer,
269-
'SpaceBeforeArrayCloser',
270-
array( \strlen( $this->tokens[ ( $closer - 1 ) ]['content'] ) )
271-
);
272-
273-
if ( true === $fix ) {
274-
$this->phpcsFile->fixer->replaceToken( ( $closer - 1 ), ' ' );
275156
}
276157
}
277158
}
@@ -289,34 +170,6 @@ protected function process_single_line_array( $stackPtr, $opener, $closer ) {
289170
* @return void
290171
*/
291172
protected function process_multi_line_array( $stackPtr, $opener, $closer ) {
292-
/*
293-
* Check that the closing bracket is on a new line.
294-
*/
295-
$last_content = $this->phpcsFile->findPrevious( \T_WHITESPACE, ( $closer - 1 ), $opener, true );
296-
if ( false !== $last_content
297-
&& $this->tokens[ $last_content ]['line'] === $this->tokens[ $closer ]['line']
298-
) {
299-
$fix = $this->phpcsFile->addFixableError(
300-
'Closing parenthesis of array declaration must be on a new line',
301-
$closer,
302-
'CloseBraceNewLine'
303-
);
304-
if ( true === $fix ) {
305-
$this->phpcsFile->fixer->beginChangeset();
306-
307-
if ( $last_content < ( $closer - 1 )
308-
&& \T_WHITESPACE === $this->tokens[ ( $closer - 1 ) ]['code']
309-
) {
310-
// Remove whitespace which would otherwise becoming trailing
311-
// (as it gives problems with the fixed file).
312-
$this->phpcsFile->fixer->replaceToken( ( $closer - 1 ), '' );
313-
}
314-
315-
$this->phpcsFile->fixer->addNewlineBefore( $closer );
316-
$this->phpcsFile->fixer->endChangeset();
317-
}
318-
}
319-
320173
/*
321174
* Check that each array item starts on a new line.
322175
*/

WordPress/Tests/Arrays/ArrayDeclarationSpacingUnitTest.1.inc

Lines changed: 1 addition & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,6 @@
33
* Test sniff with long arrays.
44
*/
55

6-
$good = array( 'value1', 'value2' ); // Ok.
7-
8-
$query_vars = array('food'); // Bad, no spaces after opening and before closing parenthesis.
9-
10-
// Test for fixing of extra whitespace.
11-
$test = array( 1, 2 );
12-
136
$bad = array( 'key' => 'value' ); // OK, one item single-line associative arrays are ok.
147

158
// Test for fixing nested associative arrays.
@@ -32,40 +25,7 @@ $bad = array( 'key1' => 'value1', /* comment */ 'key2' => 'value2' ); // Bad.
3225
$bad = array( 'value1', 'value2', array( 'sub1' => 1, 'sub2' => 2 ), 'value4' ); // Bad.
3326

3427
/*
35-
* Test spacing between array keyword and open parenthesis.
36-
*/
37-
$a = array(); // OK.
38-
$b = array (); // Bad.
39-
$train = array
40-
(
41-
true,
42-
'aaa'
43-
); // Bad - space between keyword and opener.
44-
45-
$a = array
46-
// Bad.
47-
( 'a', 'b' );
48-
49-
$a = array /* Bad. */ ( 'a', 'b' );
50-
51-
/*
52-
* Tests for empty array with space between parentheses.
53-
*/
54-
// OK.
55-
$a = array();
56-
$value = array( /* comment */ );
57-
$x = array(
58-
// comment
59-
);
60-
61-
// Bad.
62-
$value = array ( );
63-
$value = array( );
64-
$x = array(
65-
);
66-
67-
/*
68-
* Tests for multi-line arrays - closing brace on new line + array items each on new line.
28+
* Tests for multi-line arrays - array items each on new line.
6929
*/
7030
// OK.
7131
$value = array(
@@ -97,12 +57,6 @@ $x = array('name' => 'test',
9757
$foo = array(1
9858
, 2);
9959

100-
$fields = array(
101-
'value' => 'type');
102-
103-
$bad = array('key' => 'value'); // Bad, spacing around parenthesis.
104-
$bad = array( 'key' => 'value' ); // Bad, spacing around parenthesis.
105-
10660
// phpcs:set WordPress.Arrays.ArrayDeclarationSpacing allow_single_item_single_line_associative_arrays false
10761

10862
$bad = array( 'key' => 'value' ); // Bad.

WordPress/Tests/Arrays/ArrayDeclarationSpacingUnitTest.1.inc.fixed

Lines changed: 5 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,6 @@
33
* Test sniff with long arrays.
44
*/
55

6-
$good = array( 'value1', 'value2' ); // Ok.
7-
8-
$query_vars = array( 'food' ); // Bad, no spaces after opening and before closing parenthesis.
9-
10-
// Test for fixing of extra whitespace.
11-
$test = array( 1, 2 );
12-
136
$bad = array( 'key' => 'value' ); // OK, one item single-line associative arrays are ok.
147

158
// Test for fixing nested associative arrays.
@@ -67,42 +60,10 @@ array(
6760
'sub1' => 1,
6861
'sub2' => 2
6962
),
70-
'value4'
71-
); // Bad.
72-
73-
/*
74-
* Test spacing between array keyword and open parenthesis.
75-
*/
76-
$a = array(); // OK.
77-
$b = array(); // Bad.
78-
$train = array(
79-
true,
80-
'aaa'
81-
); // Bad - space between keyword and opener.
82-
83-
$a = array
84-
// Bad.
85-
( 'a', 'b' );
86-
87-
$a = array /* Bad. */ ( 'a', 'b' );
63+
'value4' ); // Bad.
8864

8965
/*
90-
* Tests for empty array with space between parentheses.
91-
*/
92-
// OK.
93-
$a = array();
94-
$value = array( /* comment */ );
95-
$x = array(
96-
// comment
97-
);
98-
99-
// Bad.
100-
$value = array();
101-
$value = array();
102-
$x = array();
103-
104-
/*
105-
* Tests for multi-line arrays - closing brace on new line + array items each on new line.
66+
* Tests for multi-line arrays - array items each on new line.
10667
*/
10768
// OK.
10869
$value = array(
@@ -127,14 +88,12 @@ $value = array(
12788
$value = array(
12889
1 => $one,
12990
2 => $two ,
130-
/* Comment. */ 3 => $three ,
131-
);
91+
/* Comment. */ 3 => $three , );
13292

13393
$value = array(
13494
'1'=> TRUE,
13595
FALSE,
136-
'3' => 'aaa',
137-
);
96+
'3' => 'aaa',);
13897

13998
$x = array(
14099
'name' => 'test',
@@ -143,15 +102,7 @@ $x = array(
143102
$foo = array(
144103
1
145104
,
146-
2
147-
);
148-
149-
$fields = array(
150-
'value' => 'type'
151-
);
152-
153-
$bad = array( 'key' => 'value' ); // Bad, spacing around parenthesis.
154-
$bad = array( 'key' => 'value' ); // Bad, spacing around parenthesis.
105+
2);
155106

156107
// phpcs:set WordPress.Arrays.ArrayDeclarationSpacing allow_single_item_single_line_associative_arrays false
157108

0 commit comments

Comments
 (0)