|
| 1 | +package com.magento.idea.magento2plugin.inspections.php; |
| 2 | +import com.intellij.codeInspection.ProblemHighlightType; |
| 3 | +import com.intellij.codeInspection.ProblemsHolder; |
| 4 | +import com.intellij.lang.jsgraphql.psi.GraphQLQuotedString; |
| 5 | +import com.intellij.psi.PsiElement; |
| 6 | +import com.intellij.psi.PsiElementVisitor; |
| 7 | +import com.jetbrains.php.lang.inspections.PhpInspection; |
| 8 | +import com.jetbrains.php.lang.psi.elements.PhpClass; |
| 9 | +import com.jetbrains.php.lang.psi.visitors.PhpElementVisitor; |
| 10 | +import com.magento.idea.magento2plugin.stubs.indexes.graphql.GraphQlResolverIndex; |
| 11 | +import org.jetbrains.annotations.NotNull; |
| 12 | + |
| 13 | +import java.util.ArrayList; |
| 14 | +import java.util.HashMap; |
| 15 | +import java.util.List; |
| 16 | + |
| 17 | +public class GraphQlResolverInspection extends PhpInspection { |
| 18 | + |
| 19 | + @NotNull |
| 20 | + @Override |
| 21 | + public PsiElementVisitor buildVisitor(@NotNull ProblemsHolder problemsHolder, boolean b) { |
| 22 | + return new PhpElementVisitor() { |
| 23 | + public void visitPhpClass(PhpClass resolverClass) { |
| 24 | + List<? extends PsiElement> results; |
| 25 | + GraphQlUsagesCollector collector = new GraphQlUsagesCollector(); |
| 26 | + results = collector.getGraphQLUsages(resolverClass); |
| 27 | + if (results.size() > 0 ) { |
| 28 | + if (!isResolver(resolverClass)) { |
| 29 | + PsiElement currentClassNameIdentifier = ((PhpClass) resolverClass).getNameIdentifier(); |
| 30 | + assert currentClassNameIdentifier != null; |
| 31 | + problemsHolder.registerProblem(currentClassNameIdentifier, |
| 32 | + "Must implements Magento\\Framework\\GraphQl\\Query\\ResolverInterface", |
| 33 | + ProblemHighlightType.ERROR); |
| 34 | + } |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + private boolean isResolver(PhpClass psiElement) { |
| 39 | + PhpClass[] implementedInterfaces = psiElement.getImplementedInterfaces(); |
| 40 | + for (PhpClass implementedInterface: implementedInterfaces) { |
| 41 | + if (!implementedInterface.getFQN().equals("\\Magento\\Framework\\GraphQl\\Query\\ResolverInterface")) { |
| 42 | + continue; |
| 43 | + } |
| 44 | + return true; |
| 45 | + } |
| 46 | + return false; |
| 47 | + } |
| 48 | + }; |
| 49 | + } |
| 50 | + private class GraphQlUsagesCollector { |
| 51 | + |
| 52 | + private HashMap<String, List<GraphQLQuotedString>> graphQlCache = new HashMap<>(); |
| 53 | + |
| 54 | + List<GraphQLQuotedString> getGraphQLUsages(@NotNull PhpClass phpClass) { |
| 55 | + List<GraphQLQuotedString> graphQLQuotedStrings = new ArrayList<>(); |
| 56 | + |
| 57 | + graphQLQuotedStrings.addAll(getUsages(phpClass)); |
| 58 | + |
| 59 | + return graphQLQuotedStrings; |
| 60 | + } |
| 61 | + |
| 62 | + List<GraphQLQuotedString> getUsages(@NotNull PhpClass phpClass) { |
| 63 | + String phpClassFQN = phpClass.getFQN(); |
| 64 | + if (!graphQlCache.containsKey(phpClassFQN)) { |
| 65 | + List<GraphQLQuotedString> graphQLStringValues = extractGraphQLQuotesStringsForClass(phpClass); |
| 66 | + graphQlCache.put(phpClassFQN, graphQLStringValues); |
| 67 | + } |
| 68 | + return graphQlCache.get(phpClassFQN); |
| 69 | + } |
| 70 | + |
| 71 | + List<GraphQLQuotedString> extractGraphQLQuotesStringsForClass(@NotNull PhpClass phpClass) { |
| 72 | + return GraphQlResolverIndex.getGraphQLUsages(phpClass); |
| 73 | + } |
| 74 | + } |
| 75 | +} |
0 commit comments