-
-
Notifications
You must be signed in to change notification settings - Fork 172
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SlevomatCodingStandard.Functions.DisallowTrailingCommaInClosureUse: N…
…ew sniff
- Loading branch information
Showing
6 changed files
with
129 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
SlevomatCodingStandard/Sniffs/Functions/DisallowTrailingCommaInClosureUseSniff.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace SlevomatCodingStandard\Sniffs\Functions; | ||
|
||
use PHP_CodeSniffer\Files\File; | ||
use PHP_CodeSniffer\Sniffs\Sniff; | ||
use SlevomatCodingStandard\Helpers\TokenHelper; | ||
use const T_CLOSURE; | ||
use const T_COMMA; | ||
use const T_USE; | ||
use const T_WHITESPACE; | ||
|
||
class DisallowTrailingCommaInClosureUseSniff implements Sniff | ||
{ | ||
|
||
public const CODE_DISALLOWED_TRAILING_COMMA = 'DisallowedTrailingComma'; | ||
|
||
/** | ||
* @return array<int, (int|string)> | ||
*/ | ||
public function register(): array | ||
{ | ||
return [ | ||
T_CLOSURE, | ||
]; | ||
} | ||
|
||
/** | ||
* @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint | ||
* @param int $functionPointer | ||
*/ | ||
public function process(File $phpcsFile, $functionPointer): void | ||
{ | ||
$tokens = $phpcsFile->getTokens(); | ||
|
||
$parenthesisCloserPointer = $tokens[$functionPointer]['parenthesis_closer']; | ||
|
||
$usePointer = TokenHelper::findNextEffective($phpcsFile, $parenthesisCloserPointer + 1); | ||
|
||
if ($tokens[$usePointer]['code'] !== T_USE) { | ||
return; | ||
} | ||
|
||
$useParenthesisOpener = TokenHelper::findNextEffective($phpcsFile, $usePointer + 1); | ||
|
||
$pointerBeforeUseParenthesisCloser = TokenHelper::findPreviousExcluding( | ||
$phpcsFile, | ||
T_WHITESPACE, | ||
$tokens[$useParenthesisOpener]['parenthesis_closer'] - 1, | ||
$useParenthesisOpener | ||
); | ||
|
||
if ($tokens[$pointerBeforeUseParenthesisCloser]['code'] !== T_COMMA) { | ||
return; | ||
} | ||
|
||
$fix = $phpcsFile->addFixableError( | ||
'Trailing comma after the last inherited variable in "use" of closure declaration is disallowed.', | ||
$pointerBeforeUseParenthesisCloser, | ||
self::CODE_DISALLOWED_TRAILING_COMMA | ||
); | ||
|
||
if (!$fix) { | ||
return; | ||
} | ||
|
||
$phpcsFile->fixer->beginChangeset(); | ||
$phpcsFile->fixer->replaceToken($pointerBeforeUseParenthesisCloser, ''); | ||
$phpcsFile->fixer->endChangeset(); | ||
} | ||
|
||
} |
27 changes: 27 additions & 0 deletions
27
tests/Sniffs/Functions/DisallowTrailingCommaInClosureUseSniffTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace SlevomatCodingStandard\Sniffs\Functions; | ||
|
||
use SlevomatCodingStandard\Sniffs\TestCase; | ||
|
||
class DisallowTrailingCommaInClosureUseSniffTest extends TestCase | ||
{ | ||
|
||
public function testNoErrors(): void | ||
{ | ||
$report = self::checkFile(__DIR__ . '/data/disallowTrailingCommaInClosureUseNoErrors.php'); | ||
self::assertNoSniffErrorInFile($report); | ||
} | ||
|
||
public function testErrors(): void | ||
{ | ||
$report = self::checkFile(__DIR__ . '/data/disallowTrailingCommaInClosureUseErrors.php'); | ||
|
||
self::assertSame(1, $report->getErrorCount()); | ||
|
||
self::assertSniffError($report, 5, DisallowTrailingCommaInClosureUseSniff::CODE_DISALLOWED_TRAILING_COMMA); | ||
|
||
self::assertAllFixedInFile($report); | ||
} | ||
|
||
} |
8 changes: 8 additions & 0 deletions
8
tests/Sniffs/Functions/data/disallowTrailingCommaInClosureUseErrors.fixed.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?php // lint >= 8.0 | ||
|
||
function () use ( | ||
$a, | ||
$b | ||
) { | ||
|
||
}; |
8 changes: 8 additions & 0 deletions
8
tests/Sniffs/Functions/data/disallowTrailingCommaInClosureUseErrors.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?php // lint >= 8.0 | ||
|
||
function () use ( | ||
$a, | ||
$b, | ||
) { | ||
|
||
}; |
10 changes: 10 additions & 0 deletions
10
tests/Sniffs/Functions/data/disallowTrailingCommaInClosureUseNoErrors.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
function () use ( | ||
$a, | ||
$b | ||
) { | ||
|
||
}; | ||
|
||
function () {}; |