Skip to content

Commit a42989e

Browse files
committed
SlevomatCodingStandard.Functions.DisallowTrailingCommaInCall: New option "onlySingleLine"
1 parent 91c0d77 commit a42989e

7 files changed

+59
-3
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -807,6 +807,10 @@ This sniff disallows usage of named arguments.
807807

808808
This sniff disallows trailing commas in multi-line calls.
809809

810+
This sniff provides the following setting:
811+
812+
* `onlySingleLine`: to enable checks only for single-line calls.
813+
810814
#### SlevomatCodingStandard.Functions.RequireTrailingCommaInCall 🔧
811815

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

SlevomatCodingStandard/Sniffs/Functions/DisallowTrailingCommaInCallSniff.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ class DisallowTrailingCommaInCallSniff implements Sniff
2323

2424
public const CODE_DISALLOWED_TRAILING_COMMA = 'DisallowedTrailingComma';
2525

26+
/** @var bool */
27+
public $onlySingleLine = false;
28+
2629
/**
2730
* @return array<int, (int|string)>
2831
*/
@@ -64,6 +67,10 @@ public function process(File $phpcsFile, $parenthesisOpenerPointer): void
6467
return;
6568
}
6669

70+
if ($this->onlySingleLine && $tokens[$parenthesisOpenerPointer]['line'] !== $tokens[$parenthesisCloserPointer]['line']) {
71+
return;
72+
}
73+
6774
$fix = $phpcsFile->addFixableError(
6875
'Trailing comma after the last parameter in function call is disallowed.',
6976
$pointerBeforeParenthesisCloser,
@@ -76,6 +83,13 @@ public function process(File $phpcsFile, $parenthesisOpenerPointer): void
7683

7784
$phpcsFile->fixer->beginChangeset();
7885
$phpcsFile->fixer->replaceToken($pointerBeforeParenthesisCloser, '');
86+
87+
if ($tokens[$pointerBeforeParenthesisCloser]['line'] === $tokens[$parenthesisCloserPointer]['line']) {
88+
for ($i = $pointerBeforeParenthesisCloser + 1; $i < $parenthesisCloserPointer; $i++) {
89+
$phpcsFile->fixer->replaceToken($i, '');
90+
}
91+
}
92+
7993
$phpcsFile->fixer->endChangeset();
8094
}
8195

tests/Sniffs/Functions/DisallowTrailingCommaInCallSniffTest.php

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,19 @@ class DisallowTrailingCommaInCallSniffTest extends TestCase
99

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

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

20-
self::assertSame(14, $report->getErrorCount());
24+
self::assertSame(15, $report->getErrorCount());
2125

2226
self::assertSniffError($report, 5, DisallowTrailingCommaInCallSniff::CODE_DISALLOWED_TRAILING_COMMA);
2327
self::assertSniffError($report, 12, DisallowTrailingCommaInCallSniff::CODE_DISALLOWED_TRAILING_COMMA);
@@ -33,6 +37,20 @@ public function testErrors(): void
3337
self::assertSniffError($report, 75, DisallowTrailingCommaInCallSniff::CODE_DISALLOWED_TRAILING_COMMA);
3438
self::assertSniffError($report, 83, DisallowTrailingCommaInCallSniff::CODE_DISALLOWED_TRAILING_COMMA);
3539
self::assertSniffError($report, 91, DisallowTrailingCommaInCallSniff::CODE_DISALLOWED_TRAILING_COMMA);
40+
self::assertSniffError($report, 97, DisallowTrailingCommaInCallSniff::CODE_DISALLOWED_TRAILING_COMMA);
41+
42+
self::assertAllFixedInFile($report);
43+
}
44+
45+
public function testWithOnlySingleLineEnabledErrors(): void
46+
{
47+
$report = self::checkFile(__DIR__ . '/data/disallowTrailingCommaInCallWithOnlySingleLineEnabledErrors.php', [
48+
'onlySingleLine' => true,
49+
]);
50+
51+
self::assertSame(1, $report->getErrorCount());
52+
53+
self::assertSniffError($report, 8, DisallowTrailingCommaInCallSniff::CODE_DISALLOWED_TRAILING_COMMA);
3654

3755
self::assertAllFixedInFile($report);
3856
}

tests/Sniffs/Functions/data/disallowTrailingCommaInCallErrors.fixed.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,3 +93,5 @@ public static function createParent($a, $b)
9393
}
9494

9595
}
96+
97+
doSomething(1, 2, 3);

tests/Sniffs/Functions/data/disallowTrailingCommaInCallErrors.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,3 +93,5 @@ public static function createParent($a, $b)
9393
}
9494

9595
}
96+
97+
doSomething(1, 2, 3, );
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php // lint >= 7.3
2+
3+
max(
4+
0,
5+
1,
6+
);
7+
8+
doSomething(1, 2, 3);
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php // lint >= 7.3
2+
3+
max(
4+
0,
5+
1,
6+
);
7+
8+
doSomething(1, 2, 3, );

0 commit comments

Comments
 (0)