forked from magento/magento2-phpstorm-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathObserverDeclarationInspection.java
384 lines (337 loc) · 17 KB
/
ObserverDeclarationInspection.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
/*
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
package com.magento.idea.magento2plugin.inspections.xml;
import com.intellij.codeInspection.ProblemHighlightType;
import com.intellij.codeInspection.ProblemsHolder;
import com.intellij.ide.highlighter.XmlFileType;
import com.intellij.openapi.externalSystem.service.execution.NotSupportedException;
import com.intellij.openapi.vfs.VfsUtil;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiElementVisitor;
import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiManager;
import com.intellij.psi.XmlElementVisitor;
import com.intellij.psi.search.GlobalSearchScope;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.psi.xml.XmlAttribute;
import com.intellij.psi.xml.XmlAttributeValue;
import com.intellij.psi.xml.XmlDocument;
import com.intellij.psi.xml.XmlTag;
import com.jetbrains.php.lang.inspections.PhpInspection;
import com.magento.idea.magento2plugin.bundles.InspectionBundle;
import com.magento.idea.magento2plugin.indexes.EventIndex;
import com.magento.idea.magento2plugin.magento.files.ModuleEventsXml;
import com.magento.idea.magento2plugin.magento.files.ModuleXml;
import com.magento.idea.magento2plugin.magento.files.Observer;
import com.magento.idea.magento2plugin.magento.packages.Areas;
import com.magento.idea.magento2plugin.magento.packages.Package;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@SuppressWarnings({
"PMD.ExcessiveMethodLength",
"PMD.NPathComplexity",
"PMD.ExcessiveImports",
})
public class ObserverDeclarationInspection extends PhpInspection {
@NotNull
@Override
@SuppressWarnings({"PMD.AvoidInstantiatingObjectsInLoops", "PMD.CognitiveComplexity"})
public PsiElementVisitor buildVisitor(
final @NotNull ProblemsHolder problemsHolder,
final boolean isOnTheFly
) {
return new XmlElementVisitor() {
private final String moduleXmlFileName = ModuleXml.getInstance().getFileName();
private final HashMap<String, VirtualFile> loadedFileHash = new HashMap<>();//NOPMD
private final InspectionBundle inspectionBundle = new InspectionBundle();
private final ProblemHighlightType errorSeverity = ProblemHighlightType.WARNING;
@Override
public void visitFile(final @NotNull PsiFile file) {
if (!ModuleEventsXml.FILE_NAME.equals(file.getName())) {
return;
}
final XmlTag[] xmlTags = getFileXmlTags(file);
if (xmlTags == null) {
return;
}
// This added to cover case if file exists only in memory.
if (file.getContainingDirectory() == null) {
return;
}
final EventIndex eventIndex = new EventIndex(file.getProject());
final HashMap<String, XmlTag> targetObserversHash = new HashMap<>();
for (final XmlTag eventXmlTag: xmlTags) {
final HashMap<String, XmlTag> eventProblems = new HashMap<>();
if (!ModuleEventsXml.EVENT_TAG.equals(eventXmlTag.getName())) {
continue;
}
final XmlAttribute eventNameAttribute =
eventXmlTag.getAttribute(Observer.NAME_ATTRIBUTE);
if (eventNameAttribute == null) {
continue;
}
final String eventNameAttributeValue = eventNameAttribute.getValue();
if (eventNameAttributeValue == null) {
continue;
}
final List<XmlTag> targetObservers = fetchObserverTagsFromEventTag(eventXmlTag);
if (targetObservers.isEmpty()) {
continue;
}
for (final XmlTag observerXmlTag: targetObservers) {
final XmlAttribute observerNameAttribute =
observerXmlTag.getAttribute(Observer.NAME_ATTRIBUTE);
final XmlAttribute observerDisabledAttribute =
observerXmlTag.getAttribute("disabled");
if (observerNameAttribute == null) {
continue;
}
final String observerName = observerNameAttribute.getValue();
if (observerName == null
|| observerNameAttribute.getValueElement() == null) {
continue;
}
final String observerKey = eventNameAttributeValue.concat("_")
.concat(observerName);
if (targetObserversHash.containsKey(observerKey)) {
problemsHolder.registerProblem(
observerNameAttribute.getValueElement(),
inspectionBundle.message(
"inspection.observer.duplicateInSameFile"
),
errorSeverity
);
}
targetObserversHash.put(observerKey, observerXmlTag);
final List<HashMap<String, String>> modulesWithSameObserverName =
fetchModuleNamesWhereSameObserverNameUsed(
eventNameAttributeValue,
observerName,
eventIndex,
file
);
if (observerDisabledAttribute != null
&& observerDisabledAttribute.getValue() != null
&& observerDisabledAttribute.getValue().equals("true")
&& !observerName.isEmpty()
) {
@Nullable final XmlAttributeValue valueElement
= observerNameAttribute.getValueElement();
if (modulesWithSameObserverName.isEmpty() && valueElement != null) {
problemsHolder.registerProblem(
valueElement,
inspectionBundle.message(
"inspection.observer.disabledObserverDoesNotExist"
),
errorSeverity
);
} else {
continue;
}
}
for (final HashMap<String, String> moduleEntry:
modulesWithSameObserverName) {
final Map.Entry<String, String> module = moduleEntry
.entrySet().iterator().next();
final String moduleName = module.getKey();
final String scope = module.getValue();
final String problemKey = observerKey.concat("_")
.concat(moduleName)
.concat("_")
.concat(scope);
if (!eventProblems.containsKey(problemKey)) {
problemsHolder.registerProblem(
observerNameAttribute.getValueElement(),
inspectionBundle.message(
"inspection.observer.duplicateInOtherPlaces",
observerName,
eventNameAttributeValue,
moduleName,
scope
),
errorSeverity
);
eventProblems.put(problemKey, observerXmlTag);
}
}
}
}
}
private List<HashMap<String, String>> fetchModuleNamesWhereSameObserverNameUsed(
final String eventNameAttributeValue,
final String observerName,
final EventIndex eventIndex,
final PsiFile file
) {
if (file.getContainingDirectory() == null) {
throw new NotSupportedException(
"Operation is not supported for files in memory"
);
}
final List<HashMap<String, String>> modulesName = new ArrayList<>();
final String currentFileDirectory = file.getContainingDirectory().toString();
final String currentFileFullPath = currentFileDirectory
.concat("/").concat(file.getName());
final Collection<PsiElement> indexedEvents = eventIndex.getEventElements(
eventNameAttributeValue,
GlobalSearchScope.getScopeRestrictedByFileTypes(
GlobalSearchScope.allScope(file.getProject()),
XmlFileType.INSTANCE
));
for (final PsiElement indexedEvent: indexedEvents) {
final PsiFile indexedAttributeParent =
PsiTreeUtil.getTopmostParentOfType(indexedEvent, PsiFile.class);
if (indexedAttributeParent == null) {
continue;
}
final String indexedEventAttributeValue =
((XmlAttributeValue) indexedEvent).getValue();
if (!indexedEventAttributeValue.equals(eventNameAttributeValue)) {
continue;
}
final String indexedFileDirectory = indexedAttributeParent
.getContainingDirectory().toString();
final String indexedFileFullPath = indexedFileDirectory.concat("/")
.concat(indexedAttributeParent.getName());
if (indexedFileFullPath.equals(currentFileFullPath)) {
continue;
}
final String scope = getAreaFromFileDirectory(indexedAttributeParent);
final List<XmlTag> indexObserversTags =
fetchObserverTagsFromEventTag((XmlTag) indexedEvent
.getParent().getParent());
for (final XmlTag indexObserversTag: indexObserversTags) {
final XmlAttribute indexedObserverNameAttribute =
indexObserversTag.getAttribute("name");
if (indexedObserverNameAttribute == null) {
continue;
}
if (!observerName.equals(indexedObserverNameAttribute.getValue())) {
continue;
}
addModuleNameWhereSameObserverUsed(
modulesName,
indexedAttributeParent,
scope
);
}
}
return modulesName;
}
private List<XmlTag> fetchObserverTagsFromEventTag(final XmlTag eventXmlTag) {
final List<XmlTag> result = new ArrayList<>();
final XmlTag[] observerXmlTags =
PsiTreeUtil.getChildrenOfType(eventXmlTag, XmlTag.class);
if (observerXmlTags == null) {
return result;
}
for (final XmlTag observerXmlTag: observerXmlTags) {
if (!ModuleEventsXml.OBSERVER_TAG.equals(observerXmlTag.getName())) {
continue;
}
result.add(observerXmlTag);
}
return result;
}
private void addModuleNameWhereSameObserverUsed(
final List<HashMap<String, String>> modulesName,
final PsiFile indexedFile,
final String scope
) {
final XmlTag moduleDeclarationTag =
getModuleDeclarationTagByConfigFile(indexedFile);
if (moduleDeclarationTag == null) {
return;
}
if (!ModuleEventsXml.MODULE_TAG.equals(moduleDeclarationTag.getName())) {
return;
}
final XmlAttribute moduleNameAttribute
= moduleDeclarationTag.getAttribute(ModuleEventsXml.NAME_ATTRIBUTE);
if (moduleNameAttribute == null) {
return;
}
final HashMap<String, String> moduleEntry = new HashMap<>();
moduleEntry.put(moduleNameAttribute.getValue(), scope);
modulesName.add(moduleEntry);
}
@Nullable
private XmlTag getModuleDeclarationTagByConfigFile(final PsiFile file) {
final String fileDirectory = file.getContainingDirectory().toString();
final String fileArea = file.getContainingDirectory().getName();
final String moduleXmlFilePath =
getModuleXmlFilePathByConfigFileDirectory(fileDirectory, fileArea);
final VirtualFile virtualFile = getFileByPath(moduleXmlFilePath);
if (virtualFile == null) {
return null;
}
final PsiFile moduleDeclarationFile =
PsiManager.getInstance(file.getProject()).findFile(virtualFile);
final XmlTag[] moduleDeclarationTags = getFileXmlTags(moduleDeclarationFile);
if (moduleDeclarationTags == null) {
return null;
}
return moduleDeclarationTags[0];
}
@Nullable
private VirtualFile getFileByPath(final String moduleXmlFilePath) {
if (loadedFileHash.containsKey(moduleXmlFilePath)) {
return loadedFileHash.get(moduleXmlFilePath);
}
VirtualFile virtualFile;
try {
virtualFile = VfsUtil.findFileByURL(new URL(moduleXmlFilePath));
} catch (MalformedURLException e) {
return null;
}
if (virtualFile == null) {
return null;
}
loadedFileHash.put(moduleXmlFilePath, virtualFile);
return virtualFile;
}
private String getModuleXmlFilePathByConfigFileDirectory(
final String fileDirectory,
final String fileArea
) {
String moduleXmlFile = fileDirectory.replace(fileArea, "")
.concat(moduleXmlFileName);
if (fileDirectory.endsWith("etc")) {
moduleXmlFile = fileDirectory.concat("/").concat(moduleXmlFileName);
}
return moduleXmlFile.replace("PsiDirectory:", "file:");
}
@Nullable
private XmlTag[] getFileXmlTags(final PsiFile file) {
final XmlDocument xmlDocument = PsiTreeUtil.getChildOfType(file, XmlDocument.class);
final XmlTag xmlRootTag = PsiTreeUtil.getChildOfType(xmlDocument, XmlTag.class);
return PsiTreeUtil.getChildrenOfType(xmlRootTag, XmlTag.class);
}
private String getAreaFromFileDirectory(final @NotNull PsiFile file) {
if (file.getParent() == null) {
return "";
}
final String areaFromFileDirectory = file.getParent().getName();
if (areaFromFileDirectory.equals(Package.moduleBaseAreaDir)) {
return Areas.base.toString();
}
for (final Areas area: Areas.values()) {
if (area.toString().equals(areaFromFileDirectory)) {
return area.toString();
}
}
return "";
}
};
}
}