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

Breadcrumb

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

function AbstractControlStructureSpacing::checkLinesAfter

3 calls to AbstractControlStructureSpacing::checkLinesAfter()
AbstractControlStructureSpacing::process in vendor/slevomat/coding-standard/SlevomatCodingStandard/Sniffs/ControlStructures/AbstractControlStructureSpacing.php
* @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint *
JumpStatementsSpacingSniff::checkLinesAfter in vendor/slevomat/coding-standard/SlevomatCodingStandard/Sniffs/ControlStructures/JumpStatementsSpacingSniff.php
JumpStatementsSpacingSniff::checkLinesAfter in vendor/slevomat/coding-standard/SlevomatCodingStandard/Sniffs/ControlStructures/JumpStatementsSpacingSniff.php
1 method overrides AbstractControlStructureSpacing::checkLinesAfter()
JumpStatementsSpacingSniff::checkLinesAfter in vendor/slevomat/coding-standard/SlevomatCodingStandard/Sniffs/ControlStructures/JumpStatementsSpacingSniff.php

File

vendor/slevomat/coding-standard/SlevomatCodingStandard/Sniffs/ControlStructures/AbstractControlStructureSpacing.php, line 232

Class

AbstractControlStructureSpacing
@internal

Namespace

SlevomatCodingStandard\Sniffs\ControlStructures

Code

protected function checkLinesAfter(File $phpcsFile, int $controlStructurePointer) : void {
    $tokens = $phpcsFile->getTokens();
    if (in_array($tokens[$controlStructurePointer]['code'], [
        T_CASE,
        T_DEFAULT,
    ], true)) {
        $colonPointer = TokenHelper::findNext($phpcsFile, T_COLON, $controlStructurePointer + 1);
        $pointerAfterColon = TokenHelper::findNextEffective($phpcsFile, $colonPointer + 1);
        if (in_array($tokens[$pointerAfterColon]['code'], [
            T_CASE,
            T_DEFAULT,
        ], true)) {
            return;
        }
    }
    $controlStructureEndPointer = $this->findControlStructureEnd($phpcsFile, $controlStructurePointer);
    $pointerAfterControlStructureEnd = TokenHelper::findNextEffective($phpcsFile, $controlStructureEndPointer + 1);
    if ($pointerAfterControlStructureEnd !== null && $tokens[$pointerAfterControlStructureEnd]['code'] === T_SEMICOLON) {
        $controlStructureEndPointer = $pointerAfterControlStructureEnd;
    }
    $notWhitespacePointerAfter = TokenHelper::findNextNonWhitespace($phpcsFile, $controlStructureEndPointer + 1);
    if ($notWhitespacePointerAfter === null) {
        return;
    }
    $hasCommentAfter = in_array($tokens[$notWhitespacePointerAfter]['code'], Tokens::$commentTokens, true);
    $isCommentAfterOnSameLine = false;
    $pointerAfter = $notWhitespacePointerAfter;
    $isControlStructureEndAfterPointer = static function (int $pointer) use ($tokens, $controlStructurePointer) : bool {
        return in_array($tokens[$controlStructurePointer]['code'], [
            T_CASE,
            T_DEFAULT,
        ], true) ? $tokens[$pointer]['code'] === T_CLOSE_CURLY_BRACKET : in_array($tokens[$pointer]['code'], [
            T_CLOSE_CURLY_BRACKET,
            T_CASE,
            T_DEFAULT,
        ], true);
    };
    if ($hasCommentAfter) {
        if ($tokens[$notWhitespacePointerAfter]['line'] === $tokens[$controlStructureEndPointer]['line'] + 1) {
            $commentEndPointer = CommentHelper::getCommentEndPointer($phpcsFile, $notWhitespacePointerAfter);
            $pointerAfterComment = TokenHelper::findNextNonWhitespace($phpcsFile, $commentEndPointer + 1);
            if ($isControlStructureEndAfterPointer($pointerAfterComment)) {
                $controlStructureEndPointer = $commentEndPointer;
                $pointerAfter = $pointerAfterComment;
            }
        }
        elseif ($tokens[$notWhitespacePointerAfter]['line'] === $tokens[$controlStructureEndPointer]['line']) {
            $isCommentAfterOnSameLine = true;
            $pointerAfter = TokenHelper::findNextNonWhitespace($phpcsFile, $notWhitespacePointerAfter + 1);
        }
    }
    $isLastControlStructure = $isControlStructureEndAfterPointer($pointerAfter);
    $requiredLinesCountAfter = $isLastControlStructure ? $this->getLinesCountAfterLast($phpcsFile, $controlStructurePointer, $controlStructureEndPointer) : $this->getLinesCountAfter();
    $actualLinesCountAfter = $tokens[$pointerAfter]['line'] - $tokens[$controlStructureEndPointer]['line'] - 1;
    if ($requiredLinesCountAfter === $actualLinesCountAfter) {
        return;
    }
    $fix = $phpcsFile->addFixableError(sprintf('Expected %d line%s after "%s", found %d.', $requiredLinesCountAfter, $requiredLinesCountAfter === 1 ? '' : 's', $tokens[$controlStructurePointer]['content'], $actualLinesCountAfter), $controlStructurePointer, $isLastControlStructure ? self::CODE_INCORRECT_LINES_COUNT_AFTER_LAST_CONTROL_STRUCTURE : self::CODE_INCORRECT_LINES_COUNT_AFTER_CONTROL_STRUCTURE);
    if (!$fix) {
        return;
    }
    $replaceStartPointer = $isCommentAfterOnSameLine ? $notWhitespacePointerAfter : $controlStructureEndPointer;
    $endOfLineBeforeAfterPointer = TokenHelper::findLastTokenOnPreviousLine($phpcsFile, $pointerAfter);
    $phpcsFile->fixer
        ->beginChangeset();
    FixerHelper::removeBetweenIncluding($phpcsFile, $replaceStartPointer + 1, $endOfLineBeforeAfterPointer);
    if ($isCommentAfterOnSameLine) {
        for ($i = 0; $i < $requiredLinesCountAfter; $i++) {
            $phpcsFile->fixer
                ->addNewline($notWhitespacePointerAfter);
        }
    }
    else {
        $linesToAdd = substr($tokens[$controlStructureEndPointer]['content'], -strlen($phpcsFile->eolChar)) === $phpcsFile->eolChar ? $requiredLinesCountAfter - 1 : $requiredLinesCountAfter;
        for ($i = 0; $i <= $linesToAdd; $i++) {
            $phpcsFile->fixer
                ->addNewline($controlStructureEndPointer);
        }
    }
    $phpcsFile->fixer
        ->endChangeset();
}

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal