Search in sources :

Example 6 with TernaryExpression

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);
                }
            }
        }
    };
}
Also used : ParenthesizedExpression(com.jetbrains.php.lang.psi.elements.ParenthesizedExpression) IElementType(com.intellij.psi.tree.IElementType) BasePhpElementVisitor(com.kalessil.phpStorm.phpInspectionsEA.openApi.BasePhpElementVisitor) TernaryExpression(com.jetbrains.php.lang.psi.elements.TernaryExpression) BinaryExpression(com.jetbrains.php.lang.psi.elements.BinaryExpression) NotNull(org.jetbrains.annotations.NotNull) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

PsiElement (com.intellij.psi.PsiElement)6 TernaryExpression (com.jetbrains.php.lang.psi.elements.TernaryExpression)6 BasePhpElementVisitor (com.kalessil.phpStorm.phpInspectionsEA.openApi.BasePhpElementVisitor)6 NotNull (org.jetbrains.annotations.NotNull)6 IElementType (com.intellij.psi.tree.IElementType)3 BinaryExpression (com.jetbrains.php.lang.psi.elements.BinaryExpression)3 FunctionReference (com.jetbrains.php.lang.psi.elements.FunctionReference)1 ParenthesizedExpression (com.jetbrains.php.lang.psi.elements.ParenthesizedExpression)1