Skip to content

Commit c2b4444

Browse files
Code Inspection for graphql resolver
1 parent 0c3ff4f commit c2b4444

File tree

3 files changed

+94
-0
lines changed

3 files changed

+94
-0
lines changed

resources/META-INF/withJsGraphQl.xml

+6
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,11 @@
33
<fileBasedIndex implementation="com.magento.idea.magento2plugin.stubs.indexes.graphql.GraphQlResolverIndex" />
44
<codeInsight.lineMarkerProvider language="PHP" implementationClass="com.magento.idea.magento2plugin.php.linemarker.GraphQlResolverUsageLineMarkerProvider"/>
55
<codeInsight.lineMarkerProvider language="GraphQL" implementationClass="com.magento.idea.magento2plugin.graphql.linemarker.GraphQlResolverClassLineMarkerProvider"/>
6+
<localInspection language="PHP" groupPath="PHP"
7+
shortName="GraphQlResolverInspection" displayName="Graphql must implements ResolverInterface"
8+
groupName="Magento 2"
9+
enabledByDefault="true"
10+
level="ERROR"
11+
implementationClass="com.magento.idea.magento2plugin.inspections.php.GraphQlResolverInspection"/>
612
</extensions>
713
</idea-plugin>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<!--
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
-->
7+
<html>
8+
<body>
9+
<p>
10+
All GraphQl resolver must implements Magento\Framework\GraphQl\Query\ResolverInterface interface
11+
</p>
12+
</body>
13+
</html>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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

Comments
 (0)