Skip to content

Commit a23d82b

Browse files
committed
Split the CommaAfterLast errors, adding *CloserSameLine ones
Some standards may want to have different rules when the array closer is in the same line than the last element. When that happens, the new *CloserSameLine errors are emitted, allowing to disable them via ruleset. Only for MultiLine cases, they don't make sense in SingleLine ones. For testing, a couple of cases have been added to CommaAfterLastUnitTest.1.inc and then, the fixtures have been 100% duplicated into CommaAfterLastUnitTest.3.inc that runs with the *CloserSameLine errors disabled. A simple diff shows the 8 cases in which the outcome is different, as expected. Fixes #283.
1 parent 746c319 commit a23d82b

File tree

6 files changed

+504
-18
lines changed

6 files changed

+504
-18
lines changed

NormalizedArrays/Sniffs/Arrays/CommaAfterLastSniff.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,12 @@ public function process(File $phpcsFile, $stackPtr)
147147

148148
$isComma = ($tokens[$lastNonEmpty]['code'] === \T_COMMA);
149149

150+
// If the closer in the same line than the last element, change the error code for multi-line arrays.
151+
$sameLineCloser = ($tokens[$lastNonEmpty]['line'] === $tokens[$closer]['line']);
152+
if ($errorCode === 'MultiLine' && $sameLineCloser) {
153+
$errorCode .= 'CloserSameLine';
154+
}
155+
150156
$phpcsFile->recordMetric(
151157
$stackPtr,
152158
\sprintf(self::METRIC_NAME, \ucfirst($phrase)),

NormalizedArrays/Tests/Arrays/CommaAfterLastUnitTest.1.inc

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
<?php
22

3+
// This is exactly the same as CommasInArrayUnitTest.3.inc, but with these lines
4+
// and all the *MultiLineCloserSameLine errors enabled (default) for the whole file.
5+
6+
// nophpcs:disable NormalizedArrays.Arrays.CommaAfterLast.FoundMultiLineCloserSameLine
7+
// nophpcs:disable NormalizedArrays.Arrays.CommaAfterLast.MissingMultiLineCloserSameLine
8+
9+
310
/*
411
* Verify that non-arrays are not touched by the sniff.
512
* - square brackets, not short array.
@@ -79,6 +86,14 @@ $missing = [
7986
'c'/* Comment. */
8087
];
8188

89+
$missing = array(
90+
1, 2,
91+
3, 4);
92+
93+
$missing = [
94+
'1', '2',
95+
'3', '4'];
96+
8297
$missingInNested = array(
8398
1 => 'value' ,
8499
2 => [
@@ -140,6 +155,14 @@ $found = [
140155
'c',
141156
];
142157

158+
$found = array(
159+
1, 2,
160+
3, 4,);
161+
162+
$found = [
163+
'1', '2',
164+
'3', '4',];
165+
143166
$foundInNested = array(
144167
1 => 'value' ,
145168
2 => [

NormalizedArrays/Tests/Arrays/CommaAfterLastUnitTest.1.inc.fixed

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
<?php
22

3+
// This is exactly the same as CommasInArrayUnitTest.3.inc, but with these lines
4+
// and all the *MultiLineCloserSameLine errors enabled (default) for the whole file.
5+
6+
// nophpcs:disable NormalizedArrays.Arrays.CommaAfterLast.FoundMultiLineCloserSameLine
7+
// nophpcs:disable NormalizedArrays.Arrays.CommaAfterLast.MissingMultiLineCloserSameLine
8+
9+
310
/*
411
* Verify that non-arrays are not touched by the sniff.
512
* - square brackets, not short array.
@@ -79,6 +86,14 @@ $missing = [
7986
'c',/* Comment. */
8087
];
8188

89+
$missing = array(
90+
1, 2,
91+
3, 4,);
92+
93+
$missing = [
94+
'1', '2',
95+
'3', '4',];
96+
8297
$missingInNested = array(
8398
1 => 'value' ,
8499
2 => [
@@ -141,6 +156,14 @@ $found = [
141156
'c'
142157
];
143158

159+
$found = array(
160+
1, 2,
161+
3, 4);
162+
163+
$found = [
164+
'1', '2',
165+
'3', '4'];
166+
144167
$foundInNested = array(
145168
1 => 'value' ,
146169
2 => [
Lines changed: 201 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,201 @@
1+
<?php
2+
3+
// This is exactly the same as CommasInArrayUnitTest.1.inc, but with these lines
4+
// and all the *MultiLineCloserSameLine errors disabled for the whole file.
5+
//
6+
// phpcs:disable NormalizedArrays.Arrays.CommaAfterLast.FoundMultiLineCloserSameLine
7+
// phpcs:disable NormalizedArrays.Arrays.CommaAfterLast.MissingMultiLineCloserSameLine
8+
9+
10+
/*
11+
* Verify that non-arrays are not touched by the sniff.
12+
* - square brackets, not short array.
13+
* - short lists.
14+
*/
15+
$a['array_access'] = 123;
16+
17+
// Short list, not short array.
18+
[,,] = $array;
19+
[$var1, , [$var2, $var3,], $var4,] = $array;
20+
21+
/*
22+
* Empty arrays should be ignored.
23+
*/
24+
$empty = array();
25+
$empty = [ /* comment */ ];
26+
27+
/*
28+
* Test skipping the checks when 'skip' has been passed.
29+
*/
30+
// phpcs:set NormalizedArrays.Arrays.CommaAfterLast singleLine skip
31+
// phpcs:set NormalizedArrays.Arrays.CommaAfterLast multiLine skip
32+
33+
$ignore = array(1, 2, 3,);
34+
$ignore = [
35+
1,
36+
3
37+
];
38+
39+
/*
40+
* Test skipping the checks when invalid property settings have been passed.
41+
*/
42+
// phpcs:set NormalizedArrays.Arrays.CommaAfterLast singleLine disallow
43+
// phpcs:set NormalizedArrays.Arrays.CommaAfterLast multiLine demand
44+
45+
$ignore = array(1, 2, 3,);
46+
$ignore = [
47+
1,
48+
3
49+
];
50+
51+
/*
52+
* Test enforcing a comma after the last array item.
53+
*/
54+
// phpcs:set NormalizedArrays.Arrays.CommaAfterLast singleLine enforce
55+
56+
$good = array( 1, 2, 3, );
57+
$good = [ 'a', 'b', 'c', ];
58+
59+
$missing = array( 1, 2, 3 );
60+
$missing = [ 'a', 'b', 'c' ];
61+
62+
// phpcs:set NormalizedArrays.Arrays.CommaAfterLast multiLine enforce
63+
64+
$good = array(
65+
1,
66+
3,
67+
);
68+
$good = [
69+
'a',
70+
'c',
71+
];
72+
73+
$goodHeredoc = function_call()->method_name( array(
74+
'phrase' => <<<EOD
75+
Here comes some text.
76+
EOD
77+
,
78+
) );
79+
80+
$missing = array(
81+
1,
82+
3
83+
);
84+
$missing = [
85+
'a',
86+
'c'/* Comment. */
87+
];
88+
89+
$missing = array(
90+
1, 2,
91+
3, 4);
92+
93+
$missing = [
94+
'1', '2',
95+
'3', '4'];
96+
97+
$missingInNested = array(
98+
1 => 'value' ,
99+
2 => [
100+
'a' => 'value' ,// phpcs:disable Standard.Category.Sniff - the extra spacing is fine, might be for alignment with other comments.
101+
'b' => array(
102+
1
103+
),
104+
'c' => apply_filters( 'filter', $input, $var )
105+
],
106+
3 => apply_filters( 'filter', $input, $var )/* phpcs:ignore Standard.Category.Sniff */
107+
);
108+
109+
$missing = array(
110+
'first',
111+
'second'
112+
//'third',
113+
);
114+
115+
$missingNowdoc = function_call()->method_name( array(
116+
'phrase' => <<<'EOD'
117+
Here comes some text.
118+
EOD
119+
) );
120+
121+
/*
122+
* Test forbidding a comma after the last array item.
123+
*/
124+
// phpcs:set NormalizedArrays.Arrays.CommaAfterLast singleLine forbid
125+
126+
$good = array( 1, 2, 3 );
127+
$good = [ 'a', 'b', 'c' ];
128+
129+
$found = array( 1, 2, 3, );
130+
$found = [ 'a', 'b', 'c', ];
131+
132+
// phpcs:set NormalizedArrays.Arrays.CommaAfterLast multiLine forbid
133+
134+
$good = array(
135+
1,
136+
3
137+
);
138+
$good = [
139+
'a',
140+
'c'
141+
];
142+
143+
$goodNowdoc = function_call()->method_name( array(
144+
'phrase' => <<<'EOD'
145+
Here comes some text.
146+
EOD
147+
) );
148+
149+
$found = array(
150+
1,
151+
3,/* Comment. */
152+
);
153+
$found = [
154+
'a',
155+
'c',
156+
];
157+
158+
$found = array(
159+
1, 2,
160+
3, 4,);
161+
162+
$found = [
163+
'1', '2',
164+
'3', '4',];
165+
166+
$foundInNested = array(
167+
1 => 'value' ,
168+
2 => [
169+
'a' => 'value' ,// phpcs:disable Standard.Category.Sniff - the extra spacing is fine, might be for alignment with other comments.
170+
'b' => array(
171+
1,
172+
),
173+
'c' => apply_filters( 'filter', $input, $var ),
174+
],
175+
3 => apply_filters( 'filter', $input, $var ), /* phpcs:ignore Standard.Category.Sniff */
176+
);
177+
178+
$foundHeredoc = function_call()->method_name( array(
179+
'phrase' => <<<"EOD"
180+
Here comes some text.
181+
EOD
182+
,
183+
) );
184+
185+
$foundHeredoc = function_call()->method_name( array(
186+
'phrase' => <<<"EOD"
187+
Here comes some text.
188+
EOD
189+
, /*comment*/
190+
) );
191+
192+
// Reset the properties to the defaults.
193+
// phpcs:set NormalizedArrays.Arrays.CommaAfterLast singleLine forbid
194+
// phpcs:set NormalizedArrays.Arrays.CommaAfterLast multiLine enforce
195+
// phpcs:enable
196+
197+
/*
198+
* Test live coding. This should be the last test in the file.
199+
*/
200+
// Intentional parse error.
201+
$ignore = array(

0 commit comments

Comments
 (0)