function EarlyExitSniff::processElse
1 call to EarlyExitSniff::processElse()
- EarlyExitSniff::process in vendor/
slevomat/ coding-standard/ SlevomatCodingStandard/ Sniffs/ ControlStructures/ EarlyExitSniff.php - * @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint *
File
-
vendor/
slevomat/ coding-standard/ SlevomatCodingStandard/ Sniffs/ ControlStructures/ EarlyExitSniff.php, line 79
Class
Namespace
SlevomatCodingStandard\Sniffs\ControlStructuresCode
private function processElse(File $phpcsFile, int $elsePointer) : void {
$tokens = $phpcsFile->getTokens();
if (!array_key_exists('scope_opener', $tokens[$elsePointer])) {
// Else without curly braces is not supported.
return;
}
try {
$allConditionsPointers = $this->getAllConditionsPointers($phpcsFile, $elsePointer);
} catch (Throwable $e) {
// Else without curly braces is not supported.
return;
}
if (TokenHelper::findNext($phpcsFile, T_FUNCTION, $tokens[$elsePointer]['scope_opener'] + 1, $tokens[$elsePointer]['scope_closer']) !== null) {
return;
}
$ifPointer = $allConditionsPointers[0];
$ifEarlyExitPointer = null;
$elseEarlyExitPointer = null;
$previousConditionPointer = null;
$previousConditionEarlyExitPointer = null;
foreach ($allConditionsPointers as $conditionPointer) {
$conditionEarlyExitPointer = $this->findEarlyExitInScope($phpcsFile, $tokens[$conditionPointer]['scope_opener'], $tokens[$conditionPointer]['scope_closer']);
if ($conditionPointer === $elsePointer) {
$elseEarlyExitPointer = $conditionEarlyExitPointer;
continue;
}
if (count($allConditionsPointers) > 2 && $conditionEarlyExitPointer === null) {
return;
}
$previousConditionPointer = $conditionPointer;
$previousConditionEarlyExitPointer = $conditionEarlyExitPointer;
if ($conditionPointer === $ifPointer) {
$ifEarlyExitPointer = $conditionEarlyExitPointer;
continue;
}
}
if ($ifEarlyExitPointer === null && $elseEarlyExitPointer === null) {
return;
}
if ($elseEarlyExitPointer !== null && $previousConditionEarlyExitPointer === null) {
$fix = $phpcsFile->addFixableError('Use early exit instead of "else".', $elsePointer, self::CODE_EARLY_EXIT_NOT_USED);
if (!$fix) {
return;
}
$ifCodePointers = $this->getScopeCodePointers($phpcsFile, $ifPointer);
$elseCode = $this->getScopeCode($phpcsFile, $elsePointer);
$negativeIfCondition = ConditionHelper::getNegativeCondition($phpcsFile, $tokens[$ifPointer]['parenthesis_opener'], $tokens[$ifPointer]['parenthesis_closer']);
$afterIfCode = IndentationHelper::fixIndentation($phpcsFile, $ifCodePointers, IndentationHelper::getIndentation($phpcsFile, $ifPointer));
$ifContent = sprintf('if %s {%s}%s%s', $negativeIfCondition, $elseCode, $phpcsFile->eolChar, $afterIfCode);
$phpcsFile->fixer
->beginChangeset();
FixerHelper::change($phpcsFile, $ifPointer, $tokens[$elsePointer]['scope_closer'], $ifContent);
$phpcsFile->fixer
->endChangeset();
return;
}
$fix = $phpcsFile->addFixableError('Remove useless "else" to reduce code nesting.', $elsePointer, self::CODE_USELESS_ELSE);
if (!$fix) {
return;
}
$elseCodePointers = $this->getScopeCodePointers($phpcsFile, $elsePointer);
$afterIfCode = IndentationHelper::fixIndentation($phpcsFile, $elseCodePointers, IndentationHelper::getIndentation($phpcsFile, $ifPointer));
$phpcsFile->fixer
->beginChangeset();
$previousConditionContent = sprintf('%s%s', $phpcsFile->eolChar, $afterIfCode);
FixerHelper::change($phpcsFile, $tokens[$previousConditionPointer]['scope_closer'] + 1, $tokens[$elsePointer]['scope_closer'], $previousConditionContent);
$phpcsFile->fixer
->endChangeset();
}