You are here

public function ElseIfSniff::process in Coder 8.2

Same name and namespace in other branches
  1. 8.3 coder_sniffer/Drupal/Sniffs/ControlStructures/ElseIfSniff.php \Drupal\Sniffs\ControlStructures\ElseIfSniff::process()
  2. 8.3.x coder_sniffer/Drupal/Sniffs/ControlStructures/ElseIfSniff.php \Drupal\Sniffs\ControlStructures\ElseIfSniff::process()

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

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

File

coder_sniffer/Drupal/Sniffs/ControlStructures/ElseIfSniff.php, line 47

Class

ElseIfSniff
Checks that "elseif" is used instead of "else if".

Namespace

Drupal\Sniffs\ControlStructures

Code

public function process(File $phpcsFile, $stackPtr) {
  $tokens = $phpcsFile
    ->getTokens();
  $nextNonWhiteSpace = $phpcsFile
    ->findNext(T_WHITESPACE, $stackPtr + 1, null, true, null, true);
  if ($tokens[$nextNonWhiteSpace]['code'] === T_IF) {
    $fix = $phpcsFile
      ->addFixableError('Use "elseif" in place of "else if"', $nextNonWhiteSpace, 'ElseIfDeclaration');
    if ($fix === true) {
      $phpcsFile->fixer
        ->beginChangeset();
      $phpcsFile->fixer
        ->replaceToken($stackPtr, 'elseif');
      for ($i = $stackPtr + 1; $i < $nextNonWhiteSpace; $i++) {
        if ($tokens[$i]['code'] === T_WHITESPACE) {
          $phpcsFile->fixer
            ->replaceToken($i, '');
        }
      }
      $phpcsFile->fixer
        ->replaceToken($nextNonWhiteSpace, '');
      $phpcsFile->fixer
        ->endChangeset();
    }
  }
}