Search in sources :

Example 1 with SelfAssignmentExpression

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

the class AdditionOperationOnArraysInspection method buildVisitor.

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

        @Override
        public void visitPhpBinaryExpression(@NotNull BinaryExpression expression) {
            final PsiElement operation = expression.getOperation();
            if (OpenapiTypesUtil.is(operation, PhpTokenTypes.opPLUS)) {
                /* do not check nested operations */
                final boolean isNestedBinary = expression.getParent() instanceof BinaryExpression;
                if (!isNestedBinary) {
                    /* do not report ' ... + []' and '[] + ...' */
                    final PsiElement right = expression.getRightOperand();
                    PsiElement left = expression.getLeftOperand();
                    while (left instanceof BinaryExpression) {
                        left = ((BinaryExpression) left).getLeftOperand();
                    }
                    if (left != null && right != null) {
                        final boolean addsImplicitArray = left instanceof ArrayCreationExpression || right instanceof ArrayCreationExpression;
                        if (!addsImplicitArray) {
                            this.inspectExpression(operation, expression);
                        }
                    }
                }
            }
        }

        @Override
        public void visitPhpSelfAssignmentExpression(@NotNull SelfAssignmentExpression expression) {
            final PsiElement operation = expression.getOperation();
            if (OpenapiTypesUtil.is(operation, PhpTokenTypes.opPLUS_ASGN)) {
                /* do not report '... += []' */
                final boolean addsImplicitArray = expression.getValue() instanceof ArrayCreationExpression;
                if (!addsImplicitArray) {
                    this.inspectExpression(operation, expression);
                }
            }
        }

        /* inspection itself */
        private void inspectExpression(@NotNull PsiElement operation, @NotNull PsiElement expression) {
            if (expression instanceof PhpTypedElement) {
                final Set<String> types = new HashSet<>();
                final PhpType resolved = OpenapiResolveUtil.resolveType((PhpTypedElement) expression, holder.getProject());
                if (resolved != null) {
                    resolved.filterUnknown().getTypes().forEach(t -> types.add(Types.getType(t)));
                }
                if (types.size() == 1 && types.contains(Types.strArray)) {
                    holder.registerProblem(operation, message);
                }
                types.clear();
            }
        }
    };
}
Also used : SelfAssignmentExpression(com.jetbrains.php.lang.psi.elements.SelfAssignmentExpression) BasePhpElementVisitor(com.kalessil.phpStorm.phpInspectionsEA.openApi.BasePhpElementVisitor) BinaryExpression(com.jetbrains.php.lang.psi.elements.BinaryExpression) ArrayCreationExpression(com.jetbrains.php.lang.psi.elements.ArrayCreationExpression) PhpTypedElement(com.jetbrains.php.lang.psi.elements.PhpTypedElement) NotNull(org.jetbrains.annotations.NotNull) PsiElement(com.intellij.psi.PsiElement) PhpType(com.jetbrains.php.lang.psi.resolve.types.PhpType) HashSet(java.util.HashSet) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with SelfAssignmentExpression

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

the class SummerTimeUnsafeTimeManipulationInspector method buildVisitor.

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

        @Override
        public void visitPhpBinaryExpression(@NotNull BinaryExpression expression) {
            if (targetOperations.contains(expression.getOperationType())) {
                final PsiElement left = expression.getLeftOperand();
                final PsiElement right = expression.getRightOperand();
                if (right != null && this.isTargetMagicNumber(right) && this.isTargetContext(right)) {
                    if (!this.isTestContext(expression)) {
                        holder.registerProblem(expression, message);
                    }
                } else if (left != null && this.isTargetMagicNumber(left) && this.isTargetContext(left)) {
                    if (!this.isTestContext(expression)) {
                        holder.registerProblem(expression, message);
                    }
                }
            }
        }

        @Override
        public void visitPhpSelfAssignmentExpression(@NotNull SelfAssignmentExpression expression) {
            if (targetAssignments.contains(expression.getOperationType())) {
                final PsiElement value = expression.getValue();
                if (value != null && this.isTargetMagicNumber(value) && !this.isTestContext(expression)) {
                    holder.registerProblem(expression, message);
                }
            }
        }

        private boolean isTargetContext(@NotNull PsiElement magicNumber) {
            boolean result = magicNumber.textMatches("86400");
            if (!result) {
                PsiElement expression = magicNumber.getParent();
                while (expression instanceof ParenthesizedExpression || expression instanceof BinaryExpression) {
                    expression = expression.getParent();
                }
                result = PsiTreeUtil.findChildrenOfType(expression, PhpExpression.class).stream().filter(OpenapiTypesUtil::isNumber).anyMatch(candidate -> {
                    final String text = candidate.getText();
                    return text.equals("60") || text.endsWith("3600");
                });
            }
            return result;
        }

        private boolean isTargetMagicNumber(@NotNull PsiElement candidate) {
            boolean result = false;
            if (OpenapiTypesUtil.isNumber(candidate)) {
                result = candidate.textMatches("24") || candidate.textMatches("86400");
            }
            return result;
        }
    };
}
Also used : SelfAssignmentExpression(com.jetbrains.php.lang.psi.elements.SelfAssignmentExpression) ParenthesizedExpression(com.jetbrains.php.lang.psi.elements.ParenthesizedExpression) BasePhpInspection(com.kalessil.phpStorm.phpInspectionsEA.openApi.BasePhpInspection) IElementType(com.intellij.psi.tree.IElementType) PhpTokenTypes(com.jetbrains.php.lang.lexer.PhpTokenTypes) Set(java.util.Set) SelfAssignmentExpression(com.jetbrains.php.lang.psi.elements.SelfAssignmentExpression) HashSet(java.util.HashSet) PsiTreeUtil(com.intellij.psi.util.PsiTreeUtil) ParenthesizedExpression(com.jetbrains.php.lang.psi.elements.ParenthesizedExpression) BasePhpElementVisitor(com.kalessil.phpStorm.phpInspectionsEA.openApi.BasePhpElementVisitor) OpenapiTypesUtil(com.kalessil.phpStorm.phpInspectionsEA.utils.OpenapiTypesUtil) BinaryExpression(com.jetbrains.php.lang.psi.elements.BinaryExpression) PhpExpression(com.jetbrains.php.lang.psi.elements.PhpExpression) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull) PsiElementVisitor(com.intellij.psi.PsiElementVisitor) ProblemsHolder(com.intellij.codeInspection.ProblemsHolder) BasePhpElementVisitor(com.kalessil.phpStorm.phpInspectionsEA.openApi.BasePhpElementVisitor) BinaryExpression(com.jetbrains.php.lang.psi.elements.BinaryExpression) OpenapiTypesUtil(com.kalessil.phpStorm.phpInspectionsEA.utils.OpenapiTypesUtil) NotNull(org.jetbrains.annotations.NotNull) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

PsiElement (com.intellij.psi.PsiElement)2 BinaryExpression (com.jetbrains.php.lang.psi.elements.BinaryExpression)2 SelfAssignmentExpression (com.jetbrains.php.lang.psi.elements.SelfAssignmentExpression)2 BasePhpElementVisitor (com.kalessil.phpStorm.phpInspectionsEA.openApi.BasePhpElementVisitor)2 HashSet (java.util.HashSet)2 NotNull (org.jetbrains.annotations.NotNull)2 ProblemsHolder (com.intellij.codeInspection.ProblemsHolder)1 PsiElementVisitor (com.intellij.psi.PsiElementVisitor)1 IElementType (com.intellij.psi.tree.IElementType)1 PsiTreeUtil (com.intellij.psi.util.PsiTreeUtil)1 PhpTokenTypes (com.jetbrains.php.lang.lexer.PhpTokenTypes)1 ArrayCreationExpression (com.jetbrains.php.lang.psi.elements.ArrayCreationExpression)1 ParenthesizedExpression (com.jetbrains.php.lang.psi.elements.ParenthesizedExpression)1 PhpExpression (com.jetbrains.php.lang.psi.elements.PhpExpression)1 PhpTypedElement (com.jetbrains.php.lang.psi.elements.PhpTypedElement)1 PhpType (com.jetbrains.php.lang.psi.resolve.types.PhpType)1 BasePhpInspection (com.kalessil.phpStorm.phpInspectionsEA.openApi.BasePhpInspection)1 OpenapiTypesUtil (com.kalessil.phpStorm.phpInspectionsEA.utils.OpenapiTypesUtil)1 Set (java.util.Set)1