function UselessTernaryOperatorSniff::process
* @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint *
Parameters
int $inlineThenPointer:
Overrides Sniff::process
File
-
vendor/
slevomat/ coding-standard/ SlevomatCodingStandard/ Sniffs/ ControlStructures/ UselessTernaryOperatorSniff.php, line 39
Class
Namespace
SlevomatCodingStandard\Sniffs\ControlStructuresCode
public function process(File $phpcsFile, $inlineThenPointer) : void {
$tokens = $phpcsFile->getTokens();
$pointerAfterInlineThen = TokenHelper::findNextEffective($phpcsFile, $inlineThenPointer + 1);
if ($tokens[$pointerAfterInlineThen]['code'] === T_INLINE_ELSE) {
$inlineElsePointer = $pointerAfterInlineThen;
}
else {
if (!in_array($tokens[$pointerAfterInlineThen]['code'], [
T_TRUE,
T_FALSE,
], true)) {
return;
}
$inlineElsePointer = TokenHelper::findNextEffective($phpcsFile, $pointerAfterInlineThen + 1);
if ($tokens[$inlineElsePointer]['code'] !== T_INLINE_ELSE) {
return;
}
}
$pointerAfterInlineElse = TokenHelper::findNextEffective($phpcsFile, $inlineElsePointer + 1);
if (!in_array($tokens[$pointerAfterInlineElse]['code'], [
T_TRUE,
T_FALSE,
], true)) {
return;
}
$conditionStartPointer = TernaryOperatorHelper::getStartPointer($phpcsFile, $inlineThenPointer);
/** @var int $conditionEndPointer */
$conditionEndPointer = TokenHelper::findPreviousEffective($phpcsFile, $inlineThenPointer - 1);
$errorParameters = [
'Useless ternary operator.',
$inlineThenPointer,
self::CODE_USELESS_TERNARY_OPERATOR,
];
if (!$this->assumeAllConditionExpressionsAreAlreadyBoolean && !ConditionHelper::conditionReturnsBoolean($phpcsFile, $conditionStartPointer, $conditionEndPointer)) {
if ($tokens[$pointerAfterInlineThen]['code'] !== T_INLINE_ELSE) {
$phpcsFile->addError(...$errorParameters);
}
return;
}
$fix = $phpcsFile->addFixableError(...$errorParameters);
if (!$fix) {
return;
}
$inlineElseEndPointer = TernaryOperatorHelper::getEndPointer($phpcsFile, $inlineThenPointer, $inlineElsePointer);
$pointerAfterTernaryOperator = TokenHelper::findNextEffective($phpcsFile, $inlineElseEndPointer + 1);
$phpcsFile->fixer
->beginChangeset();
if ($tokens[$pointerAfterInlineThen]['code'] === T_FALSE) {
$negativeCondition = ConditionHelper::getNegativeCondition($phpcsFile, $conditionStartPointer, $conditionEndPointer);
FixerHelper::change($phpcsFile, $conditionStartPointer, $conditionEndPointer, $negativeCondition);
}
FixerHelper::removeBetween($phpcsFile, $conditionEndPointer, $pointerAfterTernaryOperator);
$phpcsFile->fixer
->endChangeset();
}