function RequireNullSafeObjectOperatorSniff::checkTernaryOperator
1 call to RequireNullSafeObjectOperatorSniff::checkTernaryOperator()
- RequireNullSafeObjectOperatorSniff::process in vendor/
slevomat/ coding-standard/ SlevomatCodingStandard/ Sniffs/ ControlStructures/ RequireNullSafeObjectOperatorSniff.php - * @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint *
File
-
vendor/
slevomat/ coding-standard/ SlevomatCodingStandard/ Sniffs/ ControlStructures/ RequireNullSafeObjectOperatorSniff.php, line 109
Class
Namespace
SlevomatCodingStandard\Sniffs\ControlStructuresCode
private function checkTernaryOperator(File $phpcsFile, int $identicalPointer, int $conditionStartPointer, string $identificator, int $inlineThenPointer) : void {
$tokens = $phpcsFile->getTokens();
$ternaryOperatorStartPointer = TernaryOperatorHelper::getStartPointer($phpcsFile, $inlineThenPointer);
$searchStartPointer = $ternaryOperatorStartPointer;
do {
$booleanOperatorPointer = TokenHelper::findNext($phpcsFile, Tokens::$booleanOperators, $searchStartPointer, $inlineThenPointer);
if ($booleanOperatorPointer === null) {
break;
}
$identicalPointer = TokenHelper::findNext($phpcsFile, [
T_IS_IDENTICAL,
T_IS_NOT_IDENTICAL,
], $searchStartPointer, $booleanOperatorPointer);
if ($identicalPointer === null) {
return;
}
$pointerAfterIdentical = TokenHelper::findNextEffective($phpcsFile, $identicalPointer + 1);
if ($tokens[$pointerAfterIdentical]['code'] !== T_NULL) {
return;
}
$searchStartPointer = $booleanOperatorPointer + 1;
} while (true);
$pointerBeforeCondition = TokenHelper::findPreviousEffective($phpcsFile, $conditionStartPointer - 1);
if (in_array($tokens[$pointerBeforeCondition]['code'], [
T_BOOLEAN_AND,
T_BOOLEAN_OR,
], true)) {
$previousIdenticalPointer = TokenHelper::findPreviousLocal($phpcsFile, [
T_IS_IDENTICAL,
T_IS_NOT_IDENTICAL,
], $pointerBeforeCondition);
if ($previousIdenticalPointer !== null) {
[
$pointerBeforePreviousIdentical,
$pointerAfterPreviousIdentical,
] = $this->getIdenticalData($phpcsFile, $previousIdenticalPointer);
[
$previousIdentificatorStartPointer,
$previousIdentificatorEndPointer,
] = $this->getConditionData($phpcsFile, $pointerBeforePreviousIdentical, $pointerAfterPreviousIdentical);
if ($previousIdentificatorStartPointer !== null && $previousIdentificatorEndPointer !== null) {
$previousIdentificator = IdentificatorHelper::getContent($phpcsFile, $previousIdentificatorStartPointer, $previousIdentificatorEndPointer);
if (!self::areIdentificatorsCompatible($previousIdentificator, $identificator)) {
return;
}
}
}
}
$defaultInElse = $tokens[$identicalPointer]['code'] === T_IS_NOT_IDENTICAL;
$inlineElsePointer = TernaryOperatorHelper::getElsePointer($phpcsFile, $inlineThenPointer);
$inlineElseEndPointer = TernaryOperatorHelper::getEndPointer($phpcsFile, $inlineThenPointer, $inlineElsePointer);
if ($defaultInElse) {
$nextIdentificatorPointers = $this->getNextIdentificator($phpcsFile, $inlineThenPointer);
if ($nextIdentificatorPointers === null) {
return;
}
[
$nextIdentificatorStartPointer,
$nextIdentificatorEndPointer,
] = $nextIdentificatorPointers;
$nextIdentificator = IdentificatorHelper::getContent($phpcsFile, $nextIdentificatorStartPointer, $nextIdentificatorEndPointer);
if (!$this->areIdentificatorsCompatible($identificator, $nextIdentificator)) {
return;
}
if (TokenHelper::findNextEffective($phpcsFile, $nextIdentificatorEndPointer + 1) !== $inlineElsePointer) {
return;
}
$identificatorDifference = $this->getIdentificatorDifference($phpcsFile, $identificator, $nextIdentificatorStartPointer, $nextIdentificatorEndPointer);
$firstPointerInElse = TokenHelper::findNextEffective($phpcsFile, $inlineElsePointer + 1);
$defaultContent = TokenHelper::getContent($phpcsFile, $firstPointerInElse, $inlineElseEndPointer);
$conditionEndPointer = $inlineElseEndPointer;
}
else {
$nullPointer = TokenHelper::findNextEffective($phpcsFile, $inlineThenPointer + 1);
if ($tokens[$nullPointer]['code'] !== T_NULL) {
return;
}
if (TokenHelper::findNextEffective($phpcsFile, $nullPointer + 1) !== $inlineElsePointer) {
return;
}
$nextIdentificatorPointers = $this->getNextIdentificator($phpcsFile, $inlineElsePointer);
if ($nextIdentificatorPointers === null) {
return;
}
[
$nextIdentificatorStartPointer,
$nextIdentificatorEndPointer,
] = $nextIdentificatorPointers;
if ($nextIdentificatorEndPointer !== $inlineElseEndPointer) {
return;
}
$nextIdentificator = IdentificatorHelper::getContent($phpcsFile, $nextIdentificatorStartPointer, $nextIdentificatorEndPointer);
if (!$this->areIdentificatorsCompatible($identificator, $nextIdentificator)) {
return;
}
$identificatorDifference = $this->getIdentificatorDifference($phpcsFile, $identificator, $nextIdentificatorStartPointer, $nextIdentificatorEndPointer);
$defaultContent = trim(TokenHelper::getContent($phpcsFile, $inlineThenPointer + 1, $inlineElsePointer - 1));
$conditionEndPointer = $nextIdentificatorEndPointer;
}
$fix = $phpcsFile->addFixableError('Operator ?-> is required.', $identicalPointer, self::CODE_REQUIRED_NULL_SAFE_OBJECT_OPERATOR);
if (!$fix) {
return;
}
$conditionContent = sprintf('%s?%s', $identificator, $identificatorDifference);
if (strtolower($defaultContent) !== 'null') {
$conditionContent .= sprintf(' ?? %s', $defaultContent);
}
$phpcsFile->fixer
->beginChangeset();
FixerHelper::change($phpcsFile, $conditionStartPointer, $conditionEndPointer, $conditionContent);
$phpcsFile->fixer
->endChangeset();
}