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

Breadcrumb

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

function ReferenceUsedNamesOnlySniff::getUseStatementPlacePointer

*

Parameters

array<string, UseStatement> $useStatements:

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

File

vendor/slevomat/coding-standard/SlevomatCodingStandard/Sniffs/Namespaces/ReferenceUsedNamesOnlySniff.php, line 597

Class

ReferenceUsedNamesOnlySniff

Namespace

SlevomatCodingStandard\Sniffs\Namespaces

Code

private function getUseStatementPlacePointer(File $phpcsFile, int $openTagPointer, array $useStatements) : int {
    if (count($useStatements) !== 0) {
        $lastUseStatement = array_values($useStatements)[count($useStatements) - 1];
        
        /** @var int $useStatementPlacePointer */
        $useStatementPlacePointer = TokenHelper::findNext($phpcsFile, T_SEMICOLON, $lastUseStatement->getPointer() + 1);
        return $useStatementPlacePointer;
    }
    $namespacePointer = TokenHelper::findNext($phpcsFile, T_NAMESPACE, $openTagPointer + 1);
    if ($namespacePointer !== null) {
        
        /** @var int $useStatementPlacePointer */
        $useStatementPlacePointer = TokenHelper::findNext($phpcsFile, [
            T_SEMICOLON,
            T_OPEN_CURLY_BRACKET,
        ], $namespacePointer + 1);
        return $useStatementPlacePointer;
    }
    $tokens = $phpcsFile->getTokens();
    $useStatementPlacePointer = $openTagPointer;
    $nonWhitespacePointerAfterOpenTag = TokenHelper::findNextNonWhitespace($phpcsFile, $openTagPointer + 1);
    if (in_array($tokens[$nonWhitespacePointerAfterOpenTag]['code'], Tokens::$commentTokens, true)) {
        $commentEndPointer = CommentHelper::getCommentEndPointer($phpcsFile, $nonWhitespacePointerAfterOpenTag);
        if (StringHelper::endsWith($tokens[$commentEndPointer]['content'], $phpcsFile->eolChar)) {
            $useStatementPlacePointer = $commentEndPointer;
        }
        else {
            $newLineAfterComment = $commentEndPointer + 1;
            if (array_key_exists($newLineAfterComment, $tokens) && $tokens[$newLineAfterComment]['content'] === $phpcsFile->eolChar) {
                $pointerAfterCommentEnd = TokenHelper::findNextNonWhitespace($phpcsFile, $newLineAfterComment + 1);
                if (TokenHelper::findNextContent($phpcsFile, T_WHITESPACE, $phpcsFile->eolChar, $newLineAfterComment + 1, $pointerAfterCommentEnd) !== null) {
                    $useStatementPlacePointer = $commentEndPointer;
                }
            }
        }
    }
    $pointerAfter = TokenHelper::findNextEffective($phpcsFile, $useStatementPlacePointer + 1);
    if ($tokens[$pointerAfter]['code'] === T_DECLARE) {
        return TokenHelper::findNext($phpcsFile, T_SEMICOLON, $pointerAfter + 1);
    }
    return $useStatementPlacePointer;
}
RSS feed
Powered by Drupal