1- /**
1+ /*
22 * Copyright © Magento, Inc. All rights reserved.
33 * See COPYING.txt for license details.
44 */
5+
56package com .magento .idea .magento2plugin .linemarker .php ;
67
78import com .intellij .codeInsight .daemon .LineMarkerInfo ;
1516import com .magento .idea .magento2plugin .MagentoIcons ;
1617import com .magento .idea .magento2plugin .project .Settings ;
1718import com .magento .idea .magento2plugin .stubs .indexes .WebApiTypeIndex ;
19+ import java .util .ArrayList ;
20+ import java .util .Collection ;
21+ import java .util .HashMap ;
22+ import java .util .List ;
23+ import java .util .Map ;
1824import org .jetbrains .annotations .NotNull ;
1925import org .jetbrains .annotations .Nullable ;
2026
21- import java .util .*;
22-
2327/**
2428 * Line marker for methods and classes which are exposed as web APIs.
2529 * <p/>
2832 */
2933public class WebApiLineMarkerProvider implements LineMarkerProvider {
3034
31- @ Nullable
3235 @ Override
33- public LineMarkerInfo getLineMarkerInfo (@ NotNull PsiElement psiElement ) {
36+ public @ Nullable LineMarkerInfo <?> getLineMarkerInfo (final @ NotNull PsiElement psiElement ) {
3437 return null ;
3538 }
3639
3740 @ Override
38- public void collectSlowLineMarkers (@ NotNull List <? extends PsiElement > psiElements , @ NotNull Collection <? super LineMarkerInfo <?>> collection ) {
39- if (psiElements .size () > 0 ) {
40- if (!Settings .isEnabled (psiElements .get (0 ).getProject ())) {
41- return ;
42- }
41+ public void collectSlowLineMarkers (
42+ final @ NotNull List <? extends PsiElement > psiElements ,
43+ final @ NotNull Collection <? super LineMarkerInfo <?>> collection
44+ ) {
45+ if (psiElements .isEmpty ()) {
46+ return ;
47+ }
48+
49+ if (!Settings .isEnabled (psiElements .get (0 ).getProject ())) {
50+ return ;
4351 }
44- for (PsiElement psiElement : psiElements ) {
45- WebApiRoutesCollector collector = new WebApiRoutesCollector ();
52+
53+ for (final PsiElement psiElement : psiElements ) {
54+ final WebApiRoutesCollector collector = new WebApiRoutesCollector ();
4655 List <XmlTag > results = new ArrayList <>();
56+
4757 if (psiElement instanceof Method ) {
4858 results = collector .getRoutes ((Method ) psiElement );
4959 } else if (psiElement instanceof PhpClass ) {
5060 results = collector .getRoutes ((PhpClass ) psiElement );
5161 }
5262
53- if (!( results .size () > 0 )) {
63+ if (results .isEmpty ( )) {
5464 continue ;
5565 }
5666
57- StringBuilder tooltipText = new StringBuilder ("Navigate to Web API configuration:<pre>" );
58- for (XmlTag routeTag : results ) {
67+ StringBuilder tooltipText = new StringBuilder (
68+ "Navigate to Web API configuration:<pre>"
69+ );
70+ for (final XmlTag routeTag : results ) {
5971 tooltipText .append (routeTag .getName ()).append ("\n " );
6072 }
6173 tooltipText .append ("</pre>" );
@@ -72,24 +84,27 @@ public void collectSlowLineMarkers(@NotNull List<? extends PsiElement> psiElemen
7284 */
7385 private static class WebApiRoutesCollector {
7486
75- private HashMap <String , List <XmlTag >> routesCache = new HashMap <>();
76-
77- private static final Map <String , Integer > HTTP_METHODS_SORT_ORDER = new HashMap <String , Integer >() {{
78- put ("GET" , 1 );
79- put ("PUT" , 2 );
80- put ("POS" , 3 );
81- put ("DEL" , 4 );
82- }};
87+ private final Map <String , List <XmlTag >> routesCache = new HashMap <>();
88+ private static final Map <String , Integer > HTTP_METHODS_SORT_ORDER = new HashMap <>() {
89+ {
90+ put ("GET" , 1 );
91+ put ("PUT" , 2 );
92+ put ("POS" , 3 );
93+ put ("DEL" , 4 );
94+ }
95+ };
8396
8497 /**
8598 * Get sorted list of Web API routes related to the specified class.
8699 */
87- List <XmlTag > getRoutes (@ NotNull PhpClass phpClass ) {
88- List <XmlTag > routesForClass = new ArrayList <>();
89- for (Method method : phpClass .getMethods ()) {
100+ public List <XmlTag > getRoutes (final @ NotNull PhpClass phpClass ) {
101+ final List <XmlTag > routesForClass = new ArrayList <>();
102+
103+ for (final Method method : phpClass .getMethods ()) {
90104 routesForClass .addAll (getRoutes (method ));
91105 }
92106 sortRoutes (routesForClass );
107+
93108 return routesForClass ;
94109 }
95110
@@ -98,13 +113,15 @@ List<XmlTag> getRoutes(@NotNull PhpClass phpClass) {
98113 * <p/>
99114 * Results are cached.
100115 */
101- List <XmlTag > getRoutes (@ NotNull Method method ) {
102- String methodFqn = method .getFQN ();
116+ public List <XmlTag > getRoutes (final @ NotNull Method method ) {
117+ final String methodFqn = method .getFQN ();
118+
103119 if (!routesCache .containsKey (methodFqn )) {
104120 List <XmlTag > routesForMethod = extractRoutesForMethod (method );
105121 sortRoutes (routesForMethod );
106122 routesCache .put (methodFqn , routesForMethod );
107123 }
124+
108125 return routesCache .get (methodFqn );
109126 }
110127
@@ -114,37 +131,42 @@ List<XmlTag> getRoutes(@NotNull Method method) {
114131 * Web API declarations for parent classes are taken into account.
115132 * Results are not cached.
116133 */
117- List <XmlTag > extractRoutesForMethod (@ NotNull Method method ) {
118- List <XmlTag > routesForMethod = WebApiTypeIndex .getWebApiRoutes (method );
119- PhpClass phpClass = method .getContainingClass ();
134+ public List <XmlTag > extractRoutesForMethod (final @ NotNull Method method ) {
135+ final List <XmlTag > routesForMethod = WebApiTypeIndex .getWebApiRoutes (method );
136+ final PhpClass phpClass = method .getContainingClass ();
137+
120138 if (phpClass == null ) {
121139 return routesForMethod ;
122140 }
123- for (PhpClass parent : method .getContainingClass ().getSupers ()) {
141+ for (final PhpClass parent : method .getContainingClass ().getSupers ()) {
124142 for (Method parentMethod : parent .getMethods ()) {
125143 if (parentMethod .getName ().equals (method .getName ())) {
126144 routesForMethod .addAll (extractRoutesForMethod (parentMethod ));
127145 }
128146 }
129147 }
148+
130149 return routesForMethod ;
131150 }
132151
133152 /**
134153 * Make sure that routes are sorted as follows: GET, PUT, POST, DELETE. Then by path.
135154 */
136- private void sortRoutes (List <XmlTag > routes ) {
155+ private void sortRoutes (final List <XmlTag > routes ) {
137156 routes .sort (
138- (firstTag , secondTag ) -> {
139- String substring = firstTag .getName ().substring (2 , 5 );
140- Integer firstSortOrder = HTTP_METHODS_SORT_ORDER .get (substring );
141- Integer secondSortOrder = HTTP_METHODS_SORT_ORDER .get (secondTag .getName ().substring (2 , 5 ));
142- if (firstSortOrder .compareTo (secondSortOrder ) == 0 ) {
143- // Sort by route if HTTP methods are equal
144- return firstTag .getName ().compareTo (secondTag .getName ());
157+ (firstTag , secondTag ) -> {
158+ String substring = firstTag .getName ().substring (2 , 5 );
159+ Integer firstSortOrder = HTTP_METHODS_SORT_ORDER .get (substring );
160+ Integer secondSortOrder = HTTP_METHODS_SORT_ORDER .get (
161+ secondTag .getName ().substring (2 , 5 )
162+ );
163+ if (firstSortOrder .compareTo (secondSortOrder ) == 0 ) {
164+ // Sort by route if HTTP methods are equal
165+ return firstTag .getName ().compareTo (secondTag .getName ());
166+ }
167+
168+ return firstSortOrder .compareTo (secondSortOrder );
145169 }
146- return firstSortOrder .compareTo (secondSortOrder );
147- }
148170 );
149171 }
150172 }
0 commit comments