Skip to content

Commit 89d7f55

Browse files
authored
Merge pull request #284 from stronk7/multiline_closer_same_line
Split the errors, adding the *CloserSameLine ones
2 parents 5bda2df + ef6ab26 commit 89d7f55

File tree

4 files changed

+111
-0
lines changed

4 files changed

+111
-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: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,56 @@ EOD
166166
, /*comment*/
167167
) );
168168

169+
/**
170+
* Tests enforcing a comma after the last array item when the closer is in the same line. See #283.
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+
$missing_but_good = [
183+
'1', '2',
184+
'3', '4']; // phpcs:ignore NormalizedArrays.Arrays.CommaAfterLast.MissingMultiLineCloserSameLine
185+
186+
$good = array(
187+
1, 2,
188+
3, 4,);
189+
190+
$good = [
191+
'1', '2',
192+
'3', '4',];
193+
194+
/**
195+
* Tests forbidding a comma after the last array item when the closer is in the same line. See #283.
196+
*/
197+
// phpcs:set NormalizedArrays.Arrays.CommaAfterLast multiLine forbid
198+
199+
$found = array(
200+
1, 2,
201+
3, 4,);
202+
203+
$found = [
204+
'1', '2',
205+
'3', '4',];
206+
207+
$found_but_good = [
208+
'1', '2',
209+
'3', '4',]; // phpcs:ignore NormalizedArrays.Arrays.CommaAfterLast.FoundMultiLineCloserSameLine
210+
211+
$good = array(
212+
1, 2,
213+
3, 4);
214+
215+
$good = [
216+
'1', '2',
217+
'3', '4'];
218+
169219
// Reset the properties to the defaults.
170220
// phpcs:set NormalizedArrays.Arrays.CommaAfterLast singleLine forbid
171221
// phpcs:set NormalizedArrays.Arrays.CommaAfterLast multiLine enforce

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

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

169+
/**
170+
* Tests enforcing a comma after the last array item when the closer is in the same line. See #283.
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+
$missing_but_good = [
183+
'1', '2',
184+
'3', '4']; // phpcs:ignore NormalizedArrays.Arrays.CommaAfterLast.MissingMultiLineCloserSameLine
185+
186+
$good = array(
187+
1, 2,
188+
3, 4,);
189+
190+
$good = [
191+
'1', '2',
192+
'3', '4',];
193+
194+
/**
195+
* Tests forbidding a comma after the last array item when the closer is in the same line. See #283.
196+
*/
197+
// phpcs:set NormalizedArrays.Arrays.CommaAfterLast multiLine forbid
198+
199+
$found = array(
200+
1, 2,
201+
3, 4);
202+
203+
$found = [
204+
'1', '2',
205+
'3', '4'];
206+
207+
$found_but_good = [
208+
'1', '2',
209+
'3', '4',]; // phpcs:ignore NormalizedArrays.Arrays.CommaAfterLast.FoundMultiLineCloserSameLine
210+
211+
$good = array(
212+
1, 2,
213+
3, 4);
214+
215+
$good = [
216+
'1', '2',
217+
'3', '4'];
218+
169219
// Reset the properties to the defaults.
170220
// phpcs:set NormalizedArrays.Arrays.CommaAfterLast singleLine forbid
171221
// 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+
201 => 1,
58+
205 => 1,
5559
];
5660

5761
case 'CommaAfterLastUnitTest.2.inc':

0 commit comments

Comments
 (0)