function ElseIfDeclarationSniff::process
Same name in this branch
- 11.1.x vendor/squizlabs/php_codesniffer/src/Standards/Squiz/Sniffs/ControlStructures/ElseIfDeclarationSniff.php \PHP_CodeSniffer\Standards\Squiz\Sniffs\ControlStructures\ElseIfDeclarationSniff::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/
squizlabs/ php_codesniffer/ src/ Standards/ PSR2/ Sniffs/ ControlStructures/ ElseIfDeclarationSniff.php, line 43
Class
Namespace
PHP_CodeSniffer\Standards\PSR2\Sniffs\ControlStructuresCode
public function process(File $phpcsFile, $stackPtr) {
$tokens = $phpcsFile->getTokens();
if ($tokens[$stackPtr]['code'] === T_ELSEIF) {
$phpcsFile->recordMetric($stackPtr, 'Use of ELSE IF or ELSEIF', 'elseif');
return;
}
$next = $phpcsFile->findNext(T_WHITESPACE, $stackPtr + 1, null, true);
if ($tokens[$next]['code'] === T_IF) {
$phpcsFile->recordMetric($stackPtr, 'Use of ELSE IF or ELSEIF', 'else if');
$error = 'Usage of ELSE IF is discouraged; use ELSEIF instead';
$fix = $phpcsFile->addFixableWarning($error, $stackPtr, 'NotAllowed');
if ($fix === true) {
$phpcsFile->fixer
->beginChangeset();
$phpcsFile->fixer
->replaceToken($stackPtr, 'elseif');
for ($i = $stackPtr + 1; $i <= $next; $i++) {
$phpcsFile->fixer
->replaceToken($i, '');
}
$phpcsFile->fixer
->endChangeset();
}
}
}