Skip to content

Commit 91c0d77

Browse files
committed
SlevomatCodingStandard.Functions.DisallowTrailingCommaInClosureUse: New option "onlySingleLine"
1 parent 5fa099f commit 91c0d77

7 files changed

+75
-6
lines changed

README.md

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

822822
This sniff disallows trailing commas in multi-line `use` of closure declaration.
823823

824+
This sniff provides the following setting:
825+
826+
* `onlySingleLine`: to enable checks only for single-line `use` declarations.
827+
824828
#### SlevomatCodingStandard.Functions.RequireTrailingCommaInClosureUse 🔧
825829

826830
Commas after the last inherited variable in multi-line `use` of closure declaration make adding a new variable easier and result in a cleaner versioning diff.

SlevomatCodingStandard/Sniffs/Functions/DisallowTrailingCommaInClosureUseSniff.php

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ class DisallowTrailingCommaInClosureUseSniff implements Sniff
1515

1616
public const CODE_DISALLOWED_TRAILING_COMMA = 'DisallowedTrailingComma';
1717

18+
/** @var bool */
19+
public $onlySingleLine = false;
20+
1821
/**
1922
* @return array<int, (int|string)>
2023
*/
@@ -41,19 +44,24 @@ public function process(File $phpcsFile, $functionPointer): void
4144
return;
4245
}
4346

44-
$useParenthesisOpener = TokenHelper::findNextEffective($phpcsFile, $usePointer + 1);
47+
$useParenthesisOpenerPointer = TokenHelper::findNextEffective($phpcsFile, $usePointer + 1);
48+
$useParenthesisCloserPointer = $tokens[$useParenthesisOpenerPointer]['parenthesis_closer'];
4549

4650
$pointerBeforeUseParenthesisCloser = TokenHelper::findPreviousExcluding(
4751
$phpcsFile,
4852
T_WHITESPACE,
49-
$tokens[$useParenthesisOpener]['parenthesis_closer'] - 1,
50-
$useParenthesisOpener
53+
$tokens[$useParenthesisOpenerPointer]['parenthesis_closer'] - 1,
54+
$useParenthesisOpenerPointer
5155
);
5256

5357
if ($tokens[$pointerBeforeUseParenthesisCloser]['code'] !== T_COMMA) {
5458
return;
5559
}
5660

61+
if ($this->onlySingleLine && $tokens[$useParenthesisOpenerPointer]['line'] !== $tokens[$useParenthesisCloserPointer]['line']) {
62+
return;
63+
}
64+
5765
$fix = $phpcsFile->addFixableError(
5866
'Trailing comma after the last inherited variable in "use" of closure declaration is disallowed.',
5967
$pointerBeforeUseParenthesisCloser,
@@ -66,6 +74,13 @@ public function process(File $phpcsFile, $functionPointer): void
6674

6775
$phpcsFile->fixer->beginChangeset();
6876
$phpcsFile->fixer->replaceToken($pointerBeforeUseParenthesisCloser, '');
77+
78+
if ($tokens[$pointerBeforeUseParenthesisCloser]['line'] === $tokens[$useParenthesisCloserPointer]['line']) {
79+
for ($i = $pointerBeforeUseParenthesisCloser + 1; $i < $useParenthesisCloserPointer; $i++) {
80+
$phpcsFile->fixer->replaceToken($i, '');
81+
}
82+
}
83+
6984
$phpcsFile->fixer->endChangeset();
7085
}
7186

tests/Sniffs/Functions/DisallowTrailingCommaInClosureUseSniffTest.php

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,35 @@ class DisallowTrailingCommaInClosureUseSniffTest extends TestCase
99

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

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

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

2226
self::assertSniffError($report, 5, DisallowTrailingCommaInClosureUseSniff::CODE_DISALLOWED_TRAILING_COMMA);
27+
self::assertSniffError($report, 10, DisallowTrailingCommaInClosureUseSniff::CODE_DISALLOWED_TRAILING_COMMA);
28+
29+
self::assertAllFixedInFile($report);
30+
}
31+
32+
public function testErrorsWithOnlySingleLineEnabled(): void
33+
{
34+
$report = self::checkFile(__DIR__ . '/data/disallowTrailingCommaInClosureUseWithOnlySingleLineEnabledErrors.php', [
35+
'onlySingleLine' => true,
36+
]);
37+
38+
self::assertSame(1, $report->getErrorCount());
39+
40+
self::assertSniffError($report, 10, DisallowTrailingCommaInClosureUseSniff::CODE_DISALLOWED_TRAILING_COMMA);
2341

2442
self::assertAllFixedInFile($report);
2543
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,7 @@ function () use (
66
) {
77

88
};
9+
10+
function () use ($a, $b) {
11+
12+
};

tests/Sniffs/Functions/data/disallowTrailingCommaInClosureUseErrors.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,7 @@ function () use (
66
) {
77

88
};
9+
10+
function () use ($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 () use (
4+
$a,
5+
$b,
6+
) {
7+
8+
};
9+
10+
function () use ($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 () use (
4+
$a,
5+
$b,
6+
) {
7+
8+
};
9+
10+
function () use ($a, $b, ) {
11+
12+
};

0 commit comments

Comments
 (0)