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

Breadcrumb

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

function DisallowMultiConstantDefinitionSniff::process

* @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint *

Parameters

int $constantPointer:

Overrides Sniff::process

File

vendor/slevomat/coding-standard/SlevomatCodingStandard/Sniffs/Classes/DisallowMultiConstantDefinitionSniff.php, line 38

Class

DisallowMultiConstantDefinitionSniff

Namespace

SlevomatCodingStandard\Sniffs\Classes

Code

public function process(File $phpcsFile, $constantPointer) : void {
    $tokens = $phpcsFile->getTokens();
    $semicolonPointer = TokenHelper::findNext($phpcsFile, T_SEMICOLON, $constantPointer + 1);
    $commaPointers = [];
    $nextPointer = $constantPointer;
    do {
        $nextPointer = TokenHelper::findNext($phpcsFile, [
            T_COMMA,
            T_OPEN_SHORT_ARRAY,
        ], $nextPointer + 1, $semicolonPointer);
        if ($nextPointer === null) {
            break;
        }
        if ($tokens[$nextPointer]['code'] === T_OPEN_SHORT_ARRAY) {
            $nextPointer = $tokens[$nextPointer]['bracket_closer'];
            continue;
        }
        $commaPointers[] = $nextPointer;
    } while (true);
    if (count($commaPointers) === 0) {
        return;
    }
    $fix = $phpcsFile->addFixableError('Use of multi constant definition is disallowed.', $constantPointer, self::CODE_DISALLOWED_MULTI_CONSTANT_DEFINITION);
    if (!$fix) {
        return;
    }
    $possibleVisibilityPointer = TokenHelper::findPreviousEffective($phpcsFile, $constantPointer - 1);
    $visibilityPointer = in_array($tokens[$possibleVisibilityPointer]['code'], Tokens::$scopeModifiers, true) ? $possibleVisibilityPointer : null;
    $visibility = $visibilityPointer !== null ? $tokens[$possibleVisibilityPointer]['content'] : null;
    $pointerAfterConst = TokenHelper::findNextEffective($phpcsFile, $constantPointer + 1);
    $pointerBeforeSemicolon = TokenHelper::findPreviousEffective($phpcsFile, $semicolonPointer - 1);
    $indentation = IndentationHelper::getIndentation($phpcsFile, $visibilityPointer ?? $constantPointer);
    $docCommentPointer = DocCommentHelper::findDocCommentOpenPointer($phpcsFile, $constantPointer);
    $docComment = $docCommentPointer !== null ? trim(TokenHelper::getContent($phpcsFile, $docCommentPointer, $tokens[$docCommentPointer]['comment_closer'])) : null;
    $data = [];
    foreach ($commaPointers as $commaPointer) {
        $data[$commaPointer] = [
            'pointerBeforeComma' => TokenHelper::findPreviousEffective($phpcsFile, $commaPointer - 1),
            'pointerAfterComma' => TokenHelper::findNextEffective($phpcsFile, $commaPointer + 1),
        ];
    }
    $phpcsFile->fixer
        ->beginChangeset();
    $phpcsFile->fixer
        ->addContent($constantPointer, ' ');
    FixerHelper::removeBetween($phpcsFile, $constantPointer, $pointerAfterConst);
    foreach ($commaPointers as $commaPointer) {
        FixerHelper::removeBetween($phpcsFile, $data[$commaPointer]['pointerBeforeComma'], $commaPointer);
        $phpcsFile->fixer
            ->replaceToken($commaPointer, sprintf(';%s%s%s%sconst ', $phpcsFile->eolChar, $docComment !== null ? sprintf('%s%s%s', $indentation, $docComment, $phpcsFile->eolChar) : '', $indentation, $visibility !== null ? sprintf('%s ', $visibility) : ''));
        FixerHelper::removeBetween($phpcsFile, $commaPointer, $data[$commaPointer]['pointerAfterComma']);
    }
    FixerHelper::removeBetween($phpcsFile, $pointerBeforeSemicolon, $semicolonPointer);
    $phpcsFile->fixer
        ->endChangeset();
}
RSS feed
Powered by Drupal