Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. DisallowTrailingCommaInClosureUseSniff.php

function DisallowTrailingCommaInClosureUseSniff::process

* @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint *

Parameters

int $functionPointer:

Overrides Sniff::process

File

vendor/slevomat/coding-standard/SlevomatCodingStandard/Sniffs/Functions/DisallowTrailingCommaInClosureUseSniff.php, line 36

Class

DisallowTrailingCommaInClosureUseSniff

Namespace

SlevomatCodingStandard\Sniffs\Functions

Code

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;
    }
    $useParenthesisOpenerPointer = TokenHelper::findNextEffective($phpcsFile, $usePointer + 1);
    $useParenthesisCloserPointer = $tokens[$useParenthesisOpenerPointer]['parenthesis_closer'];
    $pointerBeforeUseParenthesisCloser = TokenHelper::findPreviousExcluding($phpcsFile, T_WHITESPACE, $tokens[$useParenthesisOpenerPointer]['parenthesis_closer'] - 1, $useParenthesisOpenerPointer);
    if ($tokens[$pointerBeforeUseParenthesisCloser]['code'] !== T_COMMA) {
        return;
    }
    if ($this->onlySingleLine && $tokens[$useParenthesisOpenerPointer]['line'] !== $tokens[$useParenthesisCloserPointer]['line']) {
        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, '');
    if ($tokens[$pointerBeforeUseParenthesisCloser]['line'] === $tokens[$useParenthesisCloserPointer]['line']) {
        FixerHelper::removeBetween($phpcsFile, $pointerBeforeUseParenthesisCloser, $useParenthesisCloserPointer);
    }
    $phpcsFile->fixer
        ->endChangeset();
}
RSS feed
Powered by Drupal