Skip to content

Commit

Permalink
SlevomatCodingStandard.Functions.DisallowTrailingCommaInClosureUse: N…
Browse files Browse the repository at this point in the history
…ew sniff
  • Loading branch information
kukulich committed May 6, 2022
1 parent 34dfb3c commit 9c16fd1
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 0 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -817,6 +817,10 @@ This sniff provides the following setting:

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

#### SlevomatCodingStandard.Functions.DisallowTrailingCommaInClosureUse 🔧

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

#### SlevomatCodingStandard.Functions.DisallowTrailingCommaInDeclaration 🔧

This sniff disallows trailing commas in multi-line declarations.
Expand Down
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();
}

}
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);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php // lint >= 8.0

function () use (
$a,
$b
) {

};
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php // lint >= 8.0

function () use (
$a,
$b,
) {

};
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

function () use (
$a,
$b
) {

};

function () {};

0 comments on commit 9c16fd1

Please sign in to comment.