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

Breadcrumb

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

function ClassDeclarationSniff::process

Same name in this branch
  1. 11.1.x vendor/squizlabs/php_codesniffer/src/Standards/Squiz/Sniffs/Classes/ClassDeclarationSniff.php \PHP_CodeSniffer\Standards\Squiz\Sniffs\Classes\ClassDeclarationSniff::process()
  2. 11.1.x vendor/squizlabs/php_codesniffer/src/Standards/PSR2/Sniffs/Classes/ClassDeclarationSniff.php \PHP_CodeSniffer\Standards\PSR2\Sniffs\Classes\ClassDeclarationSniff::process()
  3. 11.1.x vendor/squizlabs/php_codesniffer/src/Standards/PSR1/Sniffs/Classes/ClassDeclarationSniff.php \PHP_CodeSniffer\Standards\PSR1\Sniffs\Classes\ClassDeclarationSniff::process()
  4. 11.1.x vendor/squizlabs/php_codesniffer/src/Standards/PEAR/Sniffs/Classes/ClassDeclarationSniff.php \PHP_CodeSniffer\Standards\PEAR\Sniffs\Classes\ClassDeclarationSniff::process()

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

Parameters

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

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

Return value

void

Overrides ClassDeclarationSniff::process

File

vendor/drupal/coder/coder_sniffer/Drupal/Sniffs/Classes/ClassDeclarationSniff.php, line 62

Class

ClassDeclarationSniff
Class Declaration Test.

Namespace

Drupal\Sniffs\Classes

Code

public function process(File $phpcsFile, $stackPtr) {
    $tokens = $phpcsFile->getTokens();
    $errorData = [
        strtolower($tokens[$stackPtr]['content']),
    ];
    if (isset($tokens[$stackPtr]['scope_opener']) === false) {
        $error = 'Possible parse error: %s missing opening or closing brace';
        $phpcsFile->addWarning($error, $stackPtr, 'MissingBrace', $errorData);
        return;
    }
    $openingBrace = $tokens[$stackPtr]['scope_opener'];
    $next = $phpcsFile->findNext(T_WHITESPACE, $openingBrace + 1, null, true);
    if ($tokens[$next]['line'] === $tokens[$openingBrace]['line'] && $tokens[$next]['code'] !== T_CLOSE_CURLY_BRACKET) {
        $error = 'Opening brace must be the last content on the line';
        $fix = $phpcsFile->addFixableError($error, $openingBrace, 'ContentAfterBrace');
        if ($fix === true) {
            $phpcsFile->fixer
                ->addNewline($openingBrace);
        }
    }
    $previous = $phpcsFile->findPrevious(T_WHITESPACE, $openingBrace - 1, null, true);
    $declarationLine = $tokens[$previous]['line'];
    $braceLine = $tokens[$openingBrace]['line'];
    $lineDifference = $braceLine - $declarationLine;
    if ($lineDifference > 0) {
        $error = 'Opening brace should be on the same line as the declaration';
        $fix = $phpcsFile->addFixableError($error, $openingBrace, 'BraceOnNewLine');
        if ($fix === true) {
            $phpcsFile->fixer
                ->beginChangeset();
            for ($i = $previous + 1; $i < $openingBrace; $i++) {
                $phpcsFile->fixer
                    ->replaceToken($i, '');
            }
            $phpcsFile->fixer
                ->addContent($previous, ' ');
            $phpcsFile->fixer
                ->endChangeset();
        }
        return;
    }
    $openingBrace = $tokens[$stackPtr]['scope_opener'];
    if ($tokens[$openingBrace - 1]['code'] !== T_WHITESPACE) {
        $length = 0;
    }
    else {
        if ($tokens[$openingBrace - 1]['content'] === "\t") {
            $length = '\\t';
        }
        else {
            $length = strlen($tokens[$openingBrace - 1]['content']);
        }
    }
    if ($length !== 1) {
        $error = 'Expected 1 space before opening brace; found %s';
        $data = [
            $length,
        ];
        $fix = $phpcsFile->addFixableError($error, $openingBrace, 'SpaceBeforeBrace', $data);
        if ($fix === true) {
            if ($length === 0) {
                $phpcsFile->fixer
                    ->replaceToken($openingBrace, ' {');
            }
            else {
                $phpcsFile->fixer
                    ->replaceToken($openingBrace - 1, ' ');
            }
        }
    }
    // Now call the open spacing method from PSR2.
    $this->processOpen($phpcsFile, $stackPtr);
    $this->processClose($phpcsFile, $stackPtr);
}

API Navigation

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