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

Breadcrumb

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

function EmptyLinesAroundClassBracesSniff::processOpeningBrace

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

File

vendor/slevomat/coding-standard/SlevomatCodingStandard/Sniffs/Classes/EmptyLinesAroundClassBracesSniff.php, line 56

Class

EmptyLinesAroundClassBracesSniff

Namespace

SlevomatCodingStandard\Sniffs\Classes

Code

private function processOpeningBrace(File $phpcsFile, int $stackPointer) : void {
    $tokens = $phpcsFile->getTokens();
    $typeToken = $tokens[$stackPointer];
    $openerPointer = $typeToken['scope_opener'];
    $openerToken = $tokens[$openerPointer];
    $nextPointerAfterOpeningBrace = TokenHelper::findNextNonWhitespace($phpcsFile, $openerPointer + 1);
    $nextTokenAfterOpeningBrace = $tokens[$nextPointerAfterOpeningBrace];
    $lines = $nextTokenAfterOpeningBrace['line'] - $openerToken['line'] - 1;
    if ($lines === $this->linesCountAfterOpeningBrace) {
        return;
    }
    if ($this->linesCountAfterOpeningBrace === 1) {
        $fix = $phpcsFile->addFixableError(sprintf('There must be one empty line after %s opening brace.', $typeToken['content']), $openerPointer, $lines === 0 ? self::CODE_NO_EMPTY_LINE_AFTER_OPENING_BRACE : self::CODE_MULTIPLE_EMPTY_LINES_AFTER_OPENING_BRACE);
    }
    else {
        $fix = $phpcsFile->addFixableError(sprintf('There must be exactly %d empty lines after %s opening brace.', $this->linesCountAfterOpeningBrace, $typeToken['content']), $openerPointer, self::CODE_INCORRECT_EMPTY_LINES_AFTER_OPENING_BRACE);
    }
    if (!$fix) {
        return;
    }
    $phpcsFile->fixer
        ->beginChangeset();
    if ($lines < $this->linesCountAfterOpeningBrace) {
        for ($i = $lines; $i < $this->linesCountAfterOpeningBrace; $i++) {
            $phpcsFile->fixer
                ->addNewline($openerPointer);
        }
    }
    else {
        for ($i = $openerPointer + $this->linesCountAfterOpeningBrace + 2; $i < $nextPointerAfterOpeningBrace; $i++) {
            if ($phpcsFile->fixer
                ->getTokenContent($i) !== $phpcsFile->eolChar) {
                break;
            }
            $phpcsFile->fixer
                ->replaceToken($i, '');
        }
    }
    $phpcsFile->fixer
        ->endChangeset();
}

API Navigation

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