Skip to content

Commit 1d17fa7

Browse files
Bugfix-765: Code refactoring
1 parent 7d69766 commit 1d17fa7

File tree

1 file changed

+56
-39
lines changed

1 file changed

+56
-39
lines changed

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

Lines changed: 56 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,17 @@
2121
import java.util.Collection;
2222
import java.util.HashMap;
2323
import java.util.List;
24+
import java.util.Map;
2425
import org.jetbrains.annotations.NotNull;
2526
import org.jetbrains.annotations.Nullable;
2627

2728
public class PluginTargetLineMarkerProvider implements LineMarkerProvider {
28-
@Nullable
29+
30+
private static final String TARGET_CLASS_TOOLTIP_TEXT = "Navigate to target class";
31+
private static final String TARGET_METHOD_TOOLTIP_TEXT = "Navigate to target method";
32+
2933
@Override
30-
public LineMarkerInfo getLineMarkerInfo(@NotNull final PsiElement psiElement) {
34+
public @Nullable LineMarkerInfo<?> getLineMarkerInfo(final @NotNull PsiElement psiElement) {
3135
return null;
3236
}
3337

@@ -36,48 +40,52 @@ public void collectSlowLineMarkers(
3640
final @NotNull List<? extends PsiElement> psiElements,
3741
final @NotNull Collection<? super LineMarkerInfo<?>> collection
3842
) {
39-
if (!psiElements.isEmpty() && !Settings.isEnabled(psiElements.get(0).getProject())) {
43+
if (psiElements.isEmpty()) {
44+
return;
45+
}
46+
47+
if (!Settings.isEnabled(psiElements.get(0).getProject())) {
4048
return;
4149
}
4250
final PluginClassCache pluginClassCache = new PluginClassCache();
43-
final TargetClassesCollector targetClassesCollector =
44-
new TargetClassesCollector(pluginClassCache);
45-
final TargetMethodsCollector targetMethodsCollector =
46-
new TargetMethodsCollector(pluginClassCache);
51+
final TargetClassesCollector targetClassesCollector = new TargetClassesCollector(
52+
pluginClassCache
53+
);
54+
final TargetMethodsCollector targetMethodsCollector = new TargetMethodsCollector(
55+
pluginClassCache
56+
);
4757

4858
for (final PsiElement psiElement : psiElements) {
49-
if (psiElement instanceof PhpClass || psiElement instanceof Method) {
50-
List<? extends PsiElement> results;
51-
52-
if (psiElement instanceof PhpClass) {
53-
results = targetClassesCollector.collect((PhpClass) psiElement);
54-
if (!results.isEmpty()) {
55-
collection.add(NavigationGutterIconBuilder
56-
.create(AllIcons.Nodes.Class)
57-
.setTargets(results)
58-
.setTooltipText("Navigate to target class")
59-
.createLineMarkerInfo(PsiTreeUtil.getDeepestFirst(psiElement))
60-
);
61-
}
62-
} else {
63-
results = targetMethodsCollector.collect((Method) psiElement);
64-
if (!results.isEmpty()) {
65-
collection.add(NavigationGutterIconBuilder
66-
.create(AllIcons.Nodes.Method)
67-
.setTargets(results)
68-
.setTooltipText("Navigate to target method")
69-
.createLineMarkerInfo(PsiTreeUtil.getDeepestFirst(psiElement))
70-
);
71-
}
59+
if (psiElement instanceof PhpClass) {
60+
final List<? extends PsiElement> results =
61+
targetClassesCollector.collect((PhpClass) psiElement);
62+
63+
if (!results.isEmpty()) {
64+
collection.add(NavigationGutterIconBuilder
65+
.create(AllIcons.Nodes.Class)
66+
.setTargets(results)
67+
.setTooltipText(TARGET_CLASS_TOOLTIP_TEXT)
68+
.createLineMarkerInfo(PsiTreeUtil.getDeepestFirst(psiElement))
69+
);
70+
}
71+
} else if (psiElement instanceof Method) {
72+
final List<? extends PsiElement> results =
73+
targetMethodsCollector.collect((Method) psiElement);
74+
75+
if (!results.isEmpty()) {
76+
collection.add(NavigationGutterIconBuilder
77+
.create(AllIcons.Nodes.Method)
78+
.setTargets(results)
79+
.setTooltipText(TARGET_METHOD_TOOLTIP_TEXT)
80+
.createLineMarkerInfo(PsiTreeUtil.getDeepestFirst(psiElement))
81+
);
7282
}
73-
7483
}
7584
}
7685
}
7786

7887
private static class PluginClassCache {
79-
private final HashMap<String, List<PhpClass>> pluginClassesMap = // NOPMD
80-
new HashMap<>();
88+
private final Map<String, List<PhpClass>> pluginClassesMap = new HashMap<>();
8189

8290
private List<PhpClass> getTargetClassesForPlugin(
8391
@NotNull final PhpClass phpClass,
@@ -127,9 +135,10 @@ protected List<PhpClass> getTargetClassesForPlugin(@NotNull final PhpClass phpCl
127135
}
128136

129137
private static class TargetClassesCollector implements Collector<PhpClass, PhpClass> {
138+
130139
private final PluginTargetLineMarkerProvider.PluginClassCache pluginClassCache;
131140

132-
TargetClassesCollector(// NOPMD
141+
public TargetClassesCollector(
133142
final PluginTargetLineMarkerProvider.PluginClassCache pluginClassCache
134143
) {
135144
this.pluginClassCache = pluginClassCache;
@@ -142,9 +151,10 @@ public List<PhpClass> collect(@NotNull final PhpClass psiElement) {
142151
}
143152

144153
private static class TargetMethodsCollector implements Collector<Method, Method> {
154+
145155
private final PluginTargetLineMarkerProvider.PluginClassCache pluginClassCache;
146156

147-
TargetMethodsCollector(// NOPMD
157+
public TargetMethodsCollector(
148158
final PluginTargetLineMarkerProvider.PluginClassCache pluginClassCache
149159
) {
150160
this.pluginClassCache = pluginClassCache;
@@ -158,32 +168,33 @@ public List<Method> collect(@NotNull final Method pluginMethod) {
158168
if (null == getPluginPrefix(pluginMethod)) {
159169
return results;
160170
}
161-
162171
final PhpClass pluginClass = pluginMethod.getContainingClass();
172+
163173
if (pluginClass == null) {
164174
return results;
165175
}
166176

167177
final List<PhpClass> targetClasses
168178
= pluginClassCache.getTargetClassesForPlugin(pluginClass);
179+
169180
if (targetClasses.isEmpty()) {
170181
return results;
171182
}
172183

173-
for (final PhpClass targetClass: targetClasses) {
184+
for (final PhpClass targetClass : targetClasses) {
174185
final String pluginPrefix = getPluginPrefix(pluginMethod);
175186
final String targetClassMethodName = getTargetMethodName(
176187
pluginMethod, pluginPrefix
177188
);
189+
178190
if (targetClassMethodName == null) {
179191
continue;
180192
}
181-
182193
final Method targetMethod = targetClass.findMethodByName(targetClassMethodName);
194+
183195
if (targetMethod == null) {
184196
continue;
185197
}
186-
187198
results.add(targetMethod);
188199
}
189200

@@ -192,25 +203,31 @@ public List<Method> collect(@NotNull final Method pluginMethod) {
192203

193204
private String getTargetMethodName(final Method pluginMethod, final String pluginPrefix) {
194205
final String targetClassMethodName = pluginMethod.getName().replace(pluginPrefix, "");
206+
195207
if (targetClassMethodName.isEmpty()) {
196208
return null;
197209
}
198210
final char firstCharOfTargetName = targetClassMethodName.charAt(0);
211+
199212
if (Character.getType(firstCharOfTargetName) == Character.LOWERCASE_LETTER) {
200213
return null;
201214
}
215+
202216
return Character.toLowerCase(firstCharOfTargetName)
203217
+ targetClassMethodName.substring(1);
204218
}
205219

206220
private String getPluginPrefix(final Method pluginMethod) {
207221
final String pluginMethodName = pluginMethod.getName();
222+
208223
if (pluginMethodName.startsWith(Plugin.PluginType.around.toString())) {
209224
return Plugin.PluginType.around.toString();
210225
}
226+
211227
if (pluginMethodName.startsWith(Plugin.PluginType.before.toString())) {
212228
return Plugin.PluginType.before.toString();
213229
}
230+
214231
if (pluginMethodName.startsWith(Plugin.PluginType.after.toString())) {
215232
return Plugin.PluginType.after.toString();
216233
}

0 commit comments

Comments
 (0)