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

Breadcrumb

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

function RequireTrailingCommaInDeclarationSniff::process

* @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint *

Parameters

int $functionPointer:

Overrides Sniff::process

File

vendor/slevomat/coding-standard/SlevomatCodingStandard/Sniffs/Functions/RequireTrailingCommaInDeclarationSniff.php, line 31

Class

RequireTrailingCommaInDeclarationSniff

Namespace

SlevomatCodingStandard\Sniffs\Functions

Code

public function process(File $phpcsFile, $functionPointer) : void {
    $this->enable = SniffSettingsHelper::isEnabledByPhpVersion($this->enable, 80000);
    if (!$this->enable) {
        return;
    }
    $tokens = $phpcsFile->getTokens();
    $parenthesisOpenerPointer = $tokens[$functionPointer]['parenthesis_opener'];
    $parenthesisCloserPointer = $tokens[$functionPointer]['parenthesis_closer'];
    if ($tokens[$parenthesisOpenerPointer]['line'] === $tokens[$parenthesisCloserPointer]['line']) {
        return;
    }
    $pointerBeforeParenthesisCloser = TokenHelper::findPreviousEffective($phpcsFile, $parenthesisCloserPointer - 1, $parenthesisOpenerPointer);
    if ($pointerBeforeParenthesisCloser === $parenthesisOpenerPointer) {
        return;
    }
    if ($tokens[$pointerBeforeParenthesisCloser]['code'] === T_COMMA) {
        return;
    }
    $fix = $phpcsFile->addFixableError('Multi-line function declaration must have a trailing comma after the last parameter.', $pointerBeforeParenthesisCloser, self::CODE_MISSING_TRAILING_COMMA);
    if (!$fix) {
        return;
    }
    $phpcsFile->fixer
        ->beginChangeset();
    $phpcsFile->fixer
        ->addContent($pointerBeforeParenthesisCloser, ',');
    $phpcsFile->fixer
        ->endChangeset();
}
RSS feed
Powered by Drupal