You are here

public function ClassDeclarationSniff::process 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::process()
  2. 8.3.x coder_sniffer/Drupal/Sniffs/Classes/ClassDeclarationSniff.php \Drupal\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

File

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

Class

ClassDeclarationSniff
Class Declaration Test.

Namespace

Drupal\Sniffs\Classes

Code

public function process(File $phpcsFile, $stackPtr) {
  $tokens = $phpcsFile
    ->getTokens();
  $errorData = array(
    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);
  $decalrationLine = $tokens[$previous]['line'];
  $braceLine = $tokens[$openingBrace]['line'];
  $lineDifference = $braceLine - $decalrationLine;
  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 = array(
      $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);
}