forked from magento/magento2-phpstorm-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPluginDeclarationInspection.java
307 lines (267 loc) · 13.8 KB
/
PluginDeclarationInspection.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
/*
* 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.util.Pair;
import com.intellij.psi.PsiDirectory;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiElementVisitor;
import com.intellij.psi.PsiFile;
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.PluginIndex;
import com.magento.idea.magento2plugin.magento.files.ModuleDiXml;
import com.magento.idea.magento2plugin.magento.packages.Areas;
import com.magento.idea.magento2plugin.magento.packages.File;
import com.magento.idea.magento2plugin.magento.packages.Package;
import com.magento.idea.magento2plugin.util.magento.GetModuleNameByDirectoryUtil;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@SuppressWarnings({"PMD.ExcessiveMethodLength", "PMD.NPathComplexity"})
public class PluginDeclarationInspection extends PhpInspection {
@NotNull
@Override
public PsiElementVisitor buildVisitor(
final @NotNull ProblemsHolder problemsHolder,
final boolean isOnTheFly
) {
return new XmlElementVisitor() {
private InspectionBundle inspectionBundle = new InspectionBundle();
private final ProblemHighlightType errorSeverity = ProblemHighlightType.WARNING;
@Override
public void visitFile(final PsiFile file) {
if (!file.getName().equals(ModuleDiXml.FILE_NAME)) {
return;
}
final XmlTag[] xmlTags = getFileXmlTags(file);
if (xmlTags == null) {
return;
}
final HashMap<String, XmlTag> targetPluginHash = new HashMap<>();
final PluginIndex pluginIndex = PluginIndex.getInstance(file.getProject());
final HashMap<String, XmlTag> pluginProblems = new HashMap<>();
for (final XmlTag pluginXmlTag: xmlTags) {
pluginProblems.clear();
if (!pluginXmlTag.getName().equals(ModuleDiXml.TYPE_TAG)) {
continue;
}
final XmlAttribute pluginNameAttribute =
pluginXmlTag.getAttribute(ModuleDiXml.NAME_ATTR);
if (pluginNameAttribute == null) {
continue;
}
final String pluginNameAttributeValue = pluginNameAttribute.getValue();
if (pluginNameAttributeValue == null) {
continue;
}
final List<XmlTag> targetPlugin = fetchPluginTagsFromPluginTag(pluginXmlTag);
for (final XmlTag pluginTypeXmlTag: targetPlugin) {
final XmlAttribute pluginTypeNameAttribute
= pluginTypeXmlTag.getAttribute(ModuleDiXml.NAME_ATTR);
final XmlAttribute pluginTypeDisabledAttribute
= pluginTypeXmlTag.getAttribute(ModuleDiXml.DISABLED_ATTR_NAME);
if (pluginTypeNameAttribute == null) {
continue;
}
final String pluginTypeName = pluginTypeNameAttribute.getValue();
if (pluginTypeName == null) {
continue;
}
final String pluginTypeKey = pluginNameAttributeValue.concat(
Package.vendorModuleNameSeparator
).concat(pluginTypeName);
if (targetPluginHash.containsKey(pluginTypeKey)) {
problemsHolder.registerProblem(
pluginTypeNameAttribute.getValueElement(),
inspectionBundle.message(
"inspection.plugin.duplicateInSameFile"
),
errorSeverity
);
}
targetPluginHash.put(pluginTypeKey, pluginTypeXmlTag);
final List<Pair<String, String>> modulesWithSamePluginName =
fetchModuleNamesWhereSamePluginNameUsed(
pluginNameAttributeValue,
pluginTypeName,
pluginIndex,
file
);
final XmlAttribute pluginTypeAttribute =
pluginTypeXmlTag.getAttribute(ModuleDiXml.TYPE_ATTR);
if (pluginTypeDisabledAttribute != null
&& pluginTypeAttribute == null
&& pluginTypeDisabledAttribute.getValue() != null
&& pluginTypeDisabledAttribute.getValue().equals("true")
&& !pluginTypeName.isEmpty()
) {
@Nullable final XmlAttributeValue valueElement
= pluginTypeNameAttribute.getValueElement();
if (modulesWithSamePluginName.isEmpty() && valueElement != null) {
problemsHolder.registerProblem(
valueElement,
inspectionBundle.message(
"inspection.plugin.disabledPluginDoesNotExist"
),
errorSeverity
);
} else {
continue;
}
}
for (final Pair<String, String> moduleEntry: modulesWithSamePluginName) {
final String scope = moduleEntry.getFirst();
final String moduleName = moduleEntry.getSecond();
if (scope == null || moduleName == null) {
continue;
}
final String problemKey = pluginTypeKey.concat(
Package.vendorModuleNameSeparator
).concat(moduleName)
.concat(Package.vendorModuleNameSeparator).concat(scope);
if (!pluginProblems.containsKey(problemKey)) {
problemsHolder.registerProblem(
pluginTypeNameAttribute.getValueElement(),
inspectionBundle.message(
"inspection.plugin.duplicateInOtherPlaces",
pluginTypeName,
pluginNameAttributeValue,
moduleName,
scope
),
errorSeverity
);
pluginProblems.put(problemKey, pluginTypeXmlTag);
}
}
}
}
}
private List<Pair<String, String>> fetchModuleNamesWhereSamePluginNameUsed(
final String pluginNameAttributeValue,
final String pluginTypeName,
final PluginIndex pluginIndex,
final PsiFile file
) {
final List<Pair<String, String>> modulesName = new ArrayList<>();
final PsiDirectory parentDirectory = file.getContainingDirectory();
if (parentDirectory == null) {
return modulesName;
}
final String currentFileDirectory = file.getContainingDirectory().toString();
final String currentFileFullPath =
currentFileDirectory.concat(File.separator).concat(file.getName());
final Collection<PsiElement> indexedPlugins =
pluginIndex.getPluginElements(
pluginNameAttributeValue,
GlobalSearchScope.getScopeRestrictedByFileTypes(
GlobalSearchScope.allScope(file.getProject()),
XmlFileType.INSTANCE
));
for (final PsiElement indexedPlugin: indexedPlugins) {
final PsiFile indexedAttributeParent =
PsiTreeUtil.getTopmostParentOfType(indexedPlugin, PsiFile.class);
if (indexedAttributeParent == null) {
continue;
}
final String indexedPluginAttributeValue =
((XmlAttributeValue) indexedPlugin).getValue();
if (!indexedPluginAttributeValue.equals(pluginNameAttributeValue)) {
continue;
}
final String indexedFileDirectory =
indexedAttributeParent.getContainingDirectory().toString();
final String indexedFileFullPath =
indexedFileDirectory.concat(File.separator)
.concat(indexedAttributeParent.getName());
if (indexedFileFullPath.equals(currentFileFullPath)) {
continue;
}
final String scope = getAreaFromFileDirectory(indexedAttributeParent);
final List<XmlTag> indexPluginTags =
fetchPluginTagsFromPluginTag((XmlTag) indexedPlugin
.getParent().getParent());
for (final XmlTag indexPluginTag: indexPluginTags) {
final XmlAttribute indexedPluginNameAttribute =
indexPluginTag.getAttribute(ModuleDiXml.NAME_ATTR);
if (indexedPluginNameAttribute == null) {
continue;
}
if (!pluginTypeName.equals(indexedPluginNameAttribute.getValue())) {
continue;
}
addModuleNameWhereSamePluginUsed(
modulesName,
indexedAttributeParent,
scope
);
}
}
return modulesName;
}
private List<XmlTag> fetchPluginTagsFromPluginTag(final XmlTag pluginXmlTag) {
final List<XmlTag> result = new ArrayList<>();
final XmlTag[] pluginTypeXmlTags =
PsiTreeUtil.getChildrenOfType(pluginXmlTag, XmlTag.class);
if (pluginTypeXmlTags == null) {
return result;
}
for (final XmlTag pluginTypeXmlTag: pluginTypeXmlTags) {
if (!pluginTypeXmlTag.getName().equals(ModuleDiXml.PLUGIN_TAG_NAME)) {
continue;
}
result.add(pluginTypeXmlTag);
}
return result;
}
private void addModuleNameWhereSamePluginUsed(
final List<Pair<String, String>> modulesName,
final PsiFile indexedFile,
final String scope
) {
final String moduleName = GetModuleNameByDirectoryUtil
.execute(
indexedFile.getContainingDirectory(),
problemsHolder.getProject()
);
modulesName.add(Pair.create(scope, moduleName));
}
@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 "";
}
};
}
}