Skip to content

Commit 1bcad5c

Browse files
committed
Add CommaAfterArrayItemSniff
1 parent 6dd3ddc commit 1bcad5c

File tree

3 files changed

+76
-3
lines changed

3 files changed

+76
-3
lines changed
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
3+
namespace PSR2R\Sniffs\PHP;
4+
5+
use PHP_CodeSniffer\Files\File;
6+
use PHP_CodeSniffer\Util\Tokens;
7+
use PSR2R\Tools\AbstractSniff;
8+
9+
/**
10+
* Adds trailing commas in multiline arrays.
11+
*
12+
* Heredoc patch taken from slevomat/coding-standard project.
13+
*
14+
* @author Mark Scherer
15+
* @license MIT
16+
*/
17+
class CommaAfterArrayItemSniff extends AbstractSniff {
18+
19+
/**
20+
* @var bool
21+
*/
22+
public $enableAfterHeredoc = PHP_VERSION_ID >= 70300;
23+
24+
/**
25+
* @inheritDoc
26+
*/
27+
public function register() {
28+
return [
29+
T_OPEN_SHORT_ARRAY,
30+
];
31+
}
32+
33+
/**
34+
* @inheritDoc
35+
*/
36+
public function process(File $phpcsFile, $stackPtr) {
37+
$tokens = $phpcsFile->getTokens();
38+
$arrayToken = $tokens[$stackPtr];
39+
$closeParenthesisPointer = $arrayToken['bracket_closer'];
40+
$openParenthesisToken = $tokens[$arrayToken['bracket_opener']];
41+
$closeParenthesisToken = $tokens[$closeParenthesisPointer];
42+
if ($openParenthesisToken['line'] === $closeParenthesisToken['line']) {
43+
return;
44+
}
45+
46+
$previousToCloseParenthesisPointer = $phpcsFile->findPrevious(Tokens::$emptyTokens, $closeParenthesisPointer - 1, 0, true);
47+
$previousToCloseParenthesisToken = $tokens[$previousToCloseParenthesisPointer];
48+
if (
49+
$previousToCloseParenthesisPointer === $arrayToken['bracket_opener']
50+
|| $previousToCloseParenthesisToken['code'] === T_COMMA
51+
|| $closeParenthesisToken['line'] === $previousToCloseParenthesisToken['line']
52+
) {
53+
return;
54+
}
55+
if (!$this->enableAfterHeredoc && in_array($previousToCloseParenthesisToken['code'], [T_END_HEREDOC, T_END_NOWDOC], true)) {
56+
return;
57+
}
58+
$fix = $phpcsFile->addFixableError(
59+
'Multi-line arrays must have a trailing comma after the last element.',
60+
$previousToCloseParenthesisPointer,
61+
'MissingTrailingComma'
62+
);
63+
if (!$fix) {
64+
return;
65+
}
66+
67+
$phpcsFile->fixer->beginChangeset();
68+
$phpcsFile->fixer->addContent($previousToCloseParenthesisPointer, ',');
69+
$phpcsFile->fixer->endChangeset();
70+
}
71+
72+
}

PSR2R/Tools/AbstractSniff.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ protected function getNamespaceInfo(File $phpcsFile) {
312312
return [
313313
'start' => $startIndex,
314314
'namespace' => $this->getNamespaceAsString($phpcsFile, $startIndex + 1, $endIndex - 1),
315-
'end' => $endIndex
315+
'end' => $endIndex,
316316
];
317317
}
318318

docs/sniffs.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# PSR2R Code Sniffer
22

33

4-
The PSR2R standard contains 126 sniffs
4+
The PSR2R standard contains 127 sniffs
55

66
Generic (21 sniffs)
77
-------------------
@@ -55,7 +55,7 @@ PSR2 (4 sniffs)
5555
- PSR2.Namespaces.NamespaceDeclaration
5656
- PSR2.Namespaces.UseDeclaration
5757

58-
PSR2R (67 sniffs)
58+
PSR2R (68 sniffs)
5959
-----------------
6060
- PSR2R.Classes.BraceOnSameLine
6161
- PSR2R.Classes.ClassCreateInstance
@@ -101,6 +101,7 @@ PSR2R (67 sniffs)
101101
- PSR2R.Namespaces.NoInlineFullyQualifiedClassName
102102
- PSR2R.Namespaces.UnusedUseStatement
103103
- PSR2R.Namespaces.UseInAlphabeticalOrder
104+
- PSR2R.PHP.CommaAfterArrayItem
104105
- PSR2R.PHP.DuplicateSemicolon
105106
- PSR2R.PHP.ListComma
106107
- PSR2R.PHP.NoIsNull

0 commit comments

Comments
 (0)