Search in sources :

Example 16 with PhpIndex

use of com.jetbrains.php.PhpIndex in project phpinspectionsea by kalessil.

the class InstanceofCanBeUsedInspector method buildVisitor.

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

        @Override
        public void visitPhpFunctionCall(@NotNull FunctionReference reference) {
            final String functionName = reference.getName();
            if (functionName != null) {
                switch(functionName) {
                    case "get_class":
                    case "get_parent_class":
                        {
                            final PsiElement[] arguments = reference.getParameters();
                            if (arguments.length == 1 && this.isTargetBinaryContext(reference) && this.isNotString(arguments[0])) {
                                final BinaryExpression binary = (BinaryExpression) reference.getParent();
                                final PsiElement candidate = OpenapiElementsUtil.getSecondOperand(binary, reference);
                                if (candidate != null) {
                                    final String fqn = this.extractClassFqn(candidate);
                                    if (fqn != null) {
                                        this.analyze(binary, arguments[0], fqn, !functionName.equals("get_class"));
                                    }
                                }
                            }
                            break;
                        }
                    case "is_a":
                    case "is_subclass_of":
                        {
                            final PsiElement[] arguments = reference.getParameters();
                            final boolean isTarget = arguments.length == 2 || (arguments.length == 3 && PhpLanguageUtil.isFalse(arguments[2]));
                            if (isTarget && this.isNotString(arguments[0])) {
                                final String fqn = this.extractClassFqn(arguments[1]);
                                if (fqn != null) {
                                    this.analyze(reference, arguments[0], fqn, true);
                                }
                            }
                            break;
                        }
                    case "in_array":
                        {
                            final PsiElement[] arguments = reference.getParameters();
                            if (arguments.length >= 2 && OpenapiTypesUtil.isFunctionReference(arguments[1])) {
                                final FunctionReference innerCall = (FunctionReference) arguments[1];
                                final String innerName = innerCall.getName();
                                if (innerName != null && (innerName.equals("class_implements") || innerName.equals("class_parents"))) {
                                    final PsiElement[] innerArguments = innerCall.getParameters();
                                    if (innerArguments.length > 0 && this.isNotString(innerArguments[0])) {
                                        final String fqn = this.extractClassFqn(arguments[0]);
                                        if (fqn != null) {
                                            this.analyze(reference, innerArguments[0], fqn, true);
                                        }
                                    }
                                }
                            }
                            break;
                        }
                }
            }
        }

        private void analyze(@NotNull PsiElement context, @NotNull PsiElement subject, @NotNull String fqn, boolean allowChildClasses) {
            final PhpIndex index = PhpIndex.getInstance(holder.getProject());
            final Collection<PhpClass> classes = OpenapiResolveUtil.resolveClassesByFQN(fqn, index);
            if (!classes.isEmpty() && (allowChildClasses || index.getDirectSubclasses(fqn).isEmpty())) {
                boolean isInverted = false;
                /* the calls can be inverted, less work for us */
                if (context instanceof BinaryExpression) {
                    final IElementType operator = ((BinaryExpression) context).getOperationType();
                    isInverted = operator == PhpTokenTypes.opNOT_IDENTICAL || operator == PhpTokenTypes.opNOT_EQUAL;
                }
                final String replacement = String.format(isInverted ? "! %s instanceof %s" : "%s instanceof %s", subject.getText(), fqn);
                holder.registerProblem(context, String.format(MessagesPresentationUtil.prefixWithEa(messagePattern), replacement), new UseInstanceofFix(replacement));
            }
        }

        @Nullable
        private String extractClassFqn(@NotNull PsiElement candidate) {
            if (candidate instanceof StringLiteralExpression) {
                final StringLiteralExpression string = (StringLiteralExpression) candidate;
                final String clazz = string.getContents();
                if (clazz.length() > 3 && !clazz.equals("__PHP_Incomplete_Class") && string.getFirstPsiChild() == null) {
                    return '\\' + clazz.replaceAll("\\\\\\\\", "\\\\");
                }
            }
            return null;
        }

        private boolean isTargetBinaryContext(@NotNull FunctionReference reference) {
            final PsiElement parent = reference.getParent();
            if (parent instanceof BinaryExpression) {
                return OpenapiTypesUtil.tsCOMPARE_EQUALITY_OPS.contains(((BinaryExpression) parent).getOperationType());
            }
            return false;
        }

        private boolean isNotString(@NotNull PsiElement subject) {
            if (subject instanceof PhpTypedElement && !(subject instanceof StringLiteralExpression)) {
                final PhpType resolved = OpenapiResolveUtil.resolveType((PhpTypedElement) subject, holder.getProject());
                if (resolved != null && !resolved.hasUnknown()) {
                    return resolved.getTypes().stream().noneMatch(type -> Types.getType(type).equals(Types.strString));
                }
            }
            return false;
        }
    };
}
Also used : PhpIndex(com.jetbrains.php.PhpIndex) NotNull(org.jetbrains.annotations.NotNull) PhpType(com.jetbrains.php.lang.psi.resolve.types.PhpType) IElementType(com.intellij.psi.tree.IElementType) BasePhpElementVisitor(com.kalessil.phpStorm.phpInspectionsEA.openApi.BasePhpElementVisitor) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Example 17 with PhpIndex

use of com.jetbrains.php.PhpIndex in project phpinspectionsea by kalessil.

the class ClassMockingCorrectnessInspector method buildVisitor.

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

        @Override
        public void visitPhpClass(@NotNull PhpClass clazz) {
            final PhpClass parent = OpenapiResolveUtil.resolveSuperClass(clazz);
            if (parent != null && parent.getFQN().equals("\\PhpSpec\\ObjectBehavior")) {
                for (final Method method : clazz.getOwnMethods()) {
                    for (final Parameter parameter : method.getParameters()) {
                        /* Since PS 2020.2 union types are introduced (BC-incompatible PSI-changes). Hence traversing node. */
                        final Collection<ClassReference> references = PsiTreeUtil.findChildrenOfType(parameter, ClassReference.class);
                        if (!references.isEmpty()) {
                            for (final ClassReference reference : references) {
                                final PsiElement resolved = OpenapiResolveUtil.resolveReference(reference);
                                if (resolved instanceof PhpClass && ((PhpClass) resolved).isFinal()) {
                                    holder.registerProblem(reference, MessagesPresentationUtil.prefixWithEa(messageFinal));
                                    break;
                                }
                            }
                            references.clear();
                        }
                    }
                }
            }
        }

        @Override
        public void visitPhpMethodReference(@NotNull MethodReference reference) {
            final String methodName = reference.getName();
            final PsiElement[] arguments = reference.getParameters();
            if (methodName != null && arguments.length > 0 && methods.containsValue(methodName)) {
                final PsiElement resolved = OpenapiResolveUtil.resolveReference(reference);
                if (resolved instanceof Method && methods.get(((Method) resolved).getFQN()) != null) {
                    final PhpClass referencedClass = this.getClass(arguments[0]);
                    if (referencedClass != null) {
                        if (methodName.equals("createMock")) {
                            if (referencedClass.isTrait()) {
                                holder.registerProblem(arguments[0], MessagesPresentationUtil.prefixWithEa(messageTrait));
                            } else if (referencedClass.isFinal()) {
                                holder.registerProblem(arguments[0], MessagesPresentationUtil.prefixWithEa(messageFinal));
                            }
                        } else if (methodName.equals("getMockBuilder")) {
                            final PsiElement parent = reference.getParent();
                            String parentName = null;
                            if (parent instanceof MethodReference) {
                                parentName = ((MethodReference) parent).getName();
                            }
                            /* classes might need different mocking methods usage */
                            if (referencedClass.isAbstract() && !referencedClass.isInterface()) {
                                if (parentName == null) {
                                    holder.registerProblem(arguments[0], MessagesPresentationUtil.prefixWithEa(messageMockAbstract));
                                }
                            } else if (referencedClass.isTrait()) {
                                if (parentName == null) {
                                    holder.registerProblem(arguments[0], MessagesPresentationUtil.prefixWithEa(messageMockTrait));
                                }
                            } else if (referencedClass.isFinal()) {
                                holder.registerProblem(arguments[0], MessagesPresentationUtil.prefixWithEa(messageFinal));
                            }
                            /* constructor might require arguments */
                            if (parentName != null && parentName.equals("getMock")) {
                                final Method constructor = referencedClass.getConstructor();
                                if (constructor != null) {
                                    final boolean needsArguments = Arrays.stream(constructor.getParameters()).anyMatch(parameter -> parameter.getDefaultValue() == null);
                                    if (needsArguments) {
                                        holder.registerProblem(arguments[0], MessagesPresentationUtil.prefixWithEa(messageMockConstructor));
                                    }
                                }
                            }
                        } else if (methodName.equals("getMockForTrait")) {
                            if (!referencedClass.isTrait()) {
                                holder.registerProblem(arguments[0], MessagesPresentationUtil.prefixWithEa(messageNeedsTrait));
                            }
                        } else if (methodName.equals("getMockForAbstractClass")) {
                            if (!referencedClass.isAbstract()) {
                                holder.registerProblem(arguments[0], MessagesPresentationUtil.prefixWithEa(messageNeedsAbstract));
                            }
                        } else {
                            if (referencedClass.isFinal()) {
                                holder.registerProblem(arguments[0], MessagesPresentationUtil.prefixWithEa(messageFinal));
                            }
                        }
                    }
                }
            }
        }

        @Nullable
        private PhpClass getClass(@NotNull PsiElement expression) {
            PhpClass result = null;
            if (expression instanceof ClassConstantReference) {
                final ClassConstantReference reference = (ClassConstantReference) expression;
                final String constantName = reference.getName();
                if (constantName != null && constantName.equals("class")) {
                    final PhpExpression classReference = reference.getClassReference();
                    if (classReference instanceof ClassReference) {
                        final PsiElement resolved = OpenapiResolveUtil.resolveReference((ClassReference) classReference);
                        result = resolved instanceof PhpClass ? (PhpClass) resolved : null;
                    }
                }
            } else if (expression instanceof StringLiteralExpression) {
                final StringLiteralExpression string = (StringLiteralExpression) expression;
                final String contents = string.getContents();
                if (string.getFirstPsiChild() == null && contents.length() > 3) {
                    String fqn = contents.replaceAll("\\\\\\\\", "\\\\");
                    fqn = fqn.charAt(0) == '\\' ? fqn : '\\' + fqn;
                    final PhpIndex index = PhpIndex.getInstance(holder.getProject());
                    for (final PhpClass clazz : OpenapiResolveUtil.resolveClassesByFQN(fqn, index)) {
                        if (clazz.isFinal()) {
                            result = clazz;
                            break;
                        }
                    }
                }
            }
            /* TODO: handle __NAMESPACE__.'\Class' */
            return result;
        }
    };
}
Also used : Arrays(java.util.Arrays) BasePhpInspection(com.kalessil.phpStorm.phpInspectionsEA.openApi.BasePhpInspection) com.jetbrains.php.lang.psi.elements(com.jetbrains.php.lang.psi.elements) Collection(java.util.Collection) HashMap(java.util.HashMap) PhpIndex(com.jetbrains.php.PhpIndex) Nullable(org.jetbrains.annotations.Nullable) PsiTreeUtil(com.intellij.psi.util.PsiTreeUtil) BasePhpElementVisitor(com.kalessil.phpStorm.phpInspectionsEA.openApi.BasePhpElementVisitor) OpenapiResolveUtil(com.kalessil.phpStorm.phpInspectionsEA.utils.OpenapiResolveUtil) MessagesPresentationUtil(com.kalessil.phpStorm.phpInspectionsEA.utils.MessagesPresentationUtil) Map(java.util.Map) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull) PsiElementVisitor(com.intellij.psi.PsiElementVisitor) ProblemsHolder(com.intellij.codeInspection.ProblemsHolder) PhpIndex(com.jetbrains.php.PhpIndex) NotNull(org.jetbrains.annotations.NotNull) BasePhpElementVisitor(com.kalessil.phpStorm.phpInspectionsEA.openApi.BasePhpElementVisitor) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Example 18 with PhpIndex

use of com.jetbrains.php.PhpIndex in project phpinspectionsea by kalessil.

the class MockingFinalClassesInspector method buildVisitor.

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

        @Override
        public void visitPhpClass(@NotNull PhpClass clazz) {
            final PhpClass parent = OpenapiResolveUtil.resolveSuperClass(clazz);
            if (parent != null && parent.getFQN().equals("\\PhpSpec\\ObjectBehavior")) {
                for (final Method method : clazz.getOwnMethods()) {
                    for (final Parameter parameter : method.getParameters()) {
                        final PsiElement typeCandidate = parameter.getFirstPsiChild();
                        if (typeCandidate instanceof ClassReference) {
                            final PsiElement resolved = OpenapiResolveUtil.resolveReference((ClassReference) typeCandidate);
                            if (resolved instanceof PhpClass && ((PhpClass) resolved).isFinal()) {
                                holder.registerProblem(typeCandidate, message);
                            }
                        }
                    }
                }
            }
        }

        @Override
        public void visitPhpMethodReference(@NotNull MethodReference reference) {
            final String methodName = reference.getName();
            final PsiElement[] arguments = reference.getParameters();
            if (methodName != null && arguments.length > 0 && methods.containsValue(methodName)) {
                final PsiElement resolved = OpenapiResolveUtil.resolveReference(reference);
                if (resolved instanceof Method && methods.get(((Method) resolved).getFQN()) != null) {
                    final PhpClass referencedClass = this.getClass(arguments[0]);
                    if (referencedClass != null && referencedClass.isFinal()) {
                        holder.registerProblem(arguments[0], message);
                    }
                }
            }
        }

        @Nullable
        private PhpClass getClass(@NotNull PsiElement expression) {
            PhpClass result = null;
            if (expression instanceof ClassConstantReference) {
                final ClassConstantReference reference = (ClassConstantReference) expression;
                final String constantName = reference.getName();
                if (constantName != null && constantName.equals("class")) {
                    final PhpExpression classReference = reference.getClassReference();
                    if (classReference instanceof ClassReference) {
                        final PsiElement resolved = OpenapiResolveUtil.resolveReference((ClassReference) classReference);
                        result = resolved instanceof PhpClass ? (PhpClass) resolved : null;
                    }
                }
            } else if (expression instanceof StringLiteralExpression) {
                final StringLiteralExpression string = (StringLiteralExpression) expression;
                final String contents = string.getContents();
                if (string.getFirstPsiChild() == null && contents.length() > 3) {
                    String fqn = contents.replaceAll("\\\\\\\\", "\\\\");
                    fqn = fqn.charAt(0) == '\\' ? fqn : '\\' + fqn;
                    final PhpIndex index = PhpIndex.getInstance(expression.getProject());
                    for (final PhpClass clazz : OpenapiResolveUtil.resolveClassesByFQN(fqn, index)) {
                        if (clazz.isFinal()) {
                            result = clazz;
                            break;
                        }
                    }
                }
            }
            /* TODO: handle __NAMESPACE__.'\Class' */
            return result;
        }
    };
}
Also used : PhpIndex(com.jetbrains.php.PhpIndex) NotNull(org.jetbrains.annotations.NotNull) BasePhpElementVisitor(com.kalessil.phpStorm.phpInspectionsEA.openApi.BasePhpElementVisitor) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Example 19 with PhpIndex

use of com.jetbrains.php.PhpIndex in project phpinspectionsea by kalessil.

the class ForeachSourceInspector method buildVisitor.

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

        @Override
        public void visitPhpForeach(@NotNull ForeachStatement foreach) {
            final PsiElement source = ExpressionSemanticUtil.getExpressionTroughParenthesis(foreach.getArray());
            if (source instanceof PhpTypedElement && !isEnsuredByPyParentIf(foreach, source)) {
                this.analyseContainer(source);
            }
        }

        /* should cover is_array/is_iterable in direct parent if of the loop, while PS types resolving gets improved */
        private boolean isEnsuredByPyParentIf(@NotNull ForeachStatement foreach, @NotNull PsiElement source) {
            boolean result = false;
            if (foreach.getPrevPsiSibling() == null) {
                final PsiElement ifCandidate = foreach.getParent() instanceof GroupStatement ? foreach.getParent().getParent() : null;
                final PsiElement conditions;
                if (ifCandidate instanceof If) {
                    conditions = ((If) ifCandidate).getCondition();
                } else if (ifCandidate instanceof ElseIf) {
                    conditions = ((ElseIf) ifCandidate).getCondition();
                } else {
                    conditions = null;
                }
                if (conditions != null) {
                    for (final PsiElement candidate : PsiTreeUtil.findChildrenOfType(conditions, source.getClass())) {
                        if (OpeanapiEquivalenceUtil.areEqual(candidate, source)) {
                            final PsiElement call = candidate.getParent() instanceof ParameterList ? candidate.getParent().getParent() : null;
                            if (OpenapiTypesUtil.isFunctionReference(call)) {
                                final String functionName = ((FunctionReference) call).getName();
                                if (functionName != null && (functionName.equals("is_array") || functionName.equals("is_iterable"))) {
                                    result = true;
                                    break;
                                }
                            }
                        }
                    }
                }
            }
            return result;
        }

        private void analyseContainer(@NotNull PsiElement container) {
            final PhpType resolvedType = OpenapiResolveUtil.resolveType((PhpTypedElement) container, container.getProject());
            if (resolvedType == null) {
                return;
            }
            final Set<String> types = new HashSet<>();
            resolvedType.filterUnknown().getTypes().forEach(t -> types.add(Types.getType(t)));
            if (types.isEmpty()) {
                /* false-positives: pre-defined variables */
                if (container instanceof Variable) {
                    final String variableName = ((Variable) container).getName();
                    if (ExpressionCostEstimateUtil.predefinedVars.contains(variableName)) {
                        return;
                    }
                }
                if (REPORT_UNRECOGNIZED_TYPES) {
                    holder.registerProblem(container, patternNotRecognized, ProblemHighlightType.WEAK_WARNING);
                }
                return;
            }
            /* false-positives: multiple return types checked only in function/method; no global context */
            final PsiElement scope = ExpressionSemanticUtil.getBlockScope(container);
            if (types.size() > 1 && !(scope instanceof Function)) {
                types.clear();
                return;
            }
            /* false-positives: mixed parameter type, parameter overridden before foreach */
            if (types.size() > 1 && scope instanceof Function && container instanceof Variable) {
                final String parameter = ((Variable) container).getName();
                final PhpEntryPointInstruction start = ((Function) scope).getControlFlow().getEntryPoint();
                final PhpAccessVariableInstruction[] uses = PhpControlFlowUtil.getFollowingVariableAccessInstructions(start, parameter, false);
                for (final PhpAccessVariableInstruction instruction : uses) {
                    final PhpPsiElement expression = instruction.getAnchor();
                    /* when matched itself, stop processing */
                    if (expression == container) {
                        break;
                    }
                    final PsiElement parent = expression.getParent();
                    if (parent instanceof AssignmentExpression) {
                        final PsiElement matchCandidate = ((AssignmentExpression) parent).getVariable();
                        if (matchCandidate != null && OpeanapiEquivalenceUtil.areEqual(matchCandidate, container)) {
                            types.clear();
                            return;
                        }
                    }
                }
            }
            /* false-positives: array type parameter declaration adds mixed */
            if (types.size() > 1 && scope instanceof Function && container instanceof ArrayAccessExpression) {
                final PsiElement candidate = ((ArrayAccessExpression) container).getValue();
                if (candidate instanceof Variable && types.contains(Types.strMixed) && types.contains(Types.strArray)) {
                    types.remove(Types.strMixed);
                }
            }
            /* gracefully request to specify exact types which can appear (mixed, object) */
            if (types.contains(Types.strMixed)) {
                /* false-positive: mixed definitions from stub functions */
                boolean isStubFunction = false;
                if (OpenapiTypesUtil.isFunctionReference(container)) {
                    final PsiElement function = OpenapiResolveUtil.resolveReference((FunctionReference) container);
                    final String filePath = function == null ? null : function.getContainingFile().getVirtualFile().getCanonicalPath();
                    isStubFunction = filePath != null && filePath.contains(".jar!") && filePath.contains("/stubs/");
                }
                /* false-positive: mixed definition from array type */
                if (!isStubFunction && !types.contains(Types.strArray) && REPORT_MIXED_TYPES) {
                    final String message = String.format(patternMixedTypes, Types.strMixed);
                    holder.registerProblem(container, message, ProblemHighlightType.WEAK_WARNING);
                }
                types.remove(Types.strMixed);
            }
            if (types.contains(Types.strObject)) {
                if (REPORT_MIXED_TYPES) {
                    final String message = String.format(patternMixedTypes, Types.strObject);
                    holder.registerProblem(container, message, ProblemHighlightType.WEAK_WARNING);
                }
                types.remove(Types.strObject);
            }
            /* respect patter when returned array and bool|null for indicating failures*/
            if (types.size() == 2 && types.contains(Types.strArray)) {
                types.remove(Types.strBoolean);
                types.remove(Types.strNull);
            }
            /* do not process foreach-compatible types */
            types.remove(Types.strArray);
            types.remove(Types.strIterable);
            types.remove("\\Traversable");
            types.remove("\\Iterator");
            types.remove("\\IteratorAggregate");
            /* don't process mysterious empty set type */
            types.remove(Types.strEmptySet);
            /* iterate rest of types */
            if (!types.isEmpty()) {
                final PhpIndex index = PhpIndex.getInstance(holder.getProject());
                for (final String type : types) {
                    /* report if scalar type is met */
                    if (!type.startsWith("\\")) {
                        holder.registerProblem(container, String.format(patternScalar, type), ProblemHighlightType.GENERIC_ERROR);
                        continue;
                    }
                    /* check classes for the Traversable interface in the inheritance chain */
                    final List<PhpClass> classes = OpenapiResolveUtil.resolveClassesAndInterfacesByFQN(type, index);
                    if (!classes.isEmpty()) {
                        boolean hasTraversable = false;
                        for (final PhpClass clazz : classes) {
                            final Set<PhpClass> interfaces = InterfacesExtractUtil.getCrawlInheritanceTree(clazz, false);
                            if (!interfaces.isEmpty()) {
                                hasTraversable = interfaces.stream().anyMatch(i -> i.getFQN().equals("\\Traversable"));
                                interfaces.clear();
                                if (hasTraversable) {
                                    break;
                                }
                            }
                        }
                        classes.clear();
                        if (!hasTraversable) {
                            holder.registerProblem(container, String.format(patternObject, type));
                        }
                    }
                }
                types.clear();
            }
        }
    };
}
Also used : PhpEntryPointInstruction(com.jetbrains.php.codeInsight.controlFlow.instructions.PhpEntryPointInstruction) BasePhpInspection(com.kalessil.phpStorm.phpInspectionsEA.openApi.BasePhpInspection) com.jetbrains.php.lang.psi.elements(com.jetbrains.php.lang.psi.elements) InterfacesExtractUtil(com.kalessil.phpStorm.phpInspectionsEA.utils.hierarhy.InterfacesExtractUtil) Set(java.util.Set) ExpressionCostEstimateUtil(com.kalessil.phpStorm.phpInspectionsEA.inspectors.ifs.utils.ExpressionCostEstimateUtil) PhpIndex(com.jetbrains.php.PhpIndex) OptionsComponent(com.kalessil.phpStorm.phpInspectionsEA.options.OptionsComponent) PhpControlFlowUtil(com.jetbrains.php.codeInsight.controlFlow.PhpControlFlowUtil) HashSet(java.util.HashSet) PsiTreeUtil(com.intellij.psi.util.PsiTreeUtil) BasePhpElementVisitor(com.kalessil.phpStorm.phpInspectionsEA.openApi.BasePhpElementVisitor) com.kalessil.phpStorm.phpInspectionsEA.utils(com.kalessil.phpStorm.phpInspectionsEA.utils) List(java.util.List) PsiElement(com.intellij.psi.PsiElement) ProblemHighlightType(com.intellij.codeInspection.ProblemHighlightType) PhpType(com.jetbrains.php.lang.psi.resolve.types.PhpType) NotNull(org.jetbrains.annotations.NotNull) PsiElementVisitor(com.intellij.psi.PsiElementVisitor) ProblemsHolder(com.intellij.codeInspection.ProblemsHolder) PhpAccessVariableInstruction(com.jetbrains.php.codeInsight.controlFlow.instructions.PhpAccessVariableInstruction) javax.swing(javax.swing) PhpEntryPointInstruction(com.jetbrains.php.codeInsight.controlFlow.instructions.PhpEntryPointInstruction) NotNull(org.jetbrains.annotations.NotNull) PhpType(com.jetbrains.php.lang.psi.resolve.types.PhpType) BasePhpElementVisitor(com.kalessil.phpStorm.phpInspectionsEA.openApi.BasePhpElementVisitor) PsiElement(com.intellij.psi.PsiElement) HashSet(java.util.HashSet) PhpAccessVariableInstruction(com.jetbrains.php.codeInsight.controlFlow.instructions.PhpAccessVariableInstruction) PhpIndex(com.jetbrains.php.PhpIndex) NotNull(org.jetbrains.annotations.NotNull)

Example 20 with PhpIndex

use of com.jetbrains.php.PhpIndex in project phpinspectionsea by kalessil.

the class CollectPossibleThrowsUtil method collectNestedAndWorkflowExceptions.

public static HashMap<PhpClass, HashSet<PsiElement>> collectNestedAndWorkflowExceptions(PsiElement scope, HashSet<PsiElement> processed, @NotNull final ProblemsHolder holder) {
    final HashMap<PhpClass, HashSet<PsiElement>> exceptions = new HashMap<>();
    /* recursively invoke and analyse nested try-catches checks */
    final Collection<Try> tryStatements = PsiTreeUtil.findChildrenOfType(scope, Try.class);
    if (tryStatements.size() > 0) {
        for (Try nestedTry : tryStatements) {
            if (!processed.contains(nestedTry)) {
                /* process nested workflow */
                final HashMap<PhpClass, HashSet<PsiElement>> nestedTryExceptions = collectNestedAndWorkflowExceptions(nestedTry, processed, holder);
                // holder.registerProblem(nestedTry.getFirstChild(), "Nested: " + nestedTryExceptions.toString(), ProblemHighlightType.WEAK_WARNING);
                if (nestedTryExceptions.size() > 0) {
                    for (final Map.Entry<PhpClass, HashSet<PsiElement>> nestedTryExceptionsPair : nestedTryExceptions.entrySet()) {
                        /* extract pairs Exception class => source expressions */
                        final PhpClass key = nestedTryExceptionsPair.getKey();
                        final HashSet<PsiElement> expressionsToDispatch = nestedTryExceptionsPair.getValue();
                        if (exceptions.containsKey(key)) {
                            /* merge entries and release refs */
                            exceptions.get(key).addAll(expressionsToDispatch);
                            expressionsToDispatch.clear();
                        } else {
                            /* store as it is */
                            exceptions.put(key, expressionsToDispatch);
                        }
                    }
                    nestedTryExceptions.clear();
                }
            }
        }
        tryStatements.clear();
    }
    /* process try-catch */
    if (scope instanceof Try) {
        /* extract workflow exceptions */
        HashMap<PhpClass, HashSet<PsiElement>> tryWorkflowExceptions = collectTryWorkflowExceptions((Try) scope, processed, holder);
        // holder.registerProblem(scope.getFirstChild(), "Throws: " + tryWorkflowExceptions.toString(), ProblemHighlightType.WEAK_WARNING);
        /* mark processed and exit, as try-catch handled in special way */
        processed.add(scope);
        exceptions.clear();
        return tryWorkflowExceptions;
    }
    /* process new statements: throws, constructors */
    Collection<NewExpression> newExpressions = PsiTreeUtil.findChildrenOfType(scope, NewExpression.class);
    if (newExpressions.size() > 0) {
        for (NewExpression newExpression : newExpressions) {
            /* skip processed */
            if (processed.contains(newExpression)) {
                continue;
            }
            // holder.registerProblem(newExpression, "New expression wil be analyzed", ProblemHighlightType.WEAK_WARNING);
            /* skip what can not be resolved */
            ClassReference newClassRef = newExpression.getClassReference();
            if (null == newClassRef) {
                processed.add(newExpression);
                continue;
            }
            PhpClass newClass;
            final PsiElement resolved = OpenapiResolveUtil.resolveReference(newClassRef);
            if (resolved instanceof PhpClass) {
                newClass = (PhpClass) resolved;
            } else if (resolved instanceof Method) {
                newClass = ((Method) resolved).getContainingClass();
            } else {
                processed.add(newExpression);
                continue;
            }
            /* throws processed */
            if (newExpression.getParent() instanceof PhpThrow) {
                /* put an expression, create container if necessary */
                if (!exceptions.containsKey(newClass)) {
                    exceptions.put(newClass, new HashSet<>());
                }
                exceptions.get(newClass).add(newExpression.getParent());
                processed.add(newExpression);
                continue;
            }
            /* process constructors invocation */
            final Method constructor = newClass == null ? null : newClass.getConstructor();
            if (constructor != null) {
                // holder.registerProblem(newExpression, "Constructor found", ProblemHighlightType.WEAK_WARNING);
                /* lookup for annotated exceptions */
                final HashSet<PhpClass> constructorExceptions = new HashSet<>();
                ThrowsResolveUtil.resolveThrownExceptions(constructor, constructorExceptions);
                /* link expression with each possible exception */
                if (constructorExceptions.size() > 0) {
                    for (PhpClass constructorException : constructorExceptions) {
                        /* put an expression, create container if necessary */
                        if (!exceptions.containsKey(constructorException)) {
                            exceptions.put(constructorException, new HashSet<>());
                        }
                        exceptions.get(constructorException).add(newExpression.getParent());
                    }
                    constructorExceptions.clear();
                }
            }
            processed.add(newExpression);
        }
        newExpressions.clear();
    }
    /* process throws - some of them might not use new-expression */
    final Project project = holder.getProject();
    final PhpIndex objIndex = PhpIndex.getInstance(project);
    Collection<PhpThrow> throwExpressions = PsiTreeUtil.findChildrenOfType(scope, PhpThrow.class);
    if (!throwExpressions.isEmpty()) {
        for (final PhpThrow throwExpression : throwExpressions) {
            /* skip processed */
            if (processed.contains(throwExpression)) {
                continue;
            }
            /* resolve argument */
            final PsiElement argument = throwExpression.getArgument();
            if (argument instanceof PhpTypedElement) {
                /* resolve argument types */
                final HashSet<String> types = new HashSet<>();
                final PhpType resolved = OpenapiResolveUtil.resolveType((PhpTypedElement) argument, project);
                if (resolved != null) {
                    resolved.filterUnknown().getTypes().forEach(t -> types.add(Types.getType(t)));
                }
                if (!types.isEmpty()) {
                    /* remove extra definition of \Exception unexpectedly added by PhpStorm */
                    final boolean dropExtraDefinitions = argument instanceof Variable && types.size() > 1 && types.contains("\\Exception");
                    if (dropExtraDefinitions) {
                        types.remove("\\Exception");
                    }
                    for (final String type : types) {
                        if (type.startsWith("\\")) {
                            /* process classes references */
                            final Collection<PhpClass> classes = OpenapiResolveUtil.resolveClassesByFQN(type, objIndex);
                            if (!classes.isEmpty()) {
                                /* put an expression, create container if necessary */
                                final PhpClass exception = classes.iterator().next();
                                exceptions.computeIfAbsent(exception, e -> new HashSet<>()).add(throwExpression);
                            }
                        }
                    }
                    types.clear();
                }
            }
            processed.add(throwExpression);
        }
        throwExpressions.clear();
    }
    /* process nested calls */
    Collection<MethodReference> calls = PsiTreeUtil.findChildrenOfType(scope, MethodReference.class);
    if (calls.size() > 0) {
        for (MethodReference call : calls) {
            /* skip processed */
            if (processed.contains(call)) {
                continue;
            }
            PsiElement methodResolved = OpenapiResolveUtil.resolveReference(call);
            if (methodResolved instanceof Method) {
                /* lookup for annotated exceptions */
                final HashSet<PhpClass> methodExceptions = new HashSet<>();
                ThrowsResolveUtil.resolveThrownExceptions((Method) methodResolved, methodExceptions);
                /* link expression with each possible exception */
                if (methodExceptions.size() > 0) {
                    for (PhpClass methodException : methodExceptions) {
                        /* put an expression, create container if necessary */
                        if (!exceptions.containsKey(methodException)) {
                            exceptions.put(methodException, new HashSet<>());
                        }
                        exceptions.get(methodException).add(call);
                    }
                    methodExceptions.clear();
                }
            }
            processed.add(call);
        }
        calls.clear();
    }
    return exceptions;
}
Also used : com.jetbrains.php.lang.psi.elements(com.jetbrains.php.lang.psi.elements) Types(com.kalessil.phpStorm.phpInspectionsEA.utils.Types) InterfacesExtractUtil(com.kalessil.phpStorm.phpInspectionsEA.utils.hierarhy.InterfacesExtractUtil) Collection(java.util.Collection) HashMap(java.util.HashMap) PhpIndex(com.jetbrains.php.PhpIndex) HashSet(java.util.HashSet) PsiTreeUtil(com.intellij.psi.util.PsiTreeUtil) OpenapiResolveUtil(com.kalessil.phpStorm.phpInspectionsEA.utils.OpenapiResolveUtil) ThrowsResolveUtil(com.kalessil.phpStorm.phpInspectionsEA.utils.phpDoc.ThrowsResolveUtil) Map(java.util.Map) PsiElement(com.intellij.psi.PsiElement) Project(com.intellij.openapi.project.Project) PhpType(com.jetbrains.php.lang.psi.resolve.types.PhpType) NotNull(org.jetbrains.annotations.NotNull) ProblemsHolder(com.intellij.codeInspection.ProblemsHolder) HashMap(java.util.HashMap) PhpType(com.jetbrains.php.lang.psi.resolve.types.PhpType) PsiElement(com.intellij.psi.PsiElement) HashSet(java.util.HashSet) PhpIndex(com.jetbrains.php.PhpIndex) Project(com.intellij.openapi.project.Project) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

PhpIndex (com.jetbrains.php.PhpIndex)29 PsiElement (com.intellij.psi.PsiElement)19 NotNull (org.jetbrains.annotations.NotNull)18 PhpType (com.jetbrains.php.lang.psi.resolve.types.PhpType)11 BasePhpElementVisitor (com.kalessil.phpStorm.phpInspectionsEA.openApi.BasePhpElementVisitor)11 HashSet (java.util.HashSet)11 ProblemsHolder (com.intellij.codeInspection.ProblemsHolder)9 Project (com.intellij.openapi.project.Project)9 PhpClass (com.jetbrains.php.lang.psi.elements.PhpClass)8 Nullable (org.jetbrains.annotations.Nullable)7 PsiElementVisitor (com.intellij.psi.PsiElementVisitor)6 com.jetbrains.php.lang.psi.elements (com.jetbrains.php.lang.psi.elements)6 BasePhpInspection (com.kalessil.phpStorm.phpInspectionsEA.openApi.BasePhpInspection)6 OpenapiResolveUtil (com.kalessil.phpStorm.phpInspectionsEA.utils.OpenapiResolveUtil)6 InterfacesExtractUtil (com.kalessil.phpStorm.phpInspectionsEA.utils.hierarhy.InterfacesExtractUtil)6 Set (java.util.Set)6 PsiTreeUtil (com.intellij.psi.util.PsiTreeUtil)5 PhpTokenTypes (com.jetbrains.php.lang.lexer.PhpTokenTypes)4 MessagesPresentationUtil (com.kalessil.phpStorm.phpInspectionsEA.utils.MessagesPresentationUtil)4 Types (com.kalessil.phpStorm.phpInspectionsEA.utils.Types)4