Skip to content

Commit 59c7b92

Browse files
Refactoring
1 parent c2b4444 commit 59c7b92

File tree

4 files changed

+64
-84
lines changed

4 files changed

+64
-84
lines changed
Lines changed: 10 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
1+
/*
2+
* Copyright © Magento, Inc. All rights reserved.
3+
* See COPYING.txt for license details.
4+
*/
5+
16
package com.magento.idea.magento2plugin.inspections.php;
7+
28
import com.intellij.codeInspection.ProblemHighlightType;
39
import com.intellij.codeInspection.ProblemsHolder;
4-
import com.intellij.lang.jsgraphql.psi.GraphQLQuotedString;
510
import com.intellij.psi.PsiElement;
611
import com.intellij.psi.PsiElementVisitor;
712
import com.jetbrains.php.lang.inspections.PhpInspection;
813
import com.jetbrains.php.lang.psi.elements.PhpClass;
914
import com.jetbrains.php.lang.psi.visitors.PhpElementVisitor;
10-
import com.magento.idea.magento2plugin.stubs.indexes.graphql.GraphQlResolverIndex;
15+
import com.magento.idea.magento2plugin.util.magento.graphql.GraphQlUsagesCollector;
16+
import com.magento.idea.magento2plugin.util.magento.graphql.GraphQlUtil;
1117
import org.jetbrains.annotations.NotNull;
1218

13-
import java.util.ArrayList;
14-
import java.util.HashMap;
1519
import java.util.List;
1620

1721
public class GraphQlResolverInspection extends PhpInspection {
@@ -25,51 +29,15 @@ public void visitPhpClass(PhpClass resolverClass) {
2529
GraphQlUsagesCollector collector = new GraphQlUsagesCollector();
2630
results = collector.getGraphQLUsages(resolverClass);
2731
if (results.size() > 0 ) {
28-
if (!isResolver(resolverClass)) {
29-
PsiElement currentClassNameIdentifier = ((PhpClass) resolverClass).getNameIdentifier();
32+
if (GraphQlUtil.isResolver(resolverClass)) {
33+
PsiElement currentClassNameIdentifier = resolverClass.getNameIdentifier();
3034
assert currentClassNameIdentifier != null;
3135
problemsHolder.registerProblem(currentClassNameIdentifier,
3236
"Must implements Magento\\Framework\\GraphQl\\Query\\ResolverInterface",
3337
ProblemHighlightType.ERROR);
3438
}
3539
}
3640
}
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-
}
4841
};
4942
}
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-
}
7543
}

src/com/magento/idea/magento2plugin/php/linemarker/GraphQlResolverUsageLineMarkerProvider.java

Lines changed: 4 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,16 @@
88
import com.intellij.codeInsight.daemon.LineMarkerProvider;
99
import com.intellij.codeInsight.navigation.NavigationGutterIconBuilder;
1010
import com.intellij.lang.jsgraphql.GraphQLIcons;
11-
import com.intellij.lang.jsgraphql.psi.GraphQLQuotedString;
1211
import com.intellij.psi.PsiElement;
1312
import com.intellij.psi.util.PsiTreeUtil;
1413
import com.jetbrains.php.lang.psi.elements.PhpClass;
1514
import com.magento.idea.magento2plugin.project.Settings;
16-
import com.magento.idea.magento2plugin.stubs.indexes.graphql.GraphQlResolverIndex;
15+
import com.magento.idea.magento2plugin.util.magento.graphql.GraphQlUsagesCollector;
16+
import com.magento.idea.magento2plugin.util.magento.graphql.GraphQlUtil;
1717
import org.jetbrains.annotations.NotNull;
1818
import org.jetbrains.annotations.Nullable;
19-
import java.util.ArrayList;
19+
2020
import java.util.Collection;
21-
import java.util.HashMap;
2221
import java.util.List;
2322

2423
public class GraphQlResolverUsageLineMarkerProvider implements LineMarkerProvider {
@@ -40,7 +39,7 @@ public void collectSlowLineMarkers(@NotNull List<PsiElement> psiElements, @NotNu
4039
if (psiElement instanceof PhpClass) {
4140
List<? extends PsiElement> results;
4241

43-
if (!isResolver((PhpClass) psiElement)) {
42+
if (GraphQlUtil.isResolver((PhpClass) psiElement)) {
4443
return;
4544
}
4645
GraphQlUsagesCollector collector = new GraphQlUsagesCollector();
@@ -57,41 +56,4 @@ public void collectSlowLineMarkers(@NotNull List<PsiElement> psiElements, @NotNu
5756
}
5857
}
5958
}
60-
61-
private boolean isResolver(PhpClass psiElement) {
62-
PhpClass[] implementedInterfaces = psiElement.getImplementedInterfaces();
63-
for (PhpClass implementedInterface: implementedInterfaces) {
64-
if (!implementedInterface.getFQN().equals("\\Magento\\Framework\\GraphQl\\Query\\ResolverInterface")) {
65-
continue;
66-
}
67-
return true;
68-
}
69-
return false;
70-
}
71-
72-
private static class GraphQlUsagesCollector {
73-
74-
private HashMap<String, List<GraphQLQuotedString>> graphQlCache = new HashMap<>();
75-
76-
List<GraphQLQuotedString> getGraphQLUsages(@NotNull PhpClass phpClass) {
77-
List<GraphQLQuotedString> graphQLQuotedStrings = new ArrayList<>();
78-
79-
graphQLQuotedStrings.addAll(getUsages(phpClass));
80-
81-
return graphQLQuotedStrings;
82-
}
83-
84-
List<GraphQLQuotedString> getUsages(@NotNull PhpClass phpClass) {
85-
String phpClassFQN = phpClass.getFQN();
86-
if (!graphQlCache.containsKey(phpClassFQN)) {
87-
List<GraphQLQuotedString> graphQLStringValues = extractGraphQLQuotesStringsForClass(phpClass);
88-
graphQlCache.put(phpClassFQN, graphQLStringValues);
89-
}
90-
return graphQlCache.get(phpClassFQN);
91-
}
92-
93-
List<GraphQLQuotedString> extractGraphQLQuotesStringsForClass(@NotNull PhpClass phpClass) {
94-
return GraphQlResolverIndex.getGraphQLUsages(phpClass);
95-
}
96-
}
9759
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.magento.idea.magento2plugin.util.magento.graphql;
2+
3+
import com.intellij.lang.jsgraphql.psi.GraphQLQuotedString;
4+
import com.jetbrains.php.lang.psi.elements.PhpClass;
5+
import com.magento.idea.magento2plugin.stubs.indexes.graphql.GraphQlResolverIndex;
6+
import org.jetbrains.annotations.NotNull;
7+
8+
import java.util.ArrayList;
9+
import java.util.HashMap;
10+
import java.util.List;
11+
12+
public class GraphQlUsagesCollector {
13+
14+
public HashMap<String, List<GraphQLQuotedString>> graphQlCache = new HashMap<>();
15+
16+
public List<GraphQLQuotedString> getGraphQLUsages(@NotNull PhpClass phpClass) {
17+
List<GraphQLQuotedString> graphQLQuotedStrings = new ArrayList<>();
18+
19+
graphQLQuotedStrings.addAll(getUsages(phpClass));
20+
21+
return graphQLQuotedStrings;
22+
}
23+
24+
List<GraphQLQuotedString> getUsages(@NotNull PhpClass phpClass) {
25+
String phpClassFQN = phpClass.getFQN();
26+
if (!graphQlCache.containsKey(phpClassFQN)) {
27+
List<GraphQLQuotedString> graphQLStringValues = extractGraphQLQuotesStringsForClass(phpClass);
28+
graphQlCache.put(phpClassFQN, graphQLStringValues);
29+
}
30+
return graphQlCache.get(phpClassFQN);
31+
}
32+
33+
List<GraphQLQuotedString> extractGraphQLQuotesStringsForClass(@NotNull PhpClass phpClass) {
34+
return GraphQlResolverIndex.getGraphQLUsages(phpClass);
35+
}
36+
}

src/com/magento/idea/magento2plugin/util/magento/graphql/GraphQlUtil.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import com.intellij.lang.jsgraphql.psi.GraphQLStringValue;
88
import com.intellij.psi.PsiElement;
9+
import com.jetbrains.php.lang.psi.elements.PhpClass;
910
import org.jetbrains.annotations.NotNull;
1011
import org.jetbrains.annotations.Nullable;
1112

@@ -40,4 +41,17 @@ public static GraphQLStringValue fetchResolverQuotedStringFromArgument(PsiElemen
4041

4142
return argumentStringValue;
4243
}
44+
45+
public static boolean isResolver(PhpClass psiElement) {
46+
PhpClass[] implementedInterfaces = psiElement.getImplementedInterfaces();
47+
for (PhpClass implementedInterface: implementedInterfaces) {
48+
if (!implementedInterface.getFQN().equals("\\Magento\\Framework\\GraphQl\\Query\\ResolverInterface")) {
49+
continue;
50+
}
51+
return false;
52+
}
53+
return true;
54+
}
55+
56+
4357
}

0 commit comments

Comments
 (0)