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

Breadcrumb

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

function 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

Overrides Sniff::process

File

vendor/drupal/coder/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();
        }
    }
}
RSS feed
Powered by Drupal