Search in sources :

Example 6 with UnaryExpression

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

the class PlainApiUseCheckStrategy method getPregMatchContext.

private static PsiElement getPregMatchContext(@NotNull FunctionReference reference) {
    PsiElement result = reference;
    final PsiElement parent = reference.getParent();
    if (ExpressionSemanticUtil.isUsedAsLogicalOperand(reference)) {
        if (parent instanceof UnaryExpression) {
            final UnaryExpression unary = (UnaryExpression) parent;
            if (OpenapiTypesUtil.is(unary.getOperation(), PhpTokenTypes.opNOT)) {
                result = parent;
            }
        }
    } else if (parent instanceof BinaryExpression) {
        final BinaryExpression binary = (BinaryExpression) parent;
        final IElementType operator = binary.getOperationType();
        final boolean isTargetOperator = OpenapiTypesUtil.tsCOMPARE_EQUALITY_OPS.contains(operator) || PhpTokenTypes.tsCOMPARE_ORDER_OPS.contains(operator);
        if (isTargetOperator) {
            final PsiElement second = OpenapiElementsUtil.getSecondOperand(binary, reference);
            if (OpenapiTypesUtil.isNumber(second)) {
                result = parent;
            }
        }
    }
    return result;
}
Also used : IElementType(com.intellij.psi.tree.IElementType) BinaryExpression(com.jetbrains.php.lang.psi.elements.BinaryExpression) UnaryExpression(com.jetbrains.php.lang.psi.elements.UnaryExpression) PsiElement(com.intellij.psi.PsiElement)

Example 7 with UnaryExpression

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

the class IssetConstructsCanBeMergedInspector method buildVisitor.

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

        @Override
        public void visitPhpBinaryExpression(@NotNull BinaryExpression expression) {
            final IElementType operator = expression.getOperationType();
            if (operator != null && (operator == PhpTokenTypes.opAND || operator == PhpTokenTypes.opOR)) {
                /* false-positives: part of another condition */
                final PsiElement parent = expression.getParent();
                final PsiElement context = parent instanceof ParenthesizedExpression ? parent.getParent() : parent;
                if (context instanceof BinaryExpression && ((BinaryExpression) context).getOperationType() == operator) {
                    return;
                }
                final List<PsiElement> fragments = this.extract(expression, operator);
                if (fragments.size() > 1) {
                    if (operator == PhpTokenTypes.opAND) {
                        /* handle isset && isset ... */
                        PsiElement firstHit = null;
                        int hitsCount = 0;
                        for (final PsiElement fragment : fragments) {
                            if (fragment instanceof PhpIsset) {
                                if (++hitsCount > 1 && firstHit != null) {
                                    fragments.remove(firstHit);
                                    fragments.remove(fragment);
                                    holder.registerProblem(fragment, MessagesPresentationUtil.prefixWithEa(messageIsset), new MergeConstructsFix(holder.getProject(), expression, fragments, (PhpIsset) firstHit, (PhpIsset) fragment, operator));
                                    break;
                                }
                                firstHit = firstHit == null ? fragment : firstHit;
                            }
                        }
                    } else {
                        /* handle !isset || !isset ... */
                        PsiElement firstHit = null;
                        int hitsCount = 0;
                        for (final PsiElement fragment : fragments) {
                            if (fragment instanceof UnaryExpression) {
                                final PsiElement candidate = ((UnaryExpression) fragment).getValue();
                                if (candidate instanceof PhpIsset) {
                                    if (++hitsCount > 1 && firstHit != null) {
                                        fragments.remove(firstHit.getParent());
                                        fragments.remove(fragment);
                                        holder.registerProblem(candidate, MessagesPresentationUtil.prefixWithEa(messageIvertedIsset), new MergeConstructsFix(holder.getProject(), expression, fragments, (PhpIsset) firstHit, (PhpIsset) candidate, operator));
                                        break;
                                    }
                                    firstHit = firstHit == null ? candidate : firstHit;
                                }
                            }
                        }
                    }
                }
                fragments.clear();
            }
        }

        @NotNull
        private List<PsiElement> extract(@NotNull BinaryExpression binary, @Nullable IElementType operator) {
            final List<PsiElement> result = new ArrayList<>();
            if (binary.getOperationType() == operator) {
                Stream.of(binary.getLeftOperand(), binary.getRightOperand()).filter(Objects::nonNull).map(ExpressionSemanticUtil::getExpressionTroughParenthesis).forEach(expression -> {
                    if (expression instanceof BinaryExpression) {
                        result.addAll(this.extract((BinaryExpression) expression, operator));
                    } else {
                        result.add(expression);
                    }
                });
            } else {
                result.add(binary);
            }
            return result;
        }
    };
}
Also used : ParenthesizedExpression(com.jetbrains.php.lang.psi.elements.ParenthesizedExpression) ArrayList(java.util.ArrayList) UnaryExpression(com.jetbrains.php.lang.psi.elements.UnaryExpression) NotNull(org.jetbrains.annotations.NotNull) IElementType(com.intellij.psi.tree.IElementType) BasePhpElementVisitor(com.kalessil.phpStorm.phpInspectionsEA.openApi.BasePhpElementVisitor) BinaryExpression(com.jetbrains.php.lang.psi.elements.BinaryExpression) Objects(java.util.Objects) PsiElement(com.intellij.psi.PsiElement) Nullable(org.jetbrains.annotations.Nullable) PhpIsset(com.jetbrains.php.lang.psi.elements.PhpIsset) NotNull(org.jetbrains.annotations.NotNull)

Example 8 with UnaryExpression

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

the class SuspiciousOperatorFormattingStrategy method apply.

public static void apply(@NotNull AssignmentExpression expression, @NotNull ProblemsHolder holder) {
    final PhpPsiElement value = expression.getValue();
    if (value instanceof UnaryExpression) {
        /* previous should be "...=[!+-] ..." */
        final PsiElement previous = value.getPrevSibling();
        if (null == previous || PhpTokenTypes.opASGN != previous.getNode().getElementType()) {
            return;
        }
        final PsiElement valueOperator = ((UnaryExpression) value).getOperation();
        if (null == valueOperator || !(valueOperator.getNextSibling() instanceof PsiWhiteSpace)) {
            return;
        }
        /* analyze statement */
        final IElementType valueOperation = valueOperator.getNode().getElementType();
        if (mapping.containsKey(valueOperation)) {
            holder.registerProblem(expression, MessagesPresentationUtil.prefixWithEa(messagePattern.replace("%o%", mapping.get(valueOperation))));
        }
    }
}
Also used : IElementType(com.intellij.psi.tree.IElementType) UnaryExpression(com.jetbrains.php.lang.psi.elements.UnaryExpression) PhpPsiElement(com.jetbrains.php.lang.psi.elements.PhpPsiElement) PsiElement(com.intellij.psi.PsiElement) PhpPsiElement(com.jetbrains.php.lang.psi.elements.PhpPsiElement) PsiWhiteSpace(com.intellij.psi.PsiWhiteSpace)

Example 9 with UnaryExpression

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

the class NullableArgumentComparisonStrategy method apply.

public static boolean apply(@NotNull BinaryExpression expression, @NotNull ProblemsHolder holder) {
    boolean result = false;
    final IElementType operator = expression.getOperationType();
    if (mapping.containsKey(operator)) {
        PsiElement parent = expression.getParent();
        while (parent instanceof ParenthesizedExpression) {
            parent = parent.getParent();
        }
        final PsiElement argument = expression.getLeftOperand();
        final PsiElement value = expression.getRightOperand();
        if (parent instanceof UnaryExpression && argument instanceof PhpTypedElement && value != null) {
            final UnaryExpression target = (UnaryExpression) parent;
            if (OpenapiTypesUtil.is(target.getOperation(), PhpTokenTypes.opNOT)) {
                final PhpType type = OpenapiResolveUtil.resolveType((PhpTypedElement) argument, holder.getProject());
                if (type != null && !type.hasUnknown()) {
                    final Set<String> types = new HashSet<>();
                    type.getTypes().forEach(t -> types.add(Types.getType(t)));
                    if (types.contains(Types.strNull) || types.contains(Types.strBoolean)) {
                        final String replacement = String.format("%s %s %s", argument.getText(), mapping.get(operator), value.getText());
                        holder.registerProblem(target, MessagesPresentationUtil.prefixWithEa(String.format(messagePattern, replacement)), new NullableArgumentComparisonFix(replacement));
                        result = true;
                    }
                    types.clear();
                }
            }
        }
    }
    return result;
}
Also used : IElementType(com.intellij.psi.tree.IElementType) ParenthesizedExpression(com.jetbrains.php.lang.psi.elements.ParenthesizedExpression) PhpTypedElement(com.jetbrains.php.lang.psi.elements.PhpTypedElement) UnaryExpression(com.jetbrains.php.lang.psi.elements.UnaryExpression) PsiElement(com.intellij.psi.PsiElement) PhpType(com.jetbrains.php.lang.psi.resolve.types.PhpType) HashSet(java.util.HashSet)

Example 10 with UnaryExpression

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

the class NullCoalescingOperatorCorrectnessStrategy method apply.

public static boolean apply(@NotNull BinaryExpression expression, @NotNull ProblemsHolder holder) {
    boolean result = false;
    if (expression.getOperationType() == PhpTokenTypes.opCOALESCE) {
        final PsiElement left = ExpressionSemanticUtil.getExpressionTroughParenthesis(expression.getLeftOperand());
        final PsiElement operation = left instanceof UnaryExpression ? ((UnaryExpression) left).getOperation() : null;
        if (operation != null) {
            final IElementType operator = operation.getNode().getElementType();
            if (result = (operator == PhpTokenTypes.opNOT || PhpTokenTypes.tsCAST_OPS.contains(operator))) {
                holder.registerProblem(left, String.format(MessagesPresentationUtil.prefixWithEa(messagePattern), left.getText()));
            }
        }
    }
    return result;
}
Also used : IElementType(com.intellij.psi.tree.IElementType) UnaryExpression(com.jetbrains.php.lang.psi.elements.UnaryExpression) PsiElement(com.intellij.psi.PsiElement)

Aggregations

PsiElement (com.intellij.psi.PsiElement)17 UnaryExpression (com.jetbrains.php.lang.psi.elements.UnaryExpression)17 IElementType (com.intellij.psi.tree.IElementType)10 BinaryExpression (com.jetbrains.php.lang.psi.elements.BinaryExpression)9 BasePhpElementVisitor (com.kalessil.phpStorm.phpInspectionsEA.openApi.BasePhpElementVisitor)7 NotNull (org.jetbrains.annotations.NotNull)7 FunctionReference (com.jetbrains.php.lang.psi.elements.FunctionReference)6 Nullable (org.jetbrains.annotations.Nullable)4 ParenthesizedExpression (com.jetbrains.php.lang.psi.elements.ParenthesizedExpression)3 PhpIsset (com.jetbrains.php.lang.psi.elements.PhpIsset)3 PhpTypedElement (com.jetbrains.php.lang.psi.elements.PhpTypedElement)2 PhpType (com.jetbrains.php.lang.psi.resolve.types.PhpType)2 PsiWhiteSpace (com.intellij.psi.PsiWhiteSpace)1 ArrayAccessExpression (com.jetbrains.php.lang.psi.elements.ArrayAccessExpression)1 AssignmentExpression (com.jetbrains.php.lang.psi.elements.AssignmentExpression)1 ConstantReference (com.jetbrains.php.lang.psi.elements.ConstantReference)1 If (com.jetbrains.php.lang.psi.elements.If)1 ParameterList (com.jetbrains.php.lang.psi.elements.ParameterList)1 PhpPsiElement (com.jetbrains.php.lang.psi.elements.PhpPsiElement)1 StringLiteralExpression (com.jetbrains.php.lang.psi.elements.StringLiteralExpression)1