use of com.jetbrains.php.lang.psi.elements.TernaryExpression in project phpinspectionsea by kalessil.
the class SuspiciousTernaryOperatorInspector method buildVisitor.
@Override
@NotNull
public PsiElementVisitor buildVisitor(@NotNull final ProblemsHolder holder, boolean isOnTheFly) {
return new BasePhpElementVisitor() {
@Override
public void visitPhpTernaryExpression(@NotNull TernaryExpression expression) {
final PsiElement condition = ExpressionSemanticUtil.getExpressionTroughParenthesis(expression.getCondition());
final PsiElement trueVariant = ExpressionSemanticUtil.getExpressionTroughParenthesis(expression.getTrueVariant());
final PsiElement falseVariant = ExpressionSemanticUtil.getExpressionTroughParenthesis(expression.getFalseVariant());
/* Case 1: identical variants */
if (trueVariant != null && falseVariant != null && OpeanapiEquivalenceUtil.areEqual(trueVariant, falseVariant)) {
holder.registerProblem(expression, messageVariantsIdentical, ProblemHighlightType.GENERIC_ERROR);
}
/* Case 2: operations which might produce a value as not expected */
if (condition instanceof BinaryExpression && !(expression.getCondition() instanceof ParenthesizedExpression)) {
final IElementType operator = ((BinaryExpression) condition).getOperationType();
if (operator != null && !safeOperations.contains(operator)) {
holder.registerProblem(condition, messagePriorities, ProblemHighlightType.GENERIC_ERROR);
}
}
/* Case 3: literal operators priorities issue */
final PsiElement parent = expression.getParent();
if (parent instanceof BinaryExpression) {
final BinaryExpression binary = (BinaryExpression) parent;
if (binary.getRightOperand() == expression && PhpTokenTypes.tsLIT_OPS.contains(binary.getOperationType())) {
holder.registerProblem(binary, messagePriorities, ProblemHighlightType.GENERIC_ERROR);
}
}
}
};
}
Aggregations