Skip to content

Commit fd1ecd5

Browse files
committed
TrailingArrayCommaSniff should not report errors for empty multiline arrays
1 parent 6f57667 commit fd1ecd5

File tree

5 files changed

+42
-8
lines changed

5 files changed

+42
-8
lines changed

SlevomatCodingStandard/Sniffs/Arrays/TrailingArrayCommaSniff.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,11 @@ public function process(\PHP_CodeSniffer_File $phpcsFile, $stackPointer)
3737

3838
$previousToCloseParenthesisPointer = TokenHelper::findPreviousEffective($phpcsFile, $closeParenthesisPointer - 1);
3939
$previousToCloseParenthesisToken = $tokens[$previousToCloseParenthesisPointer];
40-
if ($previousToCloseParenthesisToken['code'] !== T_COMMA && $closeParenthesisToken['line'] !== $previousToCloseParenthesisToken['line']) {
40+
if (
41+
$previousToCloseParenthesisPointer !== $arrayToken['bracket_opener']
42+
&& $previousToCloseParenthesisToken['code'] !== T_COMMA
43+
&& $closeParenthesisToken['line'] !== $previousToCloseParenthesisToken['line']
44+
) {
4145
$fix = $phpcsFile->addFixableError(
4246
'Multiline arrays must have a trailing comma after the last element.',
4347
$previousToCloseParenthesisPointer,

tests/Sniffs/Arrays/TrailingArrayCommaSniffTest.php

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,12 @@ class TrailingArrayCommaSniffTest extends \SlevomatCodingStandard\Sniffs\TestCas
77

88
public function testCheckFile()
99
{
10-
$resultFile = $this->checkFile(__DIR__ . '/data/trailingCommas.php');
11-
$this->assertNoSniffError($resultFile, 3);
12-
$this->assertNoSniffError($resultFile, 10);
13-
$this->assertNoSniffError($resultFile, 12);
14-
$this->assertSniffError($resultFile, 18, TrailingArrayCommaSniff::CODE_MISSING_TRAILING_COMMA);
15-
$this->assertSniffError($resultFile, 26, TrailingArrayCommaSniff::CODE_MISSING_TRAILING_COMMA);
16-
$this->assertNoSniffError($resultFile, 28);
10+
$report = $this->checkFile(__DIR__ . '/data/trailingCommas.php');
11+
12+
$this->assertSame(2, $report->getErrorCount());
13+
14+
$this->assertSniffError($report, 18, TrailingArrayCommaSniff::CODE_MISSING_TRAILING_COMMA);
15+
$this->assertSniffError($report, 26, TrailingArrayCommaSniff::CODE_MISSING_TRAILING_COMMA);
1716
}
1817

1918
public function testFixable()

tests/Sniffs/Arrays/data/fixableTrailingCommas.fixed.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,13 @@
2727
],
2828
5,
2929
];
30+
31+
[
32+
//
33+
];
34+
35+
[
36+
1,
37+
//
38+
3,
39+
];

tests/Sniffs/Arrays/data/fixableTrailingCommas.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,13 @@
2727
],
2828
5,
2929
];
30+
31+
[
32+
//
33+
];
34+
35+
[
36+
1,
37+
//
38+
3
39+
];

tests/Sniffs/Arrays/data/trailingCommas.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,14 @@
2727
],
2828
5,
2929
];
30+
31+
$a5 = [
32+
//
33+
];
34+
35+
$a6 = [
36+
1,
37+
//
38+
3,
39+
];
40+

0 commit comments

Comments
 (0)