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/squizlabs/php_codesniffer/src/Standards/Squiz/Sniffs/Classes/ClassDeclarationSniff.php \PHP_CodeSniffer\Standards\Squiz\Sniffs\Classes\ClassDeclarationSniff::processClose()
  2. 11.1.x vendor/squizlabs/php_codesniffer/src/Standards/PSR2/Sniffs/Classes/ClassDeclarationSniff.php \PHP_CodeSniffer\Standards\PSR2\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

Overrides ClassDeclarationSniff::processClose

1 call to ClassDeclarationSniff::processClose()
ClassDeclarationSniff::process in vendor/drupal/coder/coder_sniffer/Drupal/Sniffs/Classes/ClassDeclarationSniff.php
Processes this test, when one of its tokens is encountered.

File

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

Class

ClassDeclarationSniff
Class Declaration Test.

Namespace

Drupal\Sniffs\Classes

Code

public function processClose(File $phpcsFile, $stackPtr) {
    $tokens = $phpcsFile->getTokens();
    // Just in case.
    if (isset($tokens[$stackPtr]['scope_closer']) === false) {
        return;
    }
    // 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'] - 2 && isset(Tokens::$commentTokens[$tokens[$prevContent]['code']]) === false && $tokens[$stackPtr]['code'] !== T_ENUM) {
        $error = 'The closing brace for the %s must have an empty line before it';
        $data = [
            $tokens[$stackPtr]['content'],
        ];
        $fix = $phpcsFile->addFixableError($error, $closeBrace, 'CloseBraceAfterBody', $data);
        if ($fix === true) {
            $phpcsFile->fixer
                ->beginChangeset();
            for ($i = $prevContent + 1; $i < $closeBrace; $i++) {
                $phpcsFile->fixer
                    ->replaceToken($i, '');
            }
            $phpcsFile->fixer
                ->replaceToken($closeBrace, $phpcsFile->eolChar . $phpcsFile->eolChar . $tokens[$closeBrace]['content']);
            $phpcsFile->fixer
                ->endChangeset();
        }
    }
    
    //end if
    // Check the closing brace is on it's own line, but allow
    // for comments like "//end class".
    $nextContent = $phpcsFile->findNext(T_COMMENT, $closeBrace + 1, null, true);
    if ($tokens[$nextContent]['content'] !== $phpcsFile->eolChar && $tokens[$nextContent]['line'] === $tokens[$closeBrace]['line']) {
        $type = strtolower($tokens[$stackPtr]['content']);
        $error = 'Closing %s brace must be on a line by itself';
        $data = [
            $tokens[$stackPtr]['content'],
        ];
        $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