19
19
import com .intellij .psi .xml .XmlTag ;
20
20
import com .jetbrains .php .lang .inspections .PhpInspection ;
21
21
import com .magento .idea .magento2plugin .indexes .PluginIndex ;
22
+ import com .magento .idea .magento2plugin .magento .files .ModuleDiXml ;
22
23
import com .magento .idea .magento2plugin .magento .files .ModuleXml ;
23
24
import com .magento .idea .magento2plugin .magento .packages .Package ;
24
25
import org .jetbrains .annotations .NotNull ;
25
26
import org .jetbrains .annotations .Nullable ;
26
27
28
+ import java .io .File ;
27
29
import java .net .MalformedURLException ;
28
30
import java .net .URL ;
29
31
import java .util .*;
@@ -35,16 +37,15 @@ public class PluginDeclarationInspection extends PhpInspection {
35
37
public PsiElementVisitor buildVisitor (@ NotNull ProblemsHolder problemsHolder , boolean b ) {
36
38
return new XmlElementVisitor () {
37
39
private final String moduleXmlFileName = ModuleXml .getInstance ().getFileName ();
38
- private static final String pluginsXmlFileName = "di.xml" ;
39
- private static final String duplicatedObserverNameSameFileProblemDescription = "The plugin name already used in this file. For more details see Inspection Description." ;
40
- private static final String duplicatedObserverNameProblemDescription =
40
+ private static final String duplicatedPluginNameSameFileProblemDescription = "The plugin name already used in this file. For more details see Inspection Description." ;
41
+ private static final String duplicatedPluginNameProblemDescription =
41
42
"The plugin name \" %s\" for targeted \" %s\" class is already used in the module \" %s\" (%s scope). For more details see Inspection Description." ;
42
43
private HashMap <String , VirtualFile > loadedFileHash = new HashMap <>();
43
44
private final ProblemHighlightType errorSeverity = ProblemHighlightType .WARNING ;
44
45
45
46
@ Override
46
47
public void visitFile (PsiFile file ) {
47
- if (!file .getName ().equals (pluginsXmlFileName )) {
48
+ if (!file .getName ().equals (ModuleDiXml . FILE_NAME )) {
48
49
return ;
49
50
}
50
51
@@ -55,44 +56,44 @@ public void visitFile(PsiFile file) {
55
56
return ;
56
57
}
57
58
58
- HashMap <String , XmlTag > targetObserversHash = new HashMap <>();
59
+ HashMap <String , XmlTag > targetPluginHash = new HashMap <>();
59
60
60
61
for (XmlTag pluginXmlTag : xmlTags ) {
61
62
HashMap <String , XmlTag > pluginProblems = new HashMap <>();
62
- if (!pluginXmlTag .getName ().equals ("type" )) {
63
+ if (!pluginXmlTag .getName ().equals (ModuleDiXml . PLUGIN_TYPE_TAG )) {
63
64
continue ;
64
65
}
65
66
66
- XmlAttribute pluginNameAttribute = pluginXmlTag .getAttribute ("name" );
67
+ XmlAttribute pluginNameAttribute = pluginXmlTag .getAttribute (ModuleDiXml . PLUGIN_TYPE_ATTR_NAME );
67
68
68
69
String pluginNameAttributeValue = pluginNameAttribute .getValue ();
69
70
if (pluginNameAttributeValue == null ) {
70
71
continue ;
71
72
}
72
73
73
- List <XmlTag > targetObservers = fetchObserverTagsFromPluginTag (pluginXmlTag );
74
+ List <XmlTag > targetPlugin = fetchPluginTagsFromPluginTag (pluginXmlTag );
74
75
75
- for (XmlTag pluginTypeXmlTag : targetObservers ) {
76
- XmlAttribute pluginTypeNameAttribute = pluginTypeXmlTag .getAttribute ("name" );
77
- XmlAttribute pluginTypeDisabledAttribute = pluginTypeXmlTag .getAttribute ("disabled" );
76
+ for (XmlTag pluginTypeXmlTag : targetPlugin ) {
77
+ XmlAttribute pluginTypeNameAttribute = pluginTypeXmlTag .getAttribute (ModuleDiXml . PLUGIN_TYPE_ATTR_NAME );
78
+ XmlAttribute pluginTypeDisabledAttribute = pluginTypeXmlTag .getAttribute (ModuleDiXml . DISABLED_ATTR_NAME );
78
79
79
80
if (pluginTypeNameAttribute == null || (pluginTypeDisabledAttribute != null && pluginTypeDisabledAttribute .getValue ().equals ("true" ))) {
80
81
continue ;
81
82
}
82
83
83
84
String pluginTypeName = pluginTypeNameAttribute .getValue ();
84
85
String pluginTypeKey = pluginNameAttributeValue .concat ("_" ).concat (pluginTypeName );
85
- if (targetObserversHash .containsKey (pluginTypeKey )) {
86
+ if (targetPluginHash .containsKey (pluginTypeKey )) {
86
87
problemsHolder .registerProblem (
87
88
pluginTypeNameAttribute .getValueElement (),
88
- duplicatedObserverNameSameFileProblemDescription ,
89
+ duplicatedPluginNameSameFileProblemDescription ,
89
90
errorSeverity
90
91
);
91
92
}
92
- targetObserversHash .put (pluginTypeKey , pluginTypeXmlTag );
93
+ targetPluginHash .put (pluginTypeKey , pluginTypeXmlTag );
93
94
94
- List <HashMap <String , String >> modulesWithSameObserverName = fetchModuleNamesWhereSamePluginNameUsed (pluginNameAttributeValue , pluginTypeName , pluginIndex , file );
95
- for (HashMap <String , String > moduleEntry : modulesWithSameObserverName ) {
95
+ List <HashMap <String , String >> modulesWithSamePluginName = fetchModuleNamesWhereSamePluginNameUsed (pluginNameAttributeValue , pluginTypeName , pluginIndex , file );
96
+ for (HashMap <String , String > moduleEntry : modulesWithSamePluginName ) {
96
97
Map .Entry <String , String > module = moduleEntry .entrySet ().iterator ().next ();
97
98
String moduleName = module .getKey ();
98
99
String scope = module .getValue ();
@@ -101,7 +102,7 @@ public void visitFile(PsiFile file) {
101
102
problemsHolder .registerProblem (
102
103
pluginTypeNameAttribute .getValueElement (),
103
104
String .format (
104
- duplicatedObserverNameProblemDescription ,
105
+ duplicatedPluginNameProblemDescription ,
105
106
pluginTypeName ,
106
107
pluginNameAttributeValue ,
107
108
moduleName ,
@@ -119,7 +120,7 @@ public void visitFile(PsiFile file) {
119
120
private List <HashMap <String , String >> fetchModuleNamesWhereSamePluginNameUsed (String pluginNameAttributeValue , String pluginTypeName , PluginIndex pluginIndex , PsiFile file ) {
120
121
List <HashMap <String , String >> modulesName = new ArrayList <>();
121
122
String currentFileDirectory = file .getContainingDirectory ().toString ();
122
- String currentFileFullPath = currentFileDirectory .concat ("/" ).concat (file .getName ());
123
+ String currentFileFullPath = currentFileDirectory .concat (File . separator ).concat (file .getName ());
123
124
124
125
Collection <PsiElement > indexedPlugins = pluginIndex .getPluginElements (pluginNameAttributeValue , GlobalSearchScope .getScopeRestrictedByFileTypes (
125
126
GlobalSearchScope .allScope (file .getProject ()),
@@ -138,20 +139,20 @@ private List<HashMap<String, String>> fetchModuleNamesWhereSamePluginNameUsed(St
138
139
}
139
140
140
141
String indexedFileDirectory = indexedAttributeParent .getContainingDirectory ().toString ();
141
- String indexedFileFullPath = indexedFileDirectory .concat ("/" ).concat (indexedAttributeParent .getName ());
142
+ String indexedFileFullPath = indexedFileDirectory .concat (File . separator ).concat (indexedAttributeParent .getName ());
142
143
if (indexedFileFullPath .equals (currentFileFullPath )) {
143
144
continue ;
144
145
}
145
146
146
147
String scope = getAreaFromFileDirectory (indexedAttributeParent );
147
148
148
- List <XmlTag > indexObserversTags = fetchObserverTagsFromPluginTag ((XmlTag ) indexedPlugin .getParent ().getParent ());
149
- for (XmlTag indexObserversTag : indexObserversTags ) {
150
- XmlAttribute indexedObserverNameAttribute = indexObserversTag .getAttribute ("name" );
151
- if (indexedObserverNameAttribute == null ) {
149
+ List <XmlTag > indexPluginTags = fetchPluginTagsFromPluginTag ((XmlTag ) indexedPlugin .getParent ().getParent ());
150
+ for (XmlTag indexPluginTag : indexPluginTags ) {
151
+ XmlAttribute indexedPluginNameAttribute = indexPluginTag .getAttribute (ModuleDiXml . PLUGIN_TYPE_ATTR_NAME );
152
+ if (indexedPluginNameAttribute == null ) {
152
153
continue ;
153
154
}
154
- if (!pluginTypeName .equals (indexedObserverNameAttribute .getValue ())){
155
+ if (!pluginTypeName .equals (indexedPluginNameAttribute .getValue ())){
155
156
continue ;
156
157
}
157
158
addModuleNameWhereSamePluginUsed (modulesName , indexedAttributeParent , scope );
@@ -161,15 +162,15 @@ private List<HashMap<String, String>> fetchModuleNamesWhereSamePluginNameUsed(St
161
162
return modulesName ;
162
163
}
163
164
164
- private List <XmlTag > fetchObserverTagsFromPluginTag (XmlTag pluginXmlTag ) {
165
+ private List <XmlTag > fetchPluginTagsFromPluginTag (XmlTag pluginXmlTag ) {
165
166
List <XmlTag > result = new ArrayList <>();
166
167
XmlTag [] pluginTypeXmlTags = PsiTreeUtil .getChildrenOfType (pluginXmlTag , XmlTag .class );
167
168
if (pluginTypeXmlTags == null ) {
168
169
return result ;
169
170
}
170
171
171
172
for (XmlTag pluginTypeXmlTag : pluginTypeXmlTags ) {
172
- if (!pluginTypeXmlTag .getName ().equals ("plugin" )) {
173
+ if (!pluginTypeXmlTag .getName ().equals (ModuleDiXml . PLUGIN_TAG_NAME )) {
173
174
continue ;
174
175
}
175
176
@@ -186,7 +187,7 @@ private void addModuleNameWhereSamePluginUsed(List<HashMap<String, String>> modu
186
187
if (!moduleDeclarationTag .getName ().equals ("module" )) {
187
188
return ;
188
189
}
189
- XmlAttribute moduleNameAttribute = moduleDeclarationTag .getAttribute ("name" );
190
+ XmlAttribute moduleNameAttribute = moduleDeclarationTag .getAttribute (ModuleXml . MODULE_ATTR_NAME );
190
191
if (moduleNameAttribute == null ) {
191
192
return ;
192
193
}
@@ -234,8 +235,8 @@ private VirtualFile getFileByPath(String moduleXmlFilePath) {
234
235
235
236
private String getModuleXmlFilePathByConfigFileDirectory (String fileDirectory , String fileArea ) {
236
237
String moduleXmlFile = fileDirectory .replace (fileArea , "" ).concat (moduleXmlFileName );
237
- if (fileDirectory .endsWith ("etc" )) {
238
- moduleXmlFile = fileDirectory .concat ("/" ).concat (moduleXmlFileName );
238
+ if (fileDirectory .endsWith (Package . MODULE_BASE_AREA_DIR )) {
239
+ moduleXmlFile = fileDirectory .concat (File . separator ).concat (moduleXmlFileName );
239
240
}
240
241
return moduleXmlFile .replace ("PsiDirectory:" , "file:" );
241
242
}
0 commit comments