Skip to content

Commit dd8ac97

Browse files
author
Vasilii Burlacu
committed
adjusting the namings
1 parent 8aa28bb commit dd8ac97

File tree

4 files changed

+35
-35
lines changed

4 files changed

+35
-35
lines changed

src/com/magento/idea/magento2plugin/inspections/xml/PluginDeclarationInspection.java

Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,13 @@
1919
import com.intellij.psi.xml.XmlTag;
2020
import com.jetbrains.php.lang.inspections.PhpInspection;
2121
import com.magento.idea.magento2plugin.indexes.PluginIndex;
22+
import com.magento.idea.magento2plugin.magento.files.ModuleDiXml;
2223
import com.magento.idea.magento2plugin.magento.files.ModuleXml;
2324
import com.magento.idea.magento2plugin.magento.packages.Package;
2425
import org.jetbrains.annotations.NotNull;
2526
import org.jetbrains.annotations.Nullable;
2627

28+
import java.io.File;
2729
import java.net.MalformedURLException;
2830
import java.net.URL;
2931
import 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
}

src/com/magento/idea/magento2plugin/magento/files/ModuleDiXml.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ public class ModuleDiXml implements ModuleFileInterface {
2222
public static String PLUGIN_TYPE_ATTR_NAME = "name";
2323
public static String PREFERENCE_TAG_NAME = "preference";
2424
public static String PREFERENCE_ATTR_FOR = "for";
25+
public static String DISABLED_ATTR_NAME = "disabled";
2526

2627
private static ModuleDiXml INSTANCE = null;
2728

src/com/magento/idea/magento2plugin/stubs/indexes/PluginIndex.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,6 @@
2424
import java.util.Map;
2525
import java.util.Set;
2626

27-
/**
28-
* Created by dkvashnin on 11/10/15.
29-
*/
3027
public class PluginIndex extends FileBasedIndexExtension<String, Set<String>> {
3128
public static final ID<String, Set<String>> KEY
3229
= ID.create("com.magento.idea.magento2plugin.stubs.indexes.plugin_to_type");

src/com/magento/idea/magento2plugin/stubs/indexes/PluginNameIndex.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import com.jetbrains.php.lang.psi.PhpFile;
2020
import com.jetbrains.php.lang.psi.elements.MethodReference;
2121
import com.jetbrains.php.lang.psi.elements.ParameterList;
22+
import com.magento.idea.magento2plugin.magento.files.ModuleDiXml;
2223
import com.magento.idea.magento2plugin.php.util.PhpPatternsHelper;
2324
import com.magento.idea.magento2plugin.project.Settings;
2425
import org.jetbrains.annotations.NotNull;
@@ -97,8 +98,8 @@ private void grabPluginNamesFromXmlFile(XmlFile file, Map<String, Void> map) {
9798
if (xmlDocument != null) {
9899
XmlTag xmlRootTag = xmlDocument.getRootTag();
99100
if (xmlRootTag != null) {
100-
for (XmlTag pluginTag : xmlRootTag.findSubTags("plugin")) {
101-
String name = pluginTag.getAttributeValue("name");
101+
for (XmlTag pluginTag : xmlRootTag.findSubTags(ModuleDiXml.PLUGIN_TYPE_ATTRIBUTE)) {
102+
String name = pluginTag.getAttributeValue(ModuleDiXml.PLUGIN_TYPE_ATTR_NAME);
102103
if (name != null && !name.isEmpty()) {
103104
map.put(name, null);
104105
}
@@ -118,7 +119,7 @@ public KeyDescriptor<String> getKeyDescriptor() {
118119
public FileBasedIndex.InputFilter getInputFilter() {
119120
return file -> (
120121
file.getFileType() == PhpFileType.INSTANCE
121-
|| (file.getFileType() == XmlFileType.INSTANCE && file.getName().equals("di.xml"))
122+
|| (file.getFileType() == XmlFileType.INSTANCE && file.getName().equals(ModuleDiXml.FILE_NAME))
122123
);
123124
}
124125

0 commit comments

Comments
 (0)