Skip to content

Commit 9c16fd1

Browse files
committed
SlevomatCodingStandard.Functions.DisallowTrailingCommaInClosureUse: New sniff
1 parent 34dfb3c commit 9c16fd1

File tree

6 files changed

+129
-0
lines changed

6 files changed

+129
-0
lines changed

README.md

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

818818
* `enable`: either to enable or not this sniff. By default, it is enabled for PHP versions 7.3 or higher.
819819

820+
#### SlevomatCodingStandard.Functions.DisallowTrailingCommaInClosureUse 🔧
821+
822+
This sniff disallows trailing commas in multi-line `use` of closure declaration.
823+
820824
#### SlevomatCodingStandard.Functions.DisallowTrailingCommaInDeclaration 🔧
821825

822826
This sniff disallows trailing commas in multi-line declarations.
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace SlevomatCodingStandard\Sniffs\Functions;
4+
5+
use PHP_CodeSniffer\Files\File;
6+
use PHP_CodeSniffer\Sniffs\Sniff;
7+
use SlevomatCodingStandard\Helpers\TokenHelper;
8+
use const T_CLOSURE;
9+
use const T_COMMA;
10+
use const T_USE;
11+
use const T_WHITESPACE;
12+
13+
class DisallowTrailingCommaInClosureUseSniff implements Sniff
14+
{
15+
16+
public const CODE_DISALLOWED_TRAILING_COMMA = 'DisallowedTrailingComma';
17+
18+
/**
19+
* @return array<int, (int|string)>
20+
*/
21+
public function register(): array
22+
{
23+
return [
24+
T_CLOSURE,
25+
];
26+
}
27+
28+
/**
29+
* @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint
30+
* @param int $functionPointer
31+
*/
32+
public function process(File $phpcsFile, $functionPointer): void
33+
{
34+
$tokens = $phpcsFile->getTokens();
35+
36+
$parenthesisCloserPointer = $tokens[$functionPointer]['parenthesis_closer'];
37+
38+
$usePointer = TokenHelper::findNextEffective($phpcsFile, $parenthesisCloserPointer + 1);
39+
40+
if ($tokens[$usePointer]['code'] !== T_USE) {
41+
return;
42+
}
43+
44+
$useParenthesisOpener = TokenHelper::findNextEffective($phpcsFile, $usePointer + 1);
45+
46+
$pointerBeforeUseParenthesisCloser = TokenHelper::findPreviousExcluding(
47+
$phpcsFile,
48+
T_WHITESPACE,
49+
$tokens[$useParenthesisOpener]['parenthesis_closer'] - 1,
50+
$useParenthesisOpener
51+
);
52+
53+
if ($tokens[$pointerBeforeUseParenthesisCloser]['code'] !== T_COMMA) {
54+
return;
55+
}
56+
57+
$fix = $phpcsFile->addFixableError(
58+
'Trailing comma after the last inherited variable in "use" of closure declaration is disallowed.',
59+
$pointerBeforeUseParenthesisCloser,
60+
self::CODE_DISALLOWED_TRAILING_COMMA
61+
);
62+
63+
if (!$fix) {
64+
return;
65+
}
66+
67+
$phpcsFile->fixer->beginChangeset();
68+
$phpcsFile->fixer->replaceToken($pointerBeforeUseParenthesisCloser, '');
69+
$phpcsFile->fixer->endChangeset();
70+
}
71+
72+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace SlevomatCodingStandard\Sniffs\Functions;
4+
5+
use SlevomatCodingStandard\Sniffs\TestCase;
6+
7+
class DisallowTrailingCommaInClosureUseSniffTest extends TestCase
8+
{
9+
10+
public function testNoErrors(): void
11+
{
12+
$report = self::checkFile(__DIR__ . '/data/disallowTrailingCommaInClosureUseNoErrors.php');
13+
self::assertNoSniffErrorInFile($report);
14+
}
15+
16+
public function testErrors(): void
17+
{
18+
$report = self::checkFile(__DIR__ . '/data/disallowTrailingCommaInClosureUseErrors.php');
19+
20+
self::assertSame(1, $report->getErrorCount());
21+
22+
self::assertSniffError($report, 5, DisallowTrailingCommaInClosureUseSniff::CODE_DISALLOWED_TRAILING_COMMA);
23+
24+
self::assertAllFixedInFile($report);
25+
}
26+
27+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php // lint >= 8.0
2+
3+
function () use (
4+
$a,
5+
$b
6+
) {
7+
8+
};
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php // lint >= 8.0
2+
3+
function () use (
4+
$a,
5+
$b,
6+
) {
7+
8+
};
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
function () use (
4+
$a,
5+
$b
6+
) {
7+
8+
};
9+
10+
function () {};

0 commit comments

Comments
 (0)