Skip to content

Commit c02099d

Browse files
committed
Arrays/ArrayDeclarationSpacing: add new property and soft-deprecate old one
Add `allow_single_item_single_line_explicit_key_arrays` property to replace `allow_single_item_single_line_associative_arrays`, aligning the property name with the "explicit keys" terminology from PR 2688. The old property is soft-deprecated (docblock only) and continues to work via a backward compatibility layer: if the new property has not been changed from its default and the old property has, the old property's value is used. Closes: 2691
1 parent 75c9b94 commit c02099d

6 files changed

+84
-21
lines changed

WordPress/Sniffs/Arrays/ArrayDeclarationSpacingSniff.php

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,25 @@
3838
* `allow_single_item_single_line_associative_arrays` property.
3939
* @since 3.0.0 Removed various whitespace related checks and fixers in favor of the PHPCSExtra
4040
* `NormalizedArrays.Arrays.ArrayBraceSpacing` sniff.
41+
* @since 3.4.0 The `allow_single_item_single_line_associative_arrays` property has been
42+
* deprecated in favor of `allow_single_item_single_line_explicit_key_arrays`.
4143
*/
4244
final class ArrayDeclarationSpacingSniff extends Sniff {
4345

46+
/**
47+
* Whether to allow single item arrays with explicit keys to be single line.
48+
*
49+
* @since 3.4.0
50+
*
51+
* @var bool Defaults to true.
52+
*/
53+
public $allow_single_item_single_line_explicit_key_arrays = true;
54+
4455
/**
4556
* Whether or not to allow single item arrays with explicit keys to be single line.
4657
*
4758
* @since 0.14.0
59+
* @deprecated 3.4.0 Use $allow_single_item_single_line_explicit_key_arrays instead.
4860
*
4961
* @var bool Defaults to true.
5062
*/
@@ -114,10 +126,19 @@ public function process_token( $stackPtr ) {
114126
* @return void
115127
*/
116128
protected function process_single_line_array( $stackPtr, $opener, $closer ) {
129+
// For now, if the new property has not been changed from its default value and the deprecated property has, use
130+
// the deprecated property's value.
131+
$allow_single_item = $this->allow_single_item_single_line_explicit_key_arrays;
132+
if ( true === $allow_single_item
133+
&& true !== $this->allow_single_item_single_line_associative_arrays
134+
) {
135+
$allow_single_item = $this->allow_single_item_single_line_associative_arrays;
136+
}
137+
117138
$array_items = PassedParameters::getParameters( $this->phpcsFile, $stackPtr );
118-
if ( ( false === $this->allow_single_item_single_line_associative_arrays
139+
if ( ( false === $allow_single_item
119140
&& empty( $array_items ) )
120-
|| ( true === $this->allow_single_item_single_line_associative_arrays
141+
|| ( true === $allow_single_item
121142
&& \count( $array_items ) === 1 )
122143
) {
123144
return;
@@ -138,7 +159,7 @@ protected function process_single_line_array( $stackPtr, $opener, $closer ) {
138159
return;
139160
}
140161
$error = 'When an array is declared with explicit keys, each value should start on %s.';
141-
if ( true === $this->allow_single_item_single_line_associative_arrays ) {
162+
if ( true === $allow_single_item ) {
142163
$error = 'When a multi-item array is declared with explicit keys, each value should start on %s.';
143164
}
144165

WordPress/Tests/Arrays/ArrayDeclarationSpacingUnitTest.1.inc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,3 +81,10 @@ $bad = array(
8181

8282
// Don't confuse list arrows with array arrows.
8383
$okay = array( $item1, list( 'key1' => $a, 'key2' => $b ) = $array, $item3 );
84+
85+
// phpcs:set WordPress.Arrays.ArrayDeclarationSpacing allow_single_item_single_line_explicit_key_arrays false
86+
87+
$bad = array( 'key' => 'value' ); // Bad.
88+
$bad = array( 'key1' => 'value1', 'key2' => 'value2' ); // Bad.
89+
90+
// phpcs:set WordPress.Arrays.ArrayDeclarationSpacing allow_single_item_single_line_explicit_key_arrays true

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,3 +134,15 @@ $bad = array(
134134

135135
// Don't confuse list arrows with array arrows.
136136
$okay = array( $item1, list( 'key1' => $a, 'key2' => $b ) = $array, $item3 );
137+
138+
// phpcs:set WordPress.Arrays.ArrayDeclarationSpacing allow_single_item_single_line_explicit_key_arrays false
139+
140+
$bad = array(
141+
'key' => 'value'
142+
); // Bad.
143+
$bad = array(
144+
'key1' => 'value1',
145+
'key2' => 'value2'
146+
); // Bad.
147+
148+
// phpcs:set WordPress.Arrays.ArrayDeclarationSpacing allow_single_item_single_line_explicit_key_arrays true

WordPress/Tests/Arrays/ArrayDeclarationSpacingUnitTest.2.inc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,3 +107,10 @@ $bad = [
107107

108108
// Don't confuse list arrows with array arrows.
109109
$okay = [ $item1, [ 'key1' => $a, 'key2' => $b ] = $array, $item3 ];
110+
111+
// phpcs:set WordPress.Arrays.ArrayDeclarationSpacing allow_single_item_single_line_explicit_key_arrays false
112+
113+
$bad = [ 'key' => 'value' ]; // Bad.
114+
$bad = [ 'key1' => 'value1', 'key2' => 'value2' ]; // Bad.
115+
116+
// phpcs:set WordPress.Arrays.ArrayDeclarationSpacing allow_single_item_single_line_explicit_key_arrays true

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,3 +163,15 @@ $bad = [
163163

164164
// Don't confuse list arrows with array arrows.
165165
$okay = [ $item1, [ 'key1' => $a, 'key2' => $b ] = $array, $item3 ];
166+
167+
// phpcs:set WordPress.Arrays.ArrayDeclarationSpacing allow_single_item_single_line_explicit_key_arrays false
168+
169+
$bad = [
170+
'key' => 'value'
171+
]; // Bad.
172+
$bad = [
173+
'key1' => 'value1',
174+
'key2' => 'value2'
175+
]; // Bad.
176+
177+
// phpcs:set WordPress.Arrays.ArrayDeclarationSpacing allow_single_item_single_line_explicit_key_arrays true

WordPress/Tests/Arrays/ArrayDeclarationSpacingUnitTest.php

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -51,29 +51,33 @@ public function getErrorList( $testFile = '' ) {
5151
62 => 1,
5252
63 => 1,
5353
75 => 1,
54+
87 => 1,
55+
88 => 1,
5456
);
5557

5658
// Short arrays.
5759
case 'ArrayDeclarationSpacingUnitTest.2.inc':
5860
return array(
59-
9 => 4,
60-
13 => 2,
61-
15 => 1,
62-
19 => 1,
63-
22 => 1,
64-
25 => 1,
65-
44 => 1,
66-
45 => 1,
67-
48 => 1,
68-
49 => 1,
69-
52 => 2,
70-
54 => 1,
71-
57 => 1,
72-
58 => 1,
73-
62 => 1,
74-
63 => 1,
75-
75 => 1,
76-
97 => 1,
61+
9 => 4,
62+
13 => 2,
63+
15 => 1,
64+
19 => 1,
65+
22 => 1,
66+
25 => 1,
67+
44 => 1,
68+
45 => 1,
69+
48 => 1,
70+
49 => 1,
71+
52 => 2,
72+
54 => 1,
73+
57 => 1,
74+
58 => 1,
75+
62 => 1,
76+
63 => 1,
77+
75 => 1,
78+
97 => 1,
79+
113 => 1,
80+
114 => 1,
7781
);
7882

7983
default:

0 commit comments

Comments
 (0)