ElseIfSniff.php in Coder 8.3.x
File
coder_sniffer/Drupal/Sniffs/ControlStructures/ElseIfSniff.php
View source
<?php
namespace Drupal\Sniffs\ControlStructures;
use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\Sniff;
class ElseIfSniff implements Sniff {
public function register() {
return [
T_ELSE,
];
}
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();
}
}
}
}
Classes
Name |
Description |
ElseIfSniff |
Checks that "elseif" is used instead of "else if". |