1919import com .intellij .psi .xml .XmlTag ;
2020import com .jetbrains .php .lang .inspections .PhpInspection ;
2121import com .magento .idea .magento2plugin .indexes .PluginIndex ;
22+ import com .magento .idea .magento2plugin .magento .files .ModuleDiXml ;
2223import com .magento .idea .magento2plugin .magento .files .ModuleXml ;
2324import com .magento .idea .magento2plugin .magento .packages .Package ;
2425import org .jetbrains .annotations .NotNull ;
2526import org .jetbrains .annotations .Nullable ;
2627
28+ import java .io .File ;
2729import java .net .MalformedURLException ;
2830import java .net .URL ;
2931import java .util .*;
@@ -35,16 +37,15 @@ public class PluginDeclarationInspection extends PhpInspection {
3537 public PsiElementVisitor buildVisitor (@ NotNull ProblemsHolder problemsHolder , boolean b ) {
3638 return new XmlElementVisitor () {
3739 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 =
4142 "The plugin name \" %s\" for targeted \" %s\" class is already used in the module \" %s\" (%s scope). For more details see Inspection Description." ;
4243 private HashMap <String , VirtualFile > loadedFileHash = new HashMap <>();
4344 private final ProblemHighlightType errorSeverity = ProblemHighlightType .WARNING ;
4445
4546 @ Override
4647 public void visitFile (PsiFile file ) {
47- if (!file .getName ().equals (pluginsXmlFileName )) {
48+ if (!file .getName ().equals (ModuleDiXml . FILE_NAME )) {
4849 return ;
4950 }
5051
@@ -55,44 +56,44 @@ public void visitFile(PsiFile file) {
5556 return ;
5657 }
5758
58- HashMap <String , XmlTag > targetObserversHash = new HashMap <>();
59+ HashMap <String , XmlTag > targetPluginHash = new HashMap <>();
5960
6061 for (XmlTag pluginXmlTag : xmlTags ) {
6162 HashMap <String , XmlTag > pluginProblems = new HashMap <>();
62- if (!pluginXmlTag .getName ().equals ("type" )) {
63+ if (!pluginXmlTag .getName ().equals (ModuleDiXml . PLUGIN_TYPE_TAG )) {
6364 continue ;
6465 }
6566
66- XmlAttribute pluginNameAttribute = pluginXmlTag .getAttribute ("name" );
67+ XmlAttribute pluginNameAttribute = pluginXmlTag .getAttribute (ModuleDiXml . PLUGIN_TYPE_ATTR_NAME );
6768
6869 String pluginNameAttributeValue = pluginNameAttribute .getValue ();
6970 if (pluginNameAttributeValue == null ) {
7071 continue ;
7172 }
7273
73- List <XmlTag > targetObservers = fetchObserverTagsFromPluginTag (pluginXmlTag );
74+ List <XmlTag > targetPlugin = fetchPluginTagsFromPluginTag (pluginXmlTag );
7475
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 );
7879
7980 if (pluginTypeNameAttribute == null || (pluginTypeDisabledAttribute != null && pluginTypeDisabledAttribute .getValue ().equals ("true" ))) {
8081 continue ;
8182 }
8283
8384 String pluginTypeName = pluginTypeNameAttribute .getValue ();
8485 String pluginTypeKey = pluginNameAttributeValue .concat ("_" ).concat (pluginTypeName );
85- if (targetObserversHash .containsKey (pluginTypeKey )) {
86+ if (targetPluginHash .containsKey (pluginTypeKey )) {
8687 problemsHolder .registerProblem (
8788 pluginTypeNameAttribute .getValueElement (),
88- duplicatedObserverNameSameFileProblemDescription ,
89+ duplicatedPluginNameSameFileProblemDescription ,
8990 errorSeverity
9091 );
9192 }
92- targetObserversHash .put (pluginTypeKey , pluginTypeXmlTag );
93+ targetPluginHash .put (pluginTypeKey , pluginTypeXmlTag );
9394
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 ) {
9697 Map .Entry <String , String > module = moduleEntry .entrySet ().iterator ().next ();
9798 String moduleName = module .getKey ();
9899 String scope = module .getValue ();
@@ -101,7 +102,7 @@ public void visitFile(PsiFile file) {
101102 problemsHolder .registerProblem (
102103 pluginTypeNameAttribute .getValueElement (),
103104 String .format (
104- duplicatedObserverNameProblemDescription ,
105+ duplicatedPluginNameProblemDescription ,
105106 pluginTypeName ,
106107 pluginNameAttributeValue ,
107108 moduleName ,
@@ -119,7 +120,7 @@ public void visitFile(PsiFile file) {
119120 private List <HashMap <String , String >> fetchModuleNamesWhereSamePluginNameUsed (String pluginNameAttributeValue , String pluginTypeName , PluginIndex pluginIndex , PsiFile file ) {
120121 List <HashMap <String , String >> modulesName = new ArrayList <>();
121122 String currentFileDirectory = file .getContainingDirectory ().toString ();
122- String currentFileFullPath = currentFileDirectory .concat ("/" ).concat (file .getName ());
123+ String currentFileFullPath = currentFileDirectory .concat (File . separator ).concat (file .getName ());
123124
124125 Collection <PsiElement > indexedPlugins = pluginIndex .getPluginElements (pluginNameAttributeValue , GlobalSearchScope .getScopeRestrictedByFileTypes (
125126 GlobalSearchScope .allScope (file .getProject ()),
@@ -138,20 +139,20 @@ private List<HashMap<String, String>> fetchModuleNamesWhereSamePluginNameUsed(St
138139 }
139140
140141 String indexedFileDirectory = indexedAttributeParent .getContainingDirectory ().toString ();
141- String indexedFileFullPath = indexedFileDirectory .concat ("/" ).concat (indexedAttributeParent .getName ());
142+ String indexedFileFullPath = indexedFileDirectory .concat (File . separator ).concat (indexedAttributeParent .getName ());
142143 if (indexedFileFullPath .equals (currentFileFullPath )) {
143144 continue ;
144145 }
145146
146147 String scope = getAreaFromFileDirectory (indexedAttributeParent );
147148
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 ) {
152153 continue ;
153154 }
154- if (!pluginTypeName .equals (indexedObserverNameAttribute .getValue ())){
155+ if (!pluginTypeName .equals (indexedPluginNameAttribute .getValue ())){
155156 continue ;
156157 }
157158 addModuleNameWhereSamePluginUsed (modulesName , indexedAttributeParent , scope );
@@ -161,15 +162,15 @@ private List<HashMap<String, String>> fetchModuleNamesWhereSamePluginNameUsed(St
161162 return modulesName ;
162163 }
163164
164- private List <XmlTag > fetchObserverTagsFromPluginTag (XmlTag pluginXmlTag ) {
165+ private List <XmlTag > fetchPluginTagsFromPluginTag (XmlTag pluginXmlTag ) {
165166 List <XmlTag > result = new ArrayList <>();
166167 XmlTag [] pluginTypeXmlTags = PsiTreeUtil .getChildrenOfType (pluginXmlTag , XmlTag .class );
167168 if (pluginTypeXmlTags == null ) {
168169 return result ;
169170 }
170171
171172 for (XmlTag pluginTypeXmlTag : pluginTypeXmlTags ) {
172- if (!pluginTypeXmlTag .getName ().equals ("plugin" )) {
173+ if (!pluginTypeXmlTag .getName ().equals (ModuleDiXml . PLUGIN_TAG_NAME )) {
173174 continue ;
174175 }
175176
@@ -186,7 +187,7 @@ private void addModuleNameWhereSamePluginUsed(List<HashMap<String, String>> modu
186187 if (!moduleDeclarationTag .getName ().equals ("module" )) {
187188 return ;
188189 }
189- XmlAttribute moduleNameAttribute = moduleDeclarationTag .getAttribute ("name" );
190+ XmlAttribute moduleNameAttribute = moduleDeclarationTag .getAttribute (ModuleXml . MODULE_ATTR_NAME );
190191 if (moduleNameAttribute == null ) {
191192 return ;
192193 }
@@ -234,8 +235,8 @@ private VirtualFile getFileByPath(String moduleXmlFilePath) {
234235
235236 private String getModuleXmlFilePathByConfigFileDirectory (String fileDirectory , String fileArea ) {
236237 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 );
239240 }
240241 return moduleXmlFile .replace ("PsiDirectory:" , "file:" );
241242 }
0 commit comments