function UselessParenthesesSniff::checkParenthesesAroundConditionInTernaryOperator
1 call to UselessParenthesesSniff::checkParenthesesAroundConditionInTernaryOperator()
- UselessParenthesesSniff::process in vendor/
slevomat/ coding-standard/ SlevomatCodingStandard/ Sniffs/ PHP/ UselessParenthesesSniff.php - * @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint *
File
-
vendor/
slevomat/ coding-standard/ SlevomatCodingStandard/ Sniffs/ PHP/ UselessParenthesesSniff.php, line 209
Class
Namespace
SlevomatCodingStandard\Sniffs\PHPCode
private function checkParenthesesAroundConditionInTernaryOperator(File $phpcsFile, int $parenthesisOpenerPointer) : void {
$tokens = $phpcsFile->getTokens();
$parenthesisCloserPointer = $tokens[$parenthesisOpenerPointer]['parenthesis_closer'];
$ternaryOperatorPointer = TokenHelper::findNextEffective($phpcsFile, $parenthesisCloserPointer + 1);
if ($tokens[$ternaryOperatorPointer]['code'] !== T_INLINE_THEN) {
return;
}
if (TokenHelper::findNext($phpcsFile, [
T_LOGICAL_AND,
T_LOGICAL_OR,
T_LOGICAL_XOR,
], $parenthesisOpenerPointer + 1, $parenthesisCloserPointer) !== null) {
return;
}
$pointerBeforeParenthesisOpener = TokenHelper::findPreviousEffective($phpcsFile, $parenthesisOpenerPointer - 1);
if ($tokens[$pointerBeforeParenthesisOpener]['code'] === T_BOOLEAN_NOT) {
return;
}
if (in_array($tokens[$pointerBeforeParenthesisOpener]['code'], Tokens::$comparisonTokens, true)) {
return;
}
if (in_array($tokens[$pointerBeforeParenthesisOpener]['code'], Tokens::$booleanOperators, true)) {
return;
}
if ($this->ignoreComplexTernaryConditions) {
if (TokenHelper::findNext($phpcsFile, Tokens::$booleanOperators, $parenthesisOpenerPointer + 1, $parenthesisCloserPointer) !== null) {
return;
}
if (TokenHelper::findNextContent($phpcsFile, T_WHITESPACE, $phpcsFile->eolChar, $parenthesisOpenerPointer + 1, $parenthesisCloserPointer) !== null) {
return;
}
}
$contentStartPointer = TokenHelper::findNextEffective($phpcsFile, $parenthesisOpenerPointer + 1);
$contentEndPointer = TokenHelper::findPreviousEffective($phpcsFile, $parenthesisCloserPointer - 1);
for ($i = $contentStartPointer; $i <= $contentEndPointer; $i++) {
if ($tokens[$i]['code'] === T_INLINE_THEN) {
return;
}
}
$fix = $phpcsFile->addFixableError('Useless parentheses.', $parenthesisOpenerPointer, self::CODE_USELESS_PARENTHESES);
if (!$fix) {
return;
}
$phpcsFile->fixer
->beginChangeset();
FixerHelper::removeBetweenIncluding($phpcsFile, $parenthesisOpenerPointer, $contentStartPointer - 1);
FixerHelper::removeBetweenIncluding($phpcsFile, $contentEndPointer + 1, $parenthesisCloserPointer);
$phpcsFile->fixer
->endChangeset();
}