function ConditionHelper::getNegativeConditionPart
1 call to ConditionHelper::getNegativeConditionPart()
- ConditionHelper::getNegativeCondition in vendor/
slevomat/ coding-standard/ SlevomatCodingStandard/ Helpers/ ConditionHelper.php
File
-
vendor/
slevomat/ coding-standard/ SlevomatCodingStandard/ Helpers/ ConditionHelper.php, line 136
Class
- ConditionHelper
- @internal
Namespace
SlevomatCodingStandard\HelpersCode
private static function getNegativeConditionPart(File $phpcsFile, int $conditionBoundaryStartPointer, int $conditionBoundaryEndPointer, bool $nested) : string {
$tokens = $phpcsFile->getTokens();
$condition = TokenHelper::getContent($phpcsFile, $conditionBoundaryStartPointer, $conditionBoundaryEndPointer);
if (strtolower($condition) === 'true') {
return 'false';
}
if (strtolower($condition) === 'false') {
return 'true';
}
$pointerAfterConditionStart = TokenHelper::findNextEffective($phpcsFile, $conditionBoundaryStartPointer);
$booleanPointers = TokenHelper::findNextAll($phpcsFile, Tokens::$booleanOperators, $conditionBoundaryStartPointer, $conditionBoundaryEndPointer + 1);
if ($tokens[$pointerAfterConditionStart]['code'] === T_BOOLEAN_NOT) {
$pointerAfterBooleanNot = TokenHelper::findNextEffective($phpcsFile, $pointerAfterConditionStart + 1);
if ($tokens[$pointerAfterBooleanNot]['code'] === T_OPEN_PARENTHESIS) {
if ($nested && $booleanPointers !== []) {
return self::removeBooleanNot($condition);
}
$pointerAfterParenthesisCloser = TokenHelper::findNextEffective($phpcsFile, $tokens[$pointerAfterBooleanNot]['parenthesis_closer'] + 1, $conditionBoundaryEndPointer + 1);
if ($pointerAfterParenthesisCloser === null || $pointerAfterParenthesisCloser === $conditionBoundaryEndPointer) {
return TokenHelper::getContent($phpcsFile, $pointerAfterBooleanNot + 1, $tokens[$pointerAfterBooleanNot]['parenthesis_closer'] - 1);
}
}
}
if (count($booleanPointers) > 0) {
return self::getNegativeLogicalCondition($phpcsFile, $conditionBoundaryStartPointer, $conditionBoundaryEndPointer);
}
if ($tokens[$pointerAfterConditionStart]['code'] === T_BOOLEAN_NOT) {
return self::removeBooleanNot($condition);
}
if (TokenHelper::findNext($phpcsFile, [
T_INSTANCEOF,
T_BITWISE_AND,
T_COALESCE,
T_INLINE_THEN,
], $conditionBoundaryStartPointer, $conditionBoundaryEndPointer + 1) !== null) {
return sprintf('!(%s)', $condition);
}
if ($tokens[$pointerAfterConditionStart]['code'] === T_STRING) {
$pointerAfterConditionStart = TokenHelper::findNextEffective($phpcsFile, $pointerAfterConditionStart + 1);
if ($tokens[$pointerAfterConditionStart]['code'] === T_OPEN_PARENTHESIS && $tokens[$pointerAfterConditionStart]['parenthesis_closer'] === $conditionBoundaryEndPointer) {
return sprintf('!%s', $condition);
}
}
if (in_array($tokens[$pointerAfterConditionStart]['code'], [
T_VARIABLE,
T_SELF,
T_STATIC,
T_PARENT,
], true)) {
$identificatorEndPointer = IdentificatorHelper::findEndPointer($phpcsFile, $pointerAfterConditionStart);
$pointerAfterIdentificatorEnd = TokenHelper::findNextEffective($phpcsFile, $identificatorEndPointer + 1);
if ($tokens[$pointerAfterIdentificatorEnd]['code'] === T_OPEN_PARENTHESIS && $tokens[$pointerAfterIdentificatorEnd]['parenthesis_closer'] === $conditionBoundaryEndPointer) {
return sprintf('!%s', $condition);
}
}
$comparisonPointer = TokenHelper::findNext($phpcsFile, [
T_IS_EQUAL,
T_IS_NOT_EQUAL,
T_IS_IDENTICAL,
T_IS_NOT_IDENTICAL,
T_IS_SMALLER_OR_EQUAL,
T_IS_GREATER_OR_EQUAL,
T_LESS_THAN,
T_GREATER_THAN,
], $conditionBoundaryStartPointer, $conditionBoundaryEndPointer + 1);
if ($comparisonPointer !== null) {
$comparisonReplacements = [
T_IS_EQUAL => '!=',
T_IS_NOT_EQUAL => '==',
T_IS_IDENTICAL => '!==',
T_IS_NOT_IDENTICAL => '===',
T_IS_GREATER_OR_EQUAL => '<',
T_IS_SMALLER_OR_EQUAL => '>',
T_GREATER_THAN => '<=',
T_LESS_THAN => '>=',
];
$negativeCondition = '';
for ($i = $conditionBoundaryStartPointer; $i <= $conditionBoundaryEndPointer; $i++) {
// Skip calls()
if ($tokens[$i]['code'] === T_OPEN_PARENTHESIS) {
$negativeCondition .= TokenHelper::getContent($phpcsFile, $i, $tokens[$i]['parenthesis_closer']);
$i = $tokens[$i]['parenthesis_closer'];
continue;
}
$negativeCondition .= array_key_exists($tokens[$i]['code'], $comparisonReplacements) ? $comparisonReplacements[$tokens[$i]['code']] : $tokens[$i]['content'];
}
return $negativeCondition;
}
return sprintf('!%s', $condition);
}