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

Breadcrumb

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

function RequireTrailingCommaInCallSniff::process

* @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint *

Parameters

int $parenthesisOpenerPointer:

Overrides Sniff::process

File

vendor/slevomat/coding-standard/SlevomatCodingStandard/Sniffs/Functions/RequireTrailingCommaInCallSniff.php, line 44

Class

RequireTrailingCommaInCallSniff

Namespace

SlevomatCodingStandard\Sniffs\Functions

Code

public function process(File $phpcsFile, $parenthesisOpenerPointer) : void {
    $this->enable = SniffSettingsHelper::isEnabledByPhpVersion($this->enable, 70300);
    if (!$this->enable) {
        return;
    }
    $tokens = $phpcsFile->getTokens();
    if (array_key_exists('parenthesis_owner', $tokens[$parenthesisOpenerPointer])) {
        return;
    }
    $pointerBeforeParenthesisOpener = TokenHelper::findPreviousEffective($phpcsFile, $parenthesisOpenerPointer - 1);
    if (!in_array($tokens[$pointerBeforeParenthesisOpener]['code'], array_merge(TokenHelper::getOnlyNameTokenCodes(), [
        T_VARIABLE,
        T_ISSET,
        T_UNSET,
        T_CLOSE_PARENTHESIS,
        T_SELF,
        T_STATIC,
        T_PARENT,
    ]), true)) {
        return;
    }
    $parenthesisCloserPointer = $tokens[$parenthesisOpenerPointer]['parenthesis_closer'];
    if ($tokens[$parenthesisOpenerPointer]['line'] === $tokens[$parenthesisCloserPointer]['line']) {
        return;
    }
    $pointerBeforeParenthesisCloser = TokenHelper::findPreviousEffective($phpcsFile, $parenthesisCloserPointer - 1);
    if ($pointerBeforeParenthesisCloser === $parenthesisOpenerPointer) {
        return;
    }
    if ($tokens[$parenthesisCloserPointer]['line'] === $tokens[$pointerBeforeParenthesisCloser]['line']) {
        return;
    }
    if ($tokens[$pointerBeforeParenthesisCloser]['code'] === T_COMMA) {
        return;
    }
    $fix = $phpcsFile->addFixableError('Multi-line function calls 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();
}

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal