Search in sources :

Example 1 with TernaryExpression

use of com.jetbrains.php.lang.psi.elements.TernaryExpression in project phpinspectionsea by kalessil.

the class GetDebugTypeCanBeUsedInspector method buildVisitor.

@Override
@NotNull
public PsiElementVisitor buildVisitor(@NotNull final ProblemsHolder holder, final boolean isOnTheFly) {
    return new BasePhpElementVisitor() {

        @Override
        public void visitPhpTernaryExpression(@NotNull TernaryExpression ternary) {
            if (!ternary.isShort()) {
                final PsiElement condition = ternary.getCondition();
                if (OpenapiTypesUtil.isFunctionReference(condition)) {
                    final FunctionReference reference = (FunctionReference) condition;
                    final String functionName = reference.getName();
                    if (functionName != null && functionName.equals("is_object")) {
                        final boolean isTargetVersion = PhpLanguageLevel.get(holder.getProject()).atLeast(PhpLanguageLevel.PHP800);
                        if (isTargetVersion) {
                            final PsiElement[] arguments = reference.getParameters();
                            if (arguments.length == 1 && arguments[0] != null) {
                                final PsiElement positive = ternary.getTrueVariant();
                                final PsiElement negative = ternary.getFalseVariant();
                                if (positive != null && negative != null) {
                                    final boolean isTarget = this.is(positive, "get_class", arguments[0]) && this.is(negative, "gettype", arguments[0]);
                                    if (isTarget && this.isFromRootNamespace(reference)) {
                                        final String replacement = String.format("%sget_debug_type(%s)", reference.getImmediateNamespaceName(), arguments[0].getText());
                                        holder.registerProblem(ternary, String.format(MessagesPresentationUtil.prefixWithEa(message), replacement), new UseGetDebugTypeFix(replacement));
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }

        private boolean is(@NotNull PsiElement candidate, @NotNull String targetName, @NotNull PsiElement targetArgument) {
            if (OpenapiTypesUtil.isFunctionReference(candidate)) {
                final FunctionReference reference = (FunctionReference) candidate;
                final String functionName = reference.getName();
                if (functionName != null && functionName.equals(targetName)) {
                    final PsiElement[] arguments = reference.getParameters();
                    if (arguments.length == 1 && arguments[0] != null) {
                        return OpenapiEquivalenceUtil.areEqual(arguments[0], targetArgument) && this.isFromRootNamespace(reference);
                    }
                }
            }
            return false;
        }
    };
}
Also used : BasePhpElementVisitor(com.kalessil.phpStorm.phpInspectionsEA.openApi.BasePhpElementVisitor) TernaryExpression(com.jetbrains.php.lang.psi.elements.TernaryExpression) FunctionReference(com.jetbrains.php.lang.psi.elements.FunctionReference) NotNull(org.jetbrains.annotations.NotNull) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with TernaryExpression

use of com.jetbrains.php.lang.psi.elements.TernaryExpression in project phpinspectionsea by kalessil.

the class ElvisOperatorCanBeUsedInspector method buildVisitor.

@Override
@NotNull
public PsiElementVisitor buildVisitor(@NotNull final ProblemsHolder holder, boolean isOnTheFly) {
    return new BasePhpElementVisitor() {

        @Override
        public void visitPhpTernaryExpression(@NotNull TernaryExpression ternary) {
            if (!ternary.isShort()) {
                final PsiElement condition = ExpressionSemanticUtil.getExpressionTroughParenthesis(ternary.getCondition());
                final PsiElement trueVariant = ExpressionSemanticUtil.getExpressionTroughParenthesis(ternary.getTrueVariant());
                if (condition != null && trueVariant != null) {
                    final PsiElement falseVariant = ExpressionSemanticUtil.getExpressionTroughParenthesis(ternary.getFalseVariant());
                    if (falseVariant != null && OpenapiEquivalenceUtil.areEqual(condition, trueVariant)) {
                        final String replacement = String.format("%s ?: %s", ternary.getCondition().getText(), ternary.getFalseVariant().getText());
                        holder.registerProblem(ternary, MessagesPresentationUtil.prefixWithEa(String.format(messagePattern, replacement)), new UseElvisOperatorFix(replacement));
                    }
                }
            }
        }
    };
}
Also used : BasePhpElementVisitor(com.kalessil.phpStorm.phpInspectionsEA.openApi.BasePhpElementVisitor) TernaryExpression(com.jetbrains.php.lang.psi.elements.TernaryExpression) NotNull(org.jetbrains.annotations.NotNull) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Example 3 with TernaryExpression

use of com.jetbrains.php.lang.psi.elements.TernaryExpression in project phpinspectionsea by kalessil.

the class SenselessTernaryOperatorInspector 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());
            if (condition instanceof BinaryExpression) {
                final BinaryExpression binary = (BinaryExpression) condition;
                final IElementType operationType = binary.getOperationType();
                if (operationType == PhpTokenTypes.opIDENTICAL || operationType == PhpTokenTypes.opNOT_IDENTICAL) {
                    final boolean isInverted = operationType == PhpTokenTypes.opNOT_IDENTICAL;
                    final PsiElement trueVariant = isInverted ? expression.getFalseVariant() : expression.getTrueVariant();
                    final PsiElement falseVariant = isInverted ? expression.getTrueVariant() : expression.getFalseVariant();
                    if (trueVariant != null && falseVariant != null) {
                        final PsiElement value = binary.getRightOperand();
                        final PsiElement subject = binary.getLeftOperand();
                        if (value != null && subject != null) {
                            final boolean isLeftPartReturned = OpenapiEquivalenceUtil.areEqual(value, trueVariant) || OpenapiEquivalenceUtil.areEqual(value, falseVariant);
                            if (isLeftPartReturned) {
                                final boolean isRightPartReturned = OpenapiEquivalenceUtil.areEqual(subject, falseVariant) || OpenapiEquivalenceUtil.areEqual(subject, trueVariant);
                                if (isRightPartReturned) {
                                    final String replacement = falseVariant.getText();
                                    holder.registerProblem(expression, String.format(MessagesPresentationUtil.prefixWithEa(patternUseOperands), replacement), new SimplifyFix(replacement));
                                }
                            }
                        }
                    }
                }
            }
        }
    };
}
Also used : 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)

Example 4 with TernaryExpression

use of com.jetbrains.php.lang.psi.elements.TernaryExpression in project phpinspectionsea by kalessil.

the class NestedTernaryOperatorInspector 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());
            if (condition instanceof TernaryExpression) {
                holder.registerProblem(condition, MessagesPresentationUtil.prefixWithEa(messageNested));
            }
            final PsiElement trueVariant = ExpressionSemanticUtil.getExpressionTroughParenthesis(expression.getTrueVariant());
            if (trueVariant instanceof TernaryExpression) {
                holder.registerProblem(trueVariant, MessagesPresentationUtil.prefixWithEa(messageNested));
            }
            final PsiElement falseVariant = expression.getFalseVariant();
            final PsiElement unboxedFalseVariant = ExpressionSemanticUtil.getExpressionTroughParenthesis(falseVariant);
            if (unboxedFalseVariant instanceof TernaryExpression) {
                final boolean allow = falseVariant instanceof TernaryExpression && expression.isShort() && ((TernaryExpression) falseVariant).isShort();
                if (!allow) {
                    holder.registerProblem(unboxedFalseVariant, MessagesPresentationUtil.prefixWithEa(messageNested));
                }
            }
        }
    };
}
Also used : BasePhpElementVisitor(com.kalessil.phpStorm.phpInspectionsEA.openApi.BasePhpElementVisitor) TernaryExpression(com.jetbrains.php.lang.psi.elements.TernaryExpression) NotNull(org.jetbrains.annotations.NotNull) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Example 5 with TernaryExpression

use of com.jetbrains.php.lang.psi.elements.TernaryExpression in project phpinspectionsea by kalessil.

the class TernaryOperatorSimplifyInspector 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 rawCondition = expression.getCondition();
            if (rawCondition != null) {
                final PsiElement condition = ExpressionSemanticUtil.getExpressionTroughParenthesis(rawCondition);
                if (condition instanceof BinaryExpression) {
                    final PsiElement trueVariant = ExpressionSemanticUtil.getExpressionTroughParenthesis(expression.getTrueVariant());
                    if (trueVariant != null) {
                        final PsiElement falseVariant = ExpressionSemanticUtil.getExpressionTroughParenthesis(expression.getFalseVariant());
                        if (falseVariant != null) {
                            final boolean isTarget = PhpLanguageUtil.isBoolean(trueVariant) && PhpLanguageUtil.isBoolean(falseVariant);
                            if (isTarget) {
                                final String replacement = this.generateReplacement((BinaryExpression) condition, trueVariant);
                                if (replacement != null) {
                                    holder.registerProblem(expression, MessagesPresentationUtil.prefixWithEa(String.format(messagePattern, replacement)), new SimplifyFix(replacement));
                                }
                            }
                        }
                    }
                }
            }
        }

        @Nullable
        private String generateReplacement(@NotNull BinaryExpression binary, @NotNull PsiElement trueVariant) {
            final IElementType operator = binary.getOperationType();
            if (null == operator) {
                return null;
            }
            final String replacement;
            final boolean useParentheses = !oppositeOperators.containsKey(operator);
            final boolean isInverted = PhpLanguageUtil.isFalse(trueVariant);
            if (useParentheses) {
                final boolean isLogical = PhpTokenTypes.opAND == operator || PhpTokenTypes.opOR == operator;
                final String boolCasting = isLogical ? "" : "(bool)";
                replacement = ((isInverted ? "!" : boolCasting) + "(%e%)").replace("%e%", binary.getText());
            } else {
                if (isInverted) {
                    final PsiElement left = binary.getLeftOperand();
                    final PsiElement right = binary.getRightOperand();
                    if (null == left || null == right) {
                        return null;
                    }
                    replacement = "%l% %o% %r%".replace("%r%", right.getText()).replace("%o%", oppositeOperators.get(operator)).replace("%l%", left.getText());
                } else {
                    replacement = binary.getText();
                }
            }
            return replacement;
        }
    };
}
Also used : 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