1- /**
1+ /*
22 * Copyright © Magento, Inc. All rights reserved.
33 * See COPYING.txt for license details.
44 */
5+
56package com .magento .idea .magento2plugin .stubs .indexes ;
67
78import com .intellij .ide .highlighter .XmlFileType ;
1314import com .intellij .psi .xml .XmlDocument ;
1415import com .intellij .psi .xml .XmlFile ;
1516import 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 ;
1722import com .intellij .util .io .EnumeratorStringDescriptor ;
1823import com .intellij .util .io .KeyDescriptor ;
1924import com .jetbrains .php .lang .PhpLangUtil ;
2025import com .jetbrains .php .lang .psi .elements .Method ;
2126import com .jetbrains .php .lang .psi .elements .PhpClass ;
22- import com .magento .idea .magento2plugin .project .Settings ;
2327import 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 ;
2434import org .jetbrains .annotations .NonNls ;
2535import org .jetbrains .annotations .NotNull ;
2636
27- import java .util .*;
28-
2937/**
3038 * Indexer for classes/interfaces which have methods exposed via Web API.
3139 */
3240public class WebApiTypeIndex extends ScalarIndexExtension <String > {
3341
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+ );
3645 private final KeyDescriptor <String > keyDescriptor = new EnumeratorStringDescriptor ();
3746
38- @ NotNull
3947 @ Override
40- public ID <String , Void > getName () {
48+ public @ NotNull ID <String , Void > getName () {
4149 return KEY ;
4250 }
4351
44- @ NotNull
52+ @ SuppressWarnings ( "PMD.CognitiveComplexity" )
4553 @ Override
46- public DataIndexer <String , Void , FileContent > getIndexer () {
54+ public @ NotNull DataIndexer <String , Void , FileContent > getIndexer () {
4755 return inputData -> {
48- Map <String , Void > map = new HashMap <>();
56+ final Map <String , Void > map = new HashMap <>();
57+ final PsiFile psiFile = inputData .getPsiFile ();
4958
50- PsiFile psiFile = inputData .getPsiFile ();
5159 if (!Settings .isEnabled (psiFile .getProject ())) {
5260 return map ;
5361 }
5462
5563 if (!(psiFile instanceof XmlFile )) {
5664 return map ;
5765 }
66+ final XmlDocument document = ((XmlFile ) psiFile ).getDocument ();
5867
59- XmlDocument document = ((XmlFile ) psiFile ).getDocument ();
6068 if (document == null ) {
6169 return map ;
6270 }
71+ final XmlTag [] xmlTags = PsiTreeUtil .getChildrenOfType (psiFile .getFirstChild (), XmlTag .class );
6372
64- XmlTag xmlTags [] = PsiTreeUtil .getChildrenOfType (psiFile .getFirstChild (), XmlTag .class );
6573 if (xmlTags == null ) {
6674 return map ;
6775 }
6876
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" );
77+ for (final XmlTag xmlTag : xmlTags ) {
78+ if ("routes" .equals (xmlTag .getName ())) {
79+ for (final XmlTag routeNode : xmlTag .findSubTags ("route" )) {
80+ for (final XmlTag serviceNode : routeNode .findSubTags ("service" )) {
81+ final String typeName = serviceNode .getAttributeValue ("class" );
82+
7483 if (typeName != null ) {
7584 map .put (PhpLangUtil .toPresentableFQN (typeName ), null );
7685 }
7786 }
7887 }
7988 }
8089 }
90+
8191 return map ;
8292 };
8393 }
8494
85- @ NotNull
8695 @ Override
87- public KeyDescriptor <String > getKeyDescriptor () {
96+ public @ NotNull KeyDescriptor <String > getKeyDescriptor () {
8897 return keyDescriptor ;
8998 }
9099
91- @ NotNull
92100 @ 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- );
101+ public @ NotNull FileBasedIndex .InputFilter getInputFilter () {
102+ return file -> file .getFileType () == XmlFileType .INSTANCE
103+ && "webapi" .equals (file .getNameWithoutExtension ())
104+ && !file .getPath ().contains ("testsuite" ) && !file .getPath ().contains ("_files" );
98105 }
99106
100107 @ Override
@@ -109,45 +116,68 @@ public int getVersion() {
109116
110117 /**
111118 * Get list of Web API routes associated with the provided method.
112- *
113119 * Parent classes are not taken into account.
120+ *
121+ * @param method Method
122+ *
123+ * @return List[XmlTag]
114124 */
115- public static List <XmlTag > getWebApiRoutes (Method method ) {
116- List <XmlTag > tags = new ArrayList <>();
125+ public static List <XmlTag > getWebApiRoutes (final Method method ) {
126+ final List <XmlTag > tags = new ArrayList <>();
127+
117128 if (!method .getAccess ().isPublic ()) {
118129 return tags ;
119130 }
120- PhpClass phpClass = method .getContainingClass ();
121- String methodFqn = method . getName ();
131+ final PhpClass phpClass = method .getContainingClass ();
132+
122133 if (phpClass == null ) {
123134 return tags ;
124135 }
125- String classFqn = phpClass .getPresentableFQN ();
126- Collection <VirtualFile > containingFiles = FileBasedIndex
127- .getInstance ().getContainingFiles (KEY , classFqn , GlobalSearchScope .allScope (phpClass .getProject ()));
136+ final String classFqn = phpClass .getPresentableFQN ();
137+ final Collection <VirtualFile > containingFiles = FileBasedIndex
138+ .getInstance ().getContainingFiles (
139+ KEY ,
140+ classFqn ,
141+ GlobalSearchScope .allScope (phpClass .getProject ())
142+ );
143+
144+ final PsiManager psiManager = PsiManager .getInstance (phpClass .getProject ());
145+ final String methodFqn = method .getName ();
146+
147+ for (final VirtualFile virtualFile : containingFiles ) {
148+ final XmlFile file = (XmlFile ) psiManager .findFile (virtualFile );
128149
129- PsiManager psiManager = PsiManager .getInstance (phpClass .getProject ());
130- for (VirtualFile virtualFile : containingFiles ) {
131- XmlFile file = (XmlFile ) psiManager .findFile (virtualFile );
132150 if (file == null ) {
133151 continue ;
134152 }
135- XmlTag rootTag = file .getRootTag ();
153+ final XmlTag rootTag = file .getRootTag ();
136154 fillRelatedTags (classFqn , methodFqn , rootTag , tags );
137155 }
156+
138157 return tags ;
139158 }
140159
141160 /**
142- * Find routes related to the specified method within single webapi.xml
161+ * Find routes related to the specified method within single webapi.xml.
162+ *
163+ * @param classFqn String
164+ * @param methodFqn String
165+ * @param parentTag XmlTag
166+ * @param tagsReferences List[XmlTag]
143167 */
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" );
168+ private static void fillRelatedTags (
169+ final String classFqn ,
170+ final String methodFqn ,
171+ final XmlTag parentTag ,
172+ final List <XmlTag > tagsReferences
173+ ) {
174+ for (final XmlTag routeNode : parentTag .findSubTags ("route" )) {
175+ for (final XmlTag serviceNode : routeNode .findSubTags ("service" )) {
176+ final String typeName = serviceNode .getAttributeValue ("class" );
177+ final String methodName = serviceNode .getAttributeValue ("method" );
178+
149179 if (typeName != null && typeName .equals (classFqn )
150- && methodName != null && methodName .equals (methodFqn )
180+ && methodName != null && methodName .equals (methodFqn )
151181 ) {
152182 tagsReferences .add (new WebApiLineMarkerXmlTagDecorator (routeNode ));
153183 }
@@ -160,25 +190,24 @@ private static void fillRelatedTags(String classFqn, String methodFqn, XmlTag pa
160190 */
161191 private static class WebApiLineMarkerXmlTagDecorator extends LineMarkerXmlTagDecorator {
162192
163- WebApiLineMarkerXmlTagDecorator (XmlTag xmlTag ) {
193+ public WebApiLineMarkerXmlTagDecorator (final XmlTag xmlTag ) {
164194 super (xmlTag );
165195 }
166196
167- @ NotNull
168197 @ Override
169- public String getDescription () {
198+ public @ NotNull String getDescription () {
170199 return "" ;
171200 }
172201
173202 @ Override
174- @ NotNull
175- @ NonNls
176- public String getName () {
177- String httpMethod = this .xmlTag .getAttributeValue ("method" );
178- String route = this .xmlTag .getAttributeValue ("url" );
203+ public @ NonNls @ NotNull String getName () {
204+ final String httpMethod = this .xmlTag .getAttributeValue ("method" );
205+ final String route = this .xmlTag .getAttributeValue ("url" );
206+
179207 if (httpMethod != null && route != null ) {
180208 return String .format (" %-7s %s" , httpMethod , route );
181209 }
210+
182211 return xmlTag .getName ();
183212 }
184213 }
0 commit comments