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

Breadcrumb

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

function ClassDeclarationSniff::processClose

Same name in this branch
  1. 11.1.x vendor/drupal/coder/coder_sniffer/Drupal/Sniffs/Classes/ClassDeclarationSniff.php \Drupal\Sniffs\Classes\ClassDeclarationSniff::processClose()
  2. 11.1.x vendor/squizlabs/php_codesniffer/src/Standards/Squiz/Sniffs/Classes/ClassDeclarationSniff.php \PHP_CodeSniffer\Standards\Squiz\Sniffs\Classes\ClassDeclarationSniff::processClose()

Processes the closing section of a class declaration.

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

2 calls to ClassDeclarationSniff::processClose()
AnonClassDeclarationSniff::process in vendor/squizlabs/php_codesniffer/src/Standards/PSR12/Sniffs/Classes/AnonClassDeclarationSniff.php
Processes this test, when one of its tokens is encountered.
ClassDeclarationSniff::process in vendor/squizlabs/php_codesniffer/src/Standards/PSR2/Sniffs/Classes/ClassDeclarationSniff.php
Processes this test, when one of its tokens is encountered.
2 methods override ClassDeclarationSniff::processClose()
ClassDeclarationSniff::processClose in vendor/drupal/coder/coder_sniffer/Drupal/Sniffs/Classes/ClassDeclarationSniff.php
Processes the closing section of a class declaration.
ClassDeclarationSniff::processClose in vendor/squizlabs/php_codesniffer/src/Standards/Squiz/Sniffs/Classes/ClassDeclarationSniff.php
Processes the closing section of a class declaration.

File

vendor/squizlabs/php_codesniffer/src/Standards/PSR2/Sniffs/Classes/ClassDeclarationSniff.php, line 493

Class

ClassDeclarationSniff

Namespace

PHP_CodeSniffer\Standards\PSR2\Sniffs\Classes

Code

public function processClose(File $phpcsFile, $stackPtr) {
    $tokens = $phpcsFile->getTokens();
    // Check that the closing brace comes right after the code body.
    $closeBrace = $tokens[$stackPtr]['scope_closer'];
    $prevContent = $phpcsFile->findPrevious(T_WHITESPACE, $closeBrace - 1, null, true);
    if ($prevContent !== $tokens[$stackPtr]['scope_opener'] && $tokens[$prevContent]['line'] !== $tokens[$closeBrace]['line'] - 1) {
        $error = 'The closing brace for the %s must go on the next line after the body';
        $data = [
            $tokens[$stackPtr]['content'],
        ];
        $fix = $phpcsFile->addFixableError($error, $closeBrace, 'CloseBraceAfterBody', $data);
        if ($fix === true) {
            $phpcsFile->fixer
                ->beginChangeset();
            for ($i = $prevContent + 1; $tokens[$i]['line'] !== $tokens[$closeBrace]['line']; $i++) {
                $phpcsFile->fixer
                    ->replaceToken($i, '');
            }
            if (strpos($tokens[$prevContent]['content'], $phpcsFile->eolChar) === false) {
                $phpcsFile->fixer
                    ->addNewline($prevContent);
            }
            $phpcsFile->fixer
                ->endChangeset();
        }
    }
    
    //end if
    if ($tokens[$stackPtr]['code'] !== T_ANON_CLASS) {
        // Check the closing brace is on it's own line, but allow
        // for comments like "//end class".
        $ignoreTokens = Tokens::$phpcsCommentTokens;
        $ignoreTokens[] = T_WHITESPACE;
        $ignoreTokens[] = T_COMMENT;
        $ignoreTokens[] = T_SEMICOLON;
        $nextContent = $phpcsFile->findNext($ignoreTokens, $closeBrace + 1, null, true);
        if ($tokens[$nextContent]['line'] === $tokens[$closeBrace]['line']) {
            $type = strtolower($tokens[$stackPtr]['content']);
            $error = 'Closing %s brace must be on a line by itself';
            $data = [
                $type,
            ];
            $phpcsFile->addError($error, $closeBrace, 'CloseBraceSameLine', $data);
        }
    }
}

API Navigation

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