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

Breadcrumb

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

function RequireOneNamespaceInFileSniff::process

* @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint *

Parameters

int $namespacePointer:

Overrides Sniff::process

File

vendor/slevomat/coding-standard/SlevomatCodingStandard/Sniffs/Namespaces/RequireOneNamespaceInFileSniff.php, line 30

Class

RequireOneNamespaceInFileSniff

Namespace

SlevomatCodingStandard\Sniffs\Namespaces

Code

public function process(File $phpcsFile, $namespacePointer) : void {
    $tokens = $phpcsFile->getTokens();
    $pointerAfterNamespace = TokenHelper::findNextEffective($phpcsFile, $namespacePointer + 1);
    if ($tokens[$pointerAfterNamespace]['code'] === T_NS_SEPARATOR) {
        return;
    }
    $previousNamespacePointer = $namespacePointer;
    do {
        $previousNamespacePointer = TokenHelper::findPrevious($phpcsFile, T_NAMESPACE, $previousNamespacePointer - 1);
        if ($previousNamespacePointer === null) {
            return;
        }
        $pointerAfterPreviousNamespace = TokenHelper::findNextEffective($phpcsFile, $previousNamespacePointer + 1);
        if ($tokens[$pointerAfterPreviousNamespace]['code'] === T_NS_SEPARATOR) {
            continue;
        }
        break;
    } while (true);
    $phpcsFile->addError('Only one namespace in a file is allowed.', $namespacePointer, self::CODE_MORE_NAMESPACES_IN_FILE);
}
RSS feed
Powered by Drupal