1
- /**
1
+ /*
2
2
* Copyright © Magento, Inc. All rights reserved.
3
3
* See COPYING.txt for license details.
4
4
*/
5
+
5
6
package com .magento .idea .magento2plugin .stubs .indexes ;
6
7
7
8
import com .intellij .ide .highlighter .XmlFileType ;
13
14
import com .intellij .psi .xml .XmlDocument ;
14
15
import com .intellij .psi .xml .XmlFile ;
15
16
import com .intellij .psi .xml .XmlTag ;
16
- import com .intellij .util .indexing .*;
17
+ import com .intellij .util .indexing .DataIndexer ;
18
+ import com .intellij .util .indexing .FileBasedIndex ;
19
+ import com .intellij .util .indexing .FileContent ;
20
+ import com .intellij .util .indexing .ID ;
21
+ import com .intellij .util .indexing .ScalarIndexExtension ;
17
22
import com .intellij .util .io .EnumeratorStringDescriptor ;
18
23
import com .intellij .util .io .KeyDescriptor ;
19
24
import com .jetbrains .php .lang .PhpLangUtil ;
20
25
import com .jetbrains .php .lang .psi .elements .Method ;
21
26
import com .jetbrains .php .lang .psi .elements .PhpClass ;
22
- import com .magento .idea .magento2plugin .project .Settings ;
23
27
import com .magento .idea .magento2plugin .linemarker .xml .LineMarkerXmlTagDecorator ;
28
+ import com .magento .idea .magento2plugin .project .Settings ;
29
+ import java .util .ArrayList ;
30
+ import java .util .Collection ;
31
+ import java .util .HashMap ;
32
+ import java .util .List ;
33
+ import java .util .Map ;
24
34
import org .jetbrains .annotations .NonNls ;
25
35
import org .jetbrains .annotations .NotNull ;
26
36
27
- import java .util .*;
28
-
29
37
/**
30
38
* Indexer for classes/interfaces which have methods exposed via Web API.
31
39
*/
32
40
public class WebApiTypeIndex extends ScalarIndexExtension <String > {
33
41
34
- public static final ID <String , Void > KEY = ID .create ("com.magento.idea.magento2plugin.stubs.indexes.webapi_type" );
35
-
42
+ public static final ID <String , Void > KEY = ID .create (
43
+ "com.magento.idea.magento2plugin.stubs.indexes.webapi_type"
44
+ );
36
45
private final KeyDescriptor <String > keyDescriptor = new EnumeratorStringDescriptor ();
37
46
38
- @ NotNull
39
47
@ Override
40
- public ID <String , Void > getName () {
48
+ public @ NotNull ID <String , Void > getName () {
41
49
return KEY ;
42
50
}
43
51
44
- @ NotNull
52
+ @ SuppressWarnings ( "PMD.CognitiveComplexity" )
45
53
@ Override
46
- public DataIndexer <String , Void , FileContent > getIndexer () {
54
+ public @ NotNull DataIndexer <String , Void , FileContent > getIndexer () {
47
55
return inputData -> {
48
- Map <String , Void > map = new HashMap <>();
56
+ final Map <String , Void > map = new HashMap <>();
57
+ final PsiFile psiFile = inputData .getPsiFile ();
49
58
50
- PsiFile psiFile = inputData .getPsiFile ();
51
59
if (!Settings .isEnabled (psiFile .getProject ())) {
52
60
return map ;
53
61
}
54
62
55
63
if (!(psiFile instanceof XmlFile )) {
56
64
return map ;
57
65
}
66
+ final XmlDocument document = ((XmlFile ) psiFile ).getDocument ();
58
67
59
- XmlDocument document = ((XmlFile ) psiFile ).getDocument ();
60
68
if (document == null ) {
61
69
return map ;
62
70
}
71
+ final XmlTag [] xmlTags = PsiTreeUtil .getChildrenOfType (
72
+ psiFile .getFirstChild (),
73
+ XmlTag .class
74
+ );
63
75
64
- XmlTag xmlTags [] = PsiTreeUtil .getChildrenOfType (psiFile .getFirstChild (), XmlTag .class );
65
76
if (xmlTags == null ) {
66
77
return map ;
67
78
}
68
79
69
- for (XmlTag xmlTag : xmlTags ) {
70
- if (xmlTag .getName ().equals ("routes" )) {
71
- for (XmlTag routeNode : xmlTag .findSubTags ("route" )) {
72
- for (XmlTag serviceNode : routeNode .findSubTags ("service" )) {
73
- String typeName = serviceNode .getAttributeValue ("class" );
80
+ for (final XmlTag xmlTag : xmlTags ) {
81
+ if ("routes" .equals (xmlTag .getName ())) {
82
+ for (final XmlTag routeNode : xmlTag .findSubTags ("route" )) {
83
+ for (final XmlTag serviceNode : routeNode .findSubTags ("service" )) {
84
+ final String typeName = serviceNode .getAttributeValue ("class" );
85
+
74
86
if (typeName != null ) {
75
87
map .put (PhpLangUtil .toPresentableFQN (typeName ), null );
76
88
}
77
89
}
78
90
}
79
91
}
80
92
}
93
+
81
94
return map ;
82
95
};
83
96
}
84
97
85
- @ NotNull
86
98
@ Override
87
- public KeyDescriptor <String > getKeyDescriptor () {
99
+ public @ NotNull KeyDescriptor <String > getKeyDescriptor () {
88
100
return keyDescriptor ;
89
101
}
90
102
91
- @ NotNull
92
103
@ Override
93
- public FileBasedIndex .InputFilter getInputFilter () {
94
- return file -> (
95
- file .getFileType () == XmlFileType .INSTANCE && file .getNameWithoutExtension ().equals ("webapi" )
96
- && !file .getPath ().contains ("testsuite" ) && !file .getPath ().contains ("_files" )
97
- );
104
+ public @ NotNull FileBasedIndex .InputFilter getInputFilter () {
105
+ return file -> file .getFileType () == XmlFileType .INSTANCE
106
+ && "webapi" .equals (file .getNameWithoutExtension ())
107
+ && !file .getPath ().contains ("testsuite" ) && !file .getPath ().contains ("_files" );
98
108
}
99
109
100
110
@ Override
@@ -109,45 +119,77 @@ public int getVersion() {
109
119
110
120
/**
111
121
* Get list of Web API routes associated with the provided method.
112
- *
113
122
* Parent classes are not taken into account.
123
+ *
124
+ * @param method Method
125
+ *
126
+ * @return List[XmlTag]
114
127
*/
115
- public static List <XmlTag > getWebApiRoutes (Method method ) {
116
- List <XmlTag > tags = new ArrayList <>();
128
+ public static List <XmlTag > getWebApiRoutes (final Method method ) {
129
+ final List <XmlTag > tags = new ArrayList <>();
130
+
117
131
if (!method .getAccess ().isPublic ()) {
118
132
return tags ;
119
133
}
120
- PhpClass phpClass = method .getContainingClass ();
121
- String methodFqn = method . getName ();
134
+ final PhpClass phpClass = method .getContainingClass ();
135
+
122
136
if (phpClass == null ) {
123
137
return tags ;
124
138
}
125
- String classFqn = phpClass .getPresentableFQN ();
126
- Collection <VirtualFile > containingFiles = FileBasedIndex
127
- .getInstance ().getContainingFiles (KEY , classFqn , GlobalSearchScope .allScope (phpClass .getProject ()));
139
+ final String classFqn = phpClass .getPresentableFQN ();
140
+ final Collection <VirtualFile > containingFiles = FileBasedIndex
141
+ .getInstance ()
142
+ .getContainingFiles (
143
+ KEY ,
144
+ classFqn ,
145
+ GlobalSearchScope .allScope (phpClass .getProject ())
146
+ );
147
+
148
+ final PsiManager psiManager = PsiManager .getInstance (phpClass .getProject ());
149
+ final String methodFqn = method .getName ();
150
+
151
+ for (final VirtualFile virtualFile : containingFiles ) {
152
+ if (virtualFile .getFileType () != XmlFileType .INSTANCE ) {
153
+ continue ;
154
+ }
155
+ final XmlFile file = (XmlFile ) psiManager .findFile (virtualFile );
128
156
129
- PsiManager psiManager = PsiManager .getInstance (phpClass .getProject ());
130
- for (VirtualFile virtualFile : containingFiles ) {
131
- XmlFile file = (XmlFile ) psiManager .findFile (virtualFile );
132
157
if (file == null ) {
133
158
continue ;
134
159
}
135
- XmlTag rootTag = file .getRootTag ();
160
+ final XmlTag rootTag = file .getRootTag ();
161
+
162
+ if (rootTag == null ) {
163
+ continue ;
164
+ }
136
165
fillRelatedTags (classFqn , methodFqn , rootTag , tags );
137
166
}
167
+
138
168
return tags ;
139
169
}
140
170
141
171
/**
142
- * Find routes related to the specified method within single webapi.xml
172
+ * Find routes related to the specified method within single webapi.xml.
173
+ *
174
+ * @param classFqn String
175
+ * @param methodFqn String
176
+ * @param parentTag XmlTag
177
+ * @param tagsReferences List[XmlTag]
143
178
*/
144
- private static void fillRelatedTags (String classFqn , String methodFqn , XmlTag parentTag , List <XmlTag > tagsReferences ) {
145
- for (XmlTag routeNode : parentTag .findSubTags ("route" )) {
146
- for (XmlTag serviceNode : routeNode .findSubTags ("service" )) {
147
- String typeName = serviceNode .getAttributeValue ("class" );
148
- String methodName = serviceNode .getAttributeValue ("method" );
179
+ @ SuppressWarnings ("PMD.AvoidInstantiatingObjectsInLoops" )
180
+ private static void fillRelatedTags (
181
+ final String classFqn ,
182
+ final String methodFqn ,
183
+ final XmlTag parentTag ,
184
+ final List <XmlTag > tagsReferences
185
+ ) {
186
+ for (final XmlTag routeNode : parentTag .findSubTags ("route" )) {
187
+ for (final XmlTag serviceNode : routeNode .findSubTags ("service" )) {
188
+ final String typeName = serviceNode .getAttributeValue ("class" );
189
+ final String methodName = serviceNode .getAttributeValue ("method" );
190
+
149
191
if (typeName != null && typeName .equals (classFqn )
150
- && methodName != null && methodName .equals (methodFqn )
192
+ && methodName != null && methodName .equals (methodFqn )
151
193
) {
152
194
tagsReferences .add (new WebApiLineMarkerXmlTagDecorator (routeNode ));
153
195
}
@@ -160,25 +202,24 @@ private static void fillRelatedTags(String classFqn, String methodFqn, XmlTag pa
160
202
*/
161
203
private static class WebApiLineMarkerXmlTagDecorator extends LineMarkerXmlTagDecorator {
162
204
163
- WebApiLineMarkerXmlTagDecorator (XmlTag xmlTag ) {
205
+ public WebApiLineMarkerXmlTagDecorator (final XmlTag xmlTag ) {
164
206
super (xmlTag );
165
207
}
166
208
167
- @ NotNull
168
209
@ Override
169
- public String getDescription () {
210
+ public @ NotNull String getDescription () {
170
211
return "" ;
171
212
}
172
213
173
214
@ Override
174
- @ NotNull
175
- @ NonNls
176
- public String getName () {
177
- String httpMethod = this .xmlTag .getAttributeValue ("method" );
178
- String route = this .xmlTag .getAttributeValue ("url" );
215
+ public @ NonNls @ NotNull String getName () {
216
+ final String httpMethod = this .xmlTag .getAttributeValue ("method" );
217
+ final String route = this .xmlTag .getAttributeValue ("url" );
218
+
179
219
if (httpMethod != null && route != null ) {
180
220
return String .format (" %-7s %s" , httpMethod , route );
181
221
}
222
+
182
223
return xmlTag .getName ();
183
224
}
184
225
}
0 commit comments