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

Breadcrumb

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

class UseDoesNotStartWithBackslashSniff

Hierarchy

  • class \SlevomatCodingStandard\Sniffs\Namespaces\UseDoesNotStartWithBackslashSniff implements \PHP_CodeSniffer\Sniffs\Sniff

Expanded class hierarchy of UseDoesNotStartWithBackslashSniff

File

vendor/slevomat/coding-standard/SlevomatCodingStandard/Sniffs/Namespaces/UseDoesNotStartWithBackslashSniff.php, line 14

Namespace

SlevomatCodingStandard\Sniffs\Namespaces
View source
class UseDoesNotStartWithBackslashSniff implements Sniff {
    public const CODE_STARTS_WITH_BACKSLASH = 'UseStartsWithBackslash';
    
    /**
     * @return array<int, (int|string)>
     */
    public function register() : array {
        return [
            T_USE,
        ];
    }
    
    /**
     * @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint
     * @param int $usePointer
     */
    public function process(File $phpcsFile, $usePointer) : void {
        if (!UseStatementHelper::isImportUse($phpcsFile, $usePointer)) {
            return;
        }
        $tokens = $phpcsFile->getTokens();
        
        /** @var int $nextTokenPointer */
        $nextTokenPointer = TokenHelper::findNextEffective($phpcsFile, $usePointer + 1);
        if (in_array($tokens[$nextTokenPointer]['code'], TokenHelper::getOnlyNameTokenCodes(), true) && ($tokens[$nextTokenPointer]['content'] === 'function' || $tokens[$nextTokenPointer]['content'] === 'const')) {
            
            /** @var int $nextTokenPointer */
            $nextTokenPointer = TokenHelper::findNextEffective($phpcsFile, $nextTokenPointer + 1);
        }
        if (!NamespaceHelper::isFullyQualifiedPointer($phpcsFile, $nextTokenPointer)) {
            return;
        }
        $fix = $phpcsFile->addFixableError('Use statement cannot start with a backslash.', $nextTokenPointer, self::CODE_STARTS_WITH_BACKSLASH);
        if (!$fix) {
            return;
        }
        $phpcsFile->fixer
            ->beginChangeset();
        $phpcsFile->fixer
            ->replaceToken($nextTokenPointer, ltrim($tokens[$nextTokenPointer]['content'], '\\'));
        $phpcsFile->fixer
            ->endChangeset();
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title
UseDoesNotStartWithBackslashSniff::CODE_STARTS_WITH_BACKSLASH public constant
UseDoesNotStartWithBackslashSniff::process public function * @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint
*
Overrides Sniff::process
UseDoesNotStartWithBackslashSniff::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