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

Breadcrumb

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

function AnonClassDeclarationSniff::process

Processes this test, when one of its tokens is encountered.

Parameters

\PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.:

int $stackPtr The position of the current token: in the stack passed in $tokens.

Return value

void

Overrides ClassDeclarationSniff::process

File

vendor/squizlabs/php_codesniffer/src/Standards/PSR12/Sniffs/Classes/AnonClassDeclarationSniff.php, line 57

Class

AnonClassDeclarationSniff

Namespace

PHP_CodeSniffer\Standards\PSR12\Sniffs\Classes

Code

public function process(File $phpcsFile, $stackPtr) {
    $tokens = $phpcsFile->getTokens();
    if (isset($tokens[$stackPtr]['scope_opener']) === false) {
        return;
    }
    $this->multiLineSniff = new MultiLineFunctionDeclarationSniff();
    $this->functionCallSniff = new FunctionCallArgumentSpacingSniff();
    $this->processOpen($phpcsFile, $stackPtr);
    $this->processClose($phpcsFile, $stackPtr);
    if (isset($tokens[$stackPtr]['parenthesis_opener']) === true) {
        $openBracket = $tokens[$stackPtr]['parenthesis_opener'];
        if ($this->multiLineSniff
            ->isMultiLineDeclaration($phpcsFile, $stackPtr, $openBracket, $tokens) === true) {
            $this->processMultiLineArgumentList($phpcsFile, $stackPtr);
        }
        else {
            $this->processSingleLineArgumentList($phpcsFile, $stackPtr);
        }
        $this->functionCallSniff
            ->checkSpacing($phpcsFile, $stackPtr, $openBracket);
    }
    $opener = $tokens[$stackPtr]['scope_opener'];
    if ($tokens[$opener]['line'] === $tokens[$stackPtr]['line']) {
        return;
    }
    $prev = $phpcsFile->findPrevious(T_WHITESPACE, $opener - 1, $stackPtr, true);
    $implements = $phpcsFile->findPrevious(T_IMPLEMENTS, $opener - 1, $stackPtr);
    if ($implements !== false && $tokens[$opener]['line'] !== $tokens[$implements]['line'] && $tokens[$opener]['line'] === $tokens[$prev]['line']) {
        // Opening brace must be on a new line as implements list wraps.
        $error = 'Opening brace must be on the line after the last implemented interface';
        $fix = $phpcsFile->addFixableError($error, $opener, 'OpenBraceSameLine');
        if ($fix === true) {
            $first = $phpcsFile->findFirstOnLine(T_WHITESPACE, $stackPtr, true);
            $indent = str_repeat(' ', $tokens[$first]['column'] - 1);
            $phpcsFile->fixer
                ->beginChangeset();
            if ($tokens[$prev + 1]['code'] === T_WHITESPACE) {
                $phpcsFile->fixer
                    ->replaceToken($prev + 1, '');
            }
            $phpcsFile->fixer
                ->addNewline($prev);
            $phpcsFile->fixer
                ->addContentBefore($opener, $indent);
            $phpcsFile->fixer
                ->endChangeset();
        }
    }
    if ($tokens[$opener]['line'] > $tokens[$prev]['line'] + 1) {
        // Opening brace is on a new line, so there must be no blank line before it.
        $error = 'Opening brace must not be preceded by a blank line';
        $fix = $phpcsFile->addFixableError($error, $opener, 'OpenBraceLine');
        if ($fix === true) {
            $phpcsFile->fixer
                ->beginChangeset();
            for ($x = $prev + 1; $x < $opener; $x++) {
                if ($tokens[$x]['line'] === $tokens[$prev]['line']) {
                    // Maintain existing newline.
                    continue;
                }
                if ($tokens[$x]['line'] === $tokens[$opener]['line']) {
                    // Maintain existing indent.
                    break;
                }
                $phpcsFile->fixer
                    ->replaceToken($x, '');
            }
            $phpcsFile->fixer
                ->endChangeset();
        }
    }
    
    //end if
}

API Navigation

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