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

Breadcrumb

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

class DisallowTrailingCommaInDeclarationSniff

Hierarchy

  • class \SlevomatCodingStandard\Sniffs\Functions\DisallowTrailingCommaInDeclarationSniff implements \PHP_CodeSniffer\Sniffs\Sniff

Expanded class hierarchy of DisallowTrailingCommaInDeclarationSniff

File

vendor/slevomat/coding-standard/SlevomatCodingStandard/Sniffs/Functions/DisallowTrailingCommaInDeclarationSniff.php, line 12

Namespace

SlevomatCodingStandard\Sniffs\Functions
View source
class DisallowTrailingCommaInDeclarationSniff implements Sniff {
    public const CODE_DISALLOWED_TRAILING_COMMA = 'DisallowedTrailingComma';
    
    /** @var bool */
    public $onlySingleLine = false;
    
    /**
     * @return array<int, (int|string)>
     */
    public function register() : array {
        return TokenHelper::$functionTokenCodes;
    }
    
    /**
     * @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint
     * @param int $functionPointer
     */
    public function process(File $phpcsFile, $functionPointer) : void {
        $tokens = $phpcsFile->getTokens();
        $parenthesisOpenerPointer = $tokens[$functionPointer]['parenthesis_opener'];
        $parenthesisCloserPointer = $tokens[$functionPointer]['parenthesis_closer'];
        $pointerBeforeParenthesisCloser = TokenHelper::findPreviousExcluding($phpcsFile, T_WHITESPACE, $parenthesisCloserPointer - 1, $parenthesisOpenerPointer);
        if ($tokens[$pointerBeforeParenthesisCloser]['code'] !== T_COMMA) {
            return;
        }
        if ($this->onlySingleLine && $tokens[$parenthesisOpenerPointer]['line'] !== $tokens[$parenthesisCloserPointer]['line']) {
            return;
        }
        $fix = $phpcsFile->addFixableError('Trailing comma after the last parameter in function declaration is disallowed.', $pointerBeforeParenthesisCloser, self::CODE_DISALLOWED_TRAILING_COMMA);
        if (!$fix) {
            return;
        }
        $phpcsFile->fixer
            ->beginChangeset();
        $phpcsFile->fixer
            ->replaceToken($pointerBeforeParenthesisCloser, '');
        if ($tokens[$pointerBeforeParenthesisCloser]['line'] === $tokens[$parenthesisCloserPointer]['line']) {
            FixerHelper::removeBetween($phpcsFile, $pointerBeforeParenthesisCloser, $parenthesisCloserPointer);
        }
        $phpcsFile->fixer
            ->endChangeset();
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title
DisallowTrailingCommaInDeclarationSniff::$onlySingleLine public property @var bool
DisallowTrailingCommaInDeclarationSniff::CODE_DISALLOWED_TRAILING_COMMA public constant
DisallowTrailingCommaInDeclarationSniff::process public function * @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint
*
Overrides Sniff::process
DisallowTrailingCommaInDeclarationSniff::register public function * Overrides Sniff::register

API Navigation

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