Skip to content

Commit 3d04a22

Browse files
committed
SlevomatCodingStandard.Functions.DisallowTrailingCommaInDeclaration: New option "onlySingleLine"
1 parent a42989e commit 3d04a22

5 files changed

+61
-2
lines changed

‎README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -843,6 +843,10 @@ This sniff provides the following setting:
843843

844844
This sniff disallows trailing commas in multi-line declarations.
845845

846+
This sniff provides the following setting:
847+
848+
* `onlySingleLine`: to enable checks only for single-line declarations.
849+
846850
#### SlevomatCodingStandard.Functions.RequireTrailingCommaInDeclaration 🔧
847851

848852
Commas after the last parameter in function or method declaration make adding a new parameter easier and result in a cleaner versioning diff.

‎SlevomatCodingStandard/Sniffs/Functions/DisallowTrailingCommaInDeclarationSniff.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ class DisallowTrailingCommaInDeclarationSniff implements Sniff
1313

1414
public const CODE_DISALLOWED_TRAILING_COMMA = 'DisallowedTrailingComma';
1515

16+
/** @var bool */
17+
public $onlySingleLine = false;
18+
1619
/**
1720
* @return array<int, (int|string)>
1821
*/
@@ -43,6 +46,10 @@ public function process(File $phpcsFile, $functionPointer): void
4346
return;
4447
}
4548

49+
if ($this->onlySingleLine && $tokens[$parenthesisOpenerPointer]['line'] !== $tokens[$parenthesisCloserPointer]['line']) {
50+
return;
51+
}
52+
4653
$fix = $phpcsFile->addFixableError(
4754
'Trailing comma after the last parameter in function declaration is disallowed.',
4855
$pointerBeforeParenthesisCloser,
@@ -55,6 +62,13 @@ public function process(File $phpcsFile, $functionPointer): void
5562

5663
$phpcsFile->fixer->beginChangeset();
5764
$phpcsFile->fixer->replaceToken($pointerBeforeParenthesisCloser, '');
65+
66+
if ($tokens[$pointerBeforeParenthesisCloser]['line'] === $tokens[$parenthesisCloserPointer]['line']) {
67+
for ($i = $pointerBeforeParenthesisCloser + 1; $i < $parenthesisCloserPointer; $i++) {
68+
$phpcsFile->fixer->replaceToken($i, '');
69+
}
70+
}
71+
5872
$phpcsFile->fixer->endChangeset();
5973
}
6074

‎tests/Sniffs/Functions/DisallowTrailingCommaInDeclarationSniffTest.php

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,17 @@ class DisallowTrailingCommaInDeclarationSniffTest extends TestCase
99

1010
public function testNoErrors(): void
1111
{
12-
$report = self::checkFile(__DIR__ . '/data/disallowTrailingCommaInDeclarationNoErrors.php');
12+
$report = self::checkFile(__DIR__ . '/data/disallowTrailingCommaInDeclarationNoErrors.php', [
13+
'onlySingleLine' => false,
14+
]);
1315
self::assertNoSniffErrorInFile($report);
1416
}
1517

1618
public function testErrors(): void
1719
{
18-
$report = self::checkFile(__DIR__ . '/data/disallowTrailingCommaInDeclarationErrors.php');
20+
$report = self::checkFile(__DIR__ . '/data/disallowTrailingCommaInDeclarationErrors.php', [
21+
'onlySingleLine' => false,
22+
]);
1923

2024
self::assertSame(4, $report->getErrorCount());
2125

@@ -27,4 +31,17 @@ public function testErrors(): void
2731
self::assertAllFixedInFile($report);
2832
}
2933

34+
public function testWithOnlySingleLineEnabledErrors(): void
35+
{
36+
$report = self::checkFile(__DIR__ . '/data/disallowTrailingCommaInDeclarationWithOnlySingleLineEnabledErrors.php', [
37+
'onlySingleLine' => true,
38+
]);
39+
40+
self::assertSame(1, $report->getErrorCount());
41+
42+
self::assertSniffError($report, 10, DisallowTrailingCommaInDeclarationSniff::CODE_DISALLOWED_TRAILING_COMMA);
43+
44+
self::assertAllFixedInFile($report);
45+
}
46+
3047
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php // lint >= 8.0
2+
3+
function something(
4+
$a,
5+
$b,
6+
) {
7+
8+
}
9+
10+
function anything($a, $b) {
11+
12+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php // lint >= 8.0
2+
3+
function something(
4+
$a,
5+
$b,
6+
) {
7+
8+
}
9+
10+
function anything($a, $b, ) {
11+
12+
}

0 commit comments

Comments
 (0)
close