You are here

public function ClassDeclarationSniff::processClose in Coder 8.2

Same name and namespace in other branches
  1. 8.3 coder_sniffer/Drupal/Sniffs/Classes/ClassDeclarationSniff.php \Drupal\Sniffs\Classes\ClassDeclarationSniff::processClose()
  2. 8.3.x coder_sniffer/Drupal/Sniffs/Classes/ClassDeclarationSniff.php \Drupal\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

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

File

coder_sniffer/Drupal/Sniffs/Classes/ClassDeclarationSniff.php, line 137

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) {
    $error = 'The closing brace for the %s must have an empty line before it';
    $data = array(
      $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 = array(
      $tokens[$stackPtr]['content'],
    );
    $phpcsFile
      ->addError($error, $closeBrace, 'CloseBraceSameLine', $data);
  }
}