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

Breadcrumb

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

function TraitUseDeclarationSniff::checkDeclaration

1 call to TraitUseDeclarationSniff::checkDeclaration()
TraitUseDeclarationSniff::process in vendor/slevomat/coding-standard/SlevomatCodingStandard/Sniffs/Classes/TraitUseDeclarationSniff.php
* @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint *

File

vendor/slevomat/coding-standard/SlevomatCodingStandard/Sniffs/Classes/TraitUseDeclarationSniff.php, line 50

Class

TraitUseDeclarationSniff

Namespace

SlevomatCodingStandard\Sniffs\Classes

Code

private function checkDeclaration(File $phpcsFile, int $usePointer) : void {
    $commaPointer = TokenHelper::findNextLocal($phpcsFile, T_COMMA, $usePointer + 1);
    if ($commaPointer === null) {
        return;
    }
    $endPointer = TokenHelper::findNext($phpcsFile, [
        T_OPEN_CURLY_BRACKET,
        T_SEMICOLON,
    ], $usePointer + 1);
    $tokens = $phpcsFile->getTokens();
    if ($tokens[$endPointer]['code'] === T_OPEN_CURLY_BRACKET) {
        $phpcsFile->addError('Multiple traits per use statement are forbidden.', $usePointer, self::CODE_MULTIPLE_TRAITS_PER_DECLARATION);
        return;
    }
    $fix = $phpcsFile->addFixableError('Multiple traits per use statement are forbidden.', $usePointer, self::CODE_MULTIPLE_TRAITS_PER_DECLARATION);
    if (!$fix) {
        return;
    }
    $indentation = '';
    $currentPointer = $usePointer - 1;
    while ($tokens[$currentPointer]['code'] === T_WHITESPACE && $tokens[$currentPointer]['content'] !== $phpcsFile->eolChar) {
        $indentation .= $tokens[$currentPointer]['content'];
        $currentPointer--;
    }
    $phpcsFile->fixer
        ->beginChangeset();
    $otherCommaPointers = TokenHelper::findNextAll($phpcsFile, T_COMMA, $usePointer + 1, $endPointer);
    foreach ($otherCommaPointers as $otherCommaPointer) {
        $pointerAfterComma = TokenHelper::findNextEffective($phpcsFile, $otherCommaPointer + 1);
        FixerHelper::change($phpcsFile, $otherCommaPointer, $pointerAfterComma - 1, ';' . $phpcsFile->eolChar . $indentation . 'use ');
    }
    $phpcsFile->fixer
        ->endChangeset();
}
RSS feed
Powered by Drupal