Skip to content

Commit 1694346

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.
1 parent 5bda2df commit 1694346

File tree

4 files changed

+95
-0
lines changed

4 files changed

+95
-0
lines changed

NormalizedArrays/Sniffs/Arrays/CommaAfterLastSniff.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,13 @@ public function process(File $phpcsFile, $stackPtr)
145145
return;
146146
}
147147

148+
// If the closer is on the same line as the last element, change the error code for multi-line arrays.
149+
if ($errorCode === 'MultiLine'
150+
&& $tokens[$lastNonEmpty]['line'] === $tokens[$closer]['line']
151+
) {
152+
$errorCode .= 'CloserSameLine';
153+
}
154+
148155
$isComma = ($tokens[$lastNonEmpty]['code'] === \T_COMMA);
149156

150157
$phpcsFile->recordMetric(

NormalizedArrays/Tests/Arrays/CommaAfterLastUnitTest.1.inc

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,48 @@ EOD
166166
, /*comment*/
167167
) );
168168

169+
/**
170+
* Tests enforcing a comma after the last array item when the closer is in the same line.
171+
*/
172+
// phpcs:set NormalizedArrays.Arrays.CommaAfterLast multiLine enforce
173+
174+
$missing = array(
175+
1, 2,
176+
3, 4);
177+
178+
$missing = [
179+
'1', '2',
180+
'3', '4'];
181+
182+
$good = array(
183+
1, 2,
184+
3, 4,);
185+
186+
$good = [
187+
'1', '2',
188+
'3', '4',];
189+
190+
/**
191+
* Tests forbidding a comma after the last array item when the closer is in the same line.
192+
*/
193+
// phpcs:set NormalizedArrays.Arrays.CommaAfterLast multiLine forbid
194+
195+
$found = array(
196+
1, 2,
197+
3, 4,);
198+
199+
$found = [
200+
'1', '2',
201+
'3', '4',];
202+
203+
$good = array(
204+
1, 2,
205+
3, 4);
206+
207+
$good = [
208+
'1', '2',
209+
'3', '4'];
210+
169211
// Reset the properties to the defaults.
170212
// phpcs:set NormalizedArrays.Arrays.CommaAfterLast singleLine forbid
171213
// phpcs:set NormalizedArrays.Arrays.CommaAfterLast multiLine enforce

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

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,48 @@ EOD
166166
/*comment*/
167167
) );
168168

169+
/**
170+
* Tests enforcing a comma after the last array item when the closer is in the same line.
171+
*/
172+
// phpcs:set NormalizedArrays.Arrays.CommaAfterLast multiLine enforce
173+
174+
$missing = array(
175+
1, 2,
176+
3, 4,);
177+
178+
$missing = [
179+
'1', '2',
180+
'3', '4',];
181+
182+
$good = array(
183+
1, 2,
184+
3, 4,);
185+
186+
$good = [
187+
'1', '2',
188+
'3', '4',];
189+
190+
/**
191+
* Tests forbidding a comma after the last array item when the closer is in the same line.
192+
*/
193+
// phpcs:set NormalizedArrays.Arrays.CommaAfterLast multiLine forbid
194+
195+
$found = array(
196+
1, 2,
197+
3, 4);
198+
199+
$found = [
200+
'1', '2',
201+
'3', '4'];
202+
203+
$good = array(
204+
1, 2,
205+
3, 4);
206+
207+
$good = [
208+
'1', '2',
209+
'3', '4'];
210+
169211
// Reset the properties to the defaults.
170212
// phpcs:set NormalizedArrays.Arrays.CommaAfterLast singleLine forbid
171213
// phpcs:set NormalizedArrays.Arrays.CommaAfterLast multiLine enforce

NormalizedArrays/Tests/Arrays/CommaAfterLastUnitTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ public function getErrorList($testFile = '')
5252
152 => 1,
5353
159 => 1,
5454
166 => 1,
55+
176 => 1,
56+
180 => 1,
57+
197 => 1,
58+
201 => 1,
5559
];
5660

5761
case 'CommaAfterLastUnitTest.2.inc':

0 commit comments

Comments
 (0)