forked from WordPress/WordPress-Coding-Standards
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayDeclarationSpacingUnitTest.2.inc.fixed
More file actions
177 lines (150 loc) · 3.43 KB
/
ArrayDeclarationSpacingUnitTest.2.inc.fixed
File metadata and controls
177 lines (150 loc) · 3.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
<?php
/*
* Test sniff with short arrays.
*/
$bad = [ 'key' => 'value' ]; // OK, one item single-line associative arrays are ok.
// Test for fixing nested associative arrays.
$bad = [
[
'key1' => 'value1',
'key2' => [
'sub1' => 1,
'sub2' => 2
]
],
$key3 => 'value3',
[
'value4',
10 => 'value5',
]
]; // Bad.
// Test for fixing mixed single & multi-line nested associative arrays.
$bad = [
[
'key1' => 'value1',
[
'sub1' => 1,
'sub2' => 2,
]
],
$key3 => 'value3',
[
'value4',
10 => 'value5'
]
]; // Bad.
// Test for fixing associative arrays with multiple values & line indented with whitespace.
$bad = [
'key1' => 'value1',
'key2' => 'value2',
$key3 => 'value3',
'value4',
10 => 'value5'
]; // Bad.
// Test for fixing associative arrays with comments between values.
$bad = [
'key1' => 'value1',
/* comment */ 'key2' => 'value2'
]; // Bad.
// Test for fixing non-associative array with a nested associative array which *will* be fixed.
$bad = [
'value1',
'value2',
[
'sub1' => 1,
'sub2' => 2
],
'value4' ]; // Bad.
/*
* Tests for multi-line arrays - array items each on new line.
*/
// OK.
$value = [
1,
2, /* Comment. */
3,
];
$value = [
1 => $one,
2 => $two, // phpcs:ignore Standard.Category.Sniff.Errorcode -- for reason.
3 => $three, // Comment.
];
// Bad.
$value = [
1,
2 ,
3 ,
];
$value = [
1 => $one,
2 => $two ,
/* Comment. */ 3 => $three , ];
$value = [
'1'=> TRUE,
FALSE,
'3' => 'aaa',];
$x = [
'name' => 'test',
];
$foo = [
1
,
2];
// phpcs:set WordPress.Arrays.ArrayDeclarationSpacing allow_single_item_single_line_associative_arrays false
$bad = [
'key' => 'value'
]; // Bad.
$bad = [
'key1' => 'value1',
'key2' => 'value2'
]; // Bad.
// phpcs:set WordPress.Arrays.ArrayDeclarationSpacing allow_single_item_single_line_associative_arrays true
$foo = [
'meta_key' => 'foo', // phpcs:ignore Standard.Category.SniffName.ErrorCode
'meta_value' => 'bar', // phpcs:ignore Standard.Category.SniffName.ErrorCode
];
// Test for fixing array with multi-line comments between values.
$bad = [
'key1' => 'value1', /* comment
end */
'key2' => 'value2', // Bad.
/* Non-trailing comment. */
'key3' => 'value3', /* comment
end */
'key4' => 'value4'
];
/*
* Issue #1692: Test distinguishing between short array and short list.
*
* Note: these short lists should all contain "violations" against the array
* declaration sniff if they would be recognized as short array to make sure
* they are correctly recognized as short lists.
*/
// Empty list, not allowed since PHP 7.0, but not our concern.
[ ] = $array; // OK.
[, ,] = $array; // OK.
[$var1, $var2] = $array; // OK.
// Keyed list.
[$foo => $bar] = $bar; // OK.
['enabled' => $enabled, 'compression' => $compression] = [
'enabled' => true,
'compression' => 'gzip'
]; // Bad x 1 - only for the short array, not the short list.
// Nested short lists.
[$var1, , [$var2, $var3,], $var4] = $array; // OK.
[ 'o' => [[ $one, $two, $three ], [ 'what' => $what ]] ] = $x; // OK.
// Destructuring - multiline.
[
'prop1' => $prop1, 'prop2' => $prop2, // OK.
] = $some_var;
// Don't confuse list arrows with array arrows.
$okay = [ $item1, [ 'key1' => $a, 'key2' => $b ] = $array, $item3 ];
// phpcs:set WordPress.Arrays.ArrayDeclarationSpacing allow_single_item_single_line_explicit_key_arrays false
$bad = [
'key' => 'value'
]; // Bad.
$bad = [
'key1' => 'value1',
'key2' => 'value2'
]; // Bad.
// phpcs:set WordPress.Arrays.ArrayDeclarationSpacing allow_single_item_single_line_explicit_key_arrays true