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

Breadcrumb

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

class RequireTrailingCommaInClosureUseSniff

Hierarchy

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

Expanded class hierarchy of RequireTrailingCommaInClosureUseSniff

File

vendor/slevomat/coding-standard/SlevomatCodingStandard/Sniffs/Functions/RequireTrailingCommaInClosureUseSniff.php, line 14

Namespace

SlevomatCodingStandard\Sniffs\Functions
View source
class RequireTrailingCommaInClosureUseSniff implements Sniff {
    public const CODE_MISSING_TRAILING_COMMA = 'MissingTrailingComma';
    
    /** @var bool|null */
    public $enable = null;
    
    /**
     * @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 {
        $this->enable = SniffSettingsHelper::isEnabledByPhpVersion($this->enable, 80000);
        if (!$this->enable) {
            return;
        }
        $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'];
        if ($tokens[$useParenthesisOpenerPointer]['line'] === $tokens[$useParenthesisCloserPointer]['line']) {
            return;
        }
        $pointerBeforeUseParenthesisCloser = TokenHelper::findPreviousExcluding($phpcsFile, T_WHITESPACE, $useParenthesisCloserPointer - 1, $useParenthesisOpenerPointer);
        if ($tokens[$pointerBeforeUseParenthesisCloser]['code'] === T_COMMA) {
            return;
        }
        $fix = $phpcsFile->addFixableError('Multi-line "use" of closure declaration must have a trailing comma after the last inherited variable.', $pointerBeforeUseParenthesisCloser, self::CODE_MISSING_TRAILING_COMMA);
        if (!$fix) {
            return;
        }
        $phpcsFile->fixer
            ->beginChangeset();
        $phpcsFile->fixer
            ->addContent($pointerBeforeUseParenthesisCloser, ',');
        $phpcsFile->fixer
            ->endChangeset();
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title
RequireTrailingCommaInClosureUseSniff::$enable public property @var bool|null
RequireTrailingCommaInClosureUseSniff::CODE_MISSING_TRAILING_COMMA public constant
RequireTrailingCommaInClosureUseSniff::process public function * @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint
*
Overrides Sniff::process
RequireTrailingCommaInClosureUseSniff::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