Skip to content

Commit a14c790

Browse files
author
Vasilii Burlacu
committed
Covered with tests the PluginDuplicationInspection and refactored the implementation
1 parent dc0d014 commit a14c790

File tree

25 files changed

+367
-74
lines changed

25 files changed

+367
-74
lines changed

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

Lines changed: 19 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
import com.intellij.codeInspection.ProblemHighlightType;
99
import com.intellij.codeInspection.ProblemsHolder;
1010
import com.intellij.ide.highlighter.XmlFileType;
11-
import com.intellij.openapi.vfs.VfsUtil;
12-
import com.intellij.openapi.vfs.VirtualFile;
1311
import com.intellij.psi.*;
1412
import com.intellij.psi.search.GlobalSearchScope;
1513
import com.intellij.psi.util.PsiTreeUtil;
@@ -21,24 +19,21 @@
2119
import com.magento.idea.magento2plugin.bundles.InspectionBundle;
2220
import com.magento.idea.magento2plugin.indexes.PluginIndex;
2321
import com.magento.idea.magento2plugin.magento.files.ModuleDiXml;
24-
import com.magento.idea.magento2plugin.magento.files.ModuleXml;
2522
import com.magento.idea.magento2plugin.magento.packages.Package;
23+
import com.magento.idea.magento2plugin.util.magento.GetModuleNameByDirectory;
2624
import org.jetbrains.annotations.NotNull;
2725
import org.jetbrains.annotations.Nullable;
2826
import java.io.File;
29-
import java.net.MalformedURLException;
30-
import java.net.URL;
3127
import java.util.*;
28+
import com.intellij.openapi.util.Pair;
3229

3330
public class PluginDeclarationInspection extends PhpInspection {
3431

3532
@NotNull
3633
@Override
3734
public PsiElementVisitor buildVisitor(@NotNull ProblemsHolder problemsHolder, boolean b) {
3835
return new XmlElementVisitor() {
39-
private final String moduleXmlFileName = ModuleXml.getInstance().getFileName();
4036
private InspectionBundle inspectionBundle = new InspectionBundle();
41-
private HashMap<String, VirtualFile> loadedFileHash = new HashMap<>();
4237
private final ProblemHighlightType errorSeverity = ProblemHighlightType.WARNING;
4338

4439
@Override
@@ -86,7 +81,7 @@ public void visitFile(PsiFile file) {
8681
}
8782

8883
String pluginTypeName = pluginTypeNameAttribute.getValue();
89-
String pluginTypeKey = pluginNameAttributeValue.concat("_").concat(pluginTypeName);
84+
String pluginTypeKey = pluginNameAttributeValue.concat(Package.VENDOR_MODULE_NAME_SEPARATOR).concat(pluginTypeName);
9085
if (targetPluginHash.containsKey(pluginTypeKey)) {
9186
problemsHolder.registerProblem(
9287
pluginTypeNameAttribute.getValueElement(),
@@ -96,12 +91,12 @@ public void visitFile(PsiFile file) {
9691
}
9792
targetPluginHash.put(pluginTypeKey, pluginTypeXmlTag);
9893

99-
List<HashMap<String, String>> modulesWithSamePluginName = fetchModuleNamesWhereSamePluginNameUsed(pluginNameAttributeValue, pluginTypeName, pluginIndex, file);
100-
for (HashMap<String, String> moduleEntry: modulesWithSamePluginName) {
101-
Map.Entry<String, String> module = moduleEntry.entrySet().iterator().next();
102-
String moduleName = module.getKey();
103-
String scope = module.getValue();
104-
String problemKey = pluginTypeKey.concat("_").concat(moduleName).concat("_").concat(scope);
94+
List<Pair<String, String>> modulesWithSamePluginName = fetchModuleNamesWhereSamePluginNameUsed(pluginNameAttributeValue, pluginTypeName, pluginIndex, file);
95+
for (Pair<String, String> moduleEntry: modulesWithSamePluginName) {
96+
String scope = moduleEntry.getFirst();
97+
String moduleName = moduleEntry.getSecond();
98+
String problemKey = pluginTypeKey.concat(Package.VENDOR_MODULE_NAME_SEPARATOR)
99+
.concat(moduleName).concat(Package.VENDOR_MODULE_NAME_SEPARATOR).concat(scope);
105100
if (!pluginProblems.containsKey(problemKey)){
106101
problemsHolder.registerProblem(
107102
pluginTypeNameAttribute.getValueElement(),
@@ -121,8 +116,8 @@ public void visitFile(PsiFile file) {
121116
}
122117
}
123118

124-
private List<HashMap<String, String>> fetchModuleNamesWhereSamePluginNameUsed(String pluginNameAttributeValue, String pluginTypeName, PluginIndex pluginIndex, PsiFile file) {
125-
List<HashMap<String, String>> modulesName = new ArrayList<>();
119+
private List<Pair<String, String>> fetchModuleNamesWhereSamePluginNameUsed(String pluginNameAttributeValue, String pluginTypeName, PluginIndex pluginIndex, PsiFile file) {
120+
List<Pair<String, String>> modulesName = new ArrayList<>();
126121
String currentFileDirectory = file.getContainingDirectory().toString();
127122
String currentFileFullPath = currentFileDirectory.concat(File.separator).concat(file.getName());
128123

@@ -184,65 +179,15 @@ private List<XmlTag> fetchPluginTagsFromPluginTag(XmlTag pluginXmlTag) {
184179
return result;
185180
}
186181

187-
private void addModuleNameWhereSamePluginUsed(List<HashMap<String, String>> modulesName, PsiFile indexedFile, String scope) {
188-
XmlTag moduleDeclarationTag = getModuleDeclarationTagByConfigFile(indexedFile);
189-
if (moduleDeclarationTag == null) return;
182+
private void addModuleNameWhereSamePluginUsed(
183+
List<Pair<String, String>> modulesName,
184+
PsiFile indexedFile,
185+
String scope
186+
) {
187+
String moduleName = GetModuleNameByDirectory.getInstance(problemsHolder.getProject())
188+
.execute(indexedFile.getContainingDirectory());
190189

191-
if (!moduleDeclarationTag.getName().equals("module")) {
192-
return;
193-
}
194-
XmlAttribute moduleNameAttribute = moduleDeclarationTag.getAttribute(ModuleXml.MODULE_ATTR_NAME);
195-
if (moduleNameAttribute == null) {
196-
return;
197-
}
198-
199-
HashMap<String, String> moduleEntry = new HashMap<>();
200-
201-
moduleEntry.put(moduleNameAttribute.getValue(), scope);
202-
modulesName.add(moduleEntry);
203-
}
204-
205-
@Nullable
206-
private XmlTag getModuleDeclarationTagByConfigFile(PsiFile file) {
207-
String fileDirectory = file.getContainingDirectory().toString();
208-
String fileArea = file.getContainingDirectory().getName();
209-
String moduleXmlFilePath = getModuleXmlFilePathByConfigFileDirectory(fileDirectory, fileArea);
210-
211-
VirtualFile virtualFile = getFileByPath(moduleXmlFilePath);
212-
if (virtualFile == null) return null;
213-
214-
PsiFile moduleDeclarationFile = PsiManager.getInstance(file.getProject()).findFile(virtualFile);
215-
XmlTag[] moduleDeclarationTags = getFileXmlTags(moduleDeclarationFile);
216-
if (moduleDeclarationTags == null) {
217-
return null;
218-
}
219-
return moduleDeclarationTags[0];
220-
}
221-
222-
@Nullable
223-
private VirtualFile getFileByPath(String moduleXmlFilePath) {
224-
if (loadedFileHash.containsKey(moduleXmlFilePath)) {
225-
return loadedFileHash.get(moduleXmlFilePath);
226-
}
227-
VirtualFile virtualFile;
228-
try {
229-
virtualFile = VfsUtil.findFileByURL(new URL(moduleXmlFilePath));
230-
} catch (MalformedURLException e) {
231-
return null;
232-
}
233-
if (virtualFile == null) {
234-
return null;
235-
}
236-
loadedFileHash.put(moduleXmlFilePath, virtualFile);
237-
return virtualFile;
238-
}
239-
240-
private String getModuleXmlFilePathByConfigFileDirectory(String fileDirectory, String fileArea) {
241-
String moduleXmlFile = fileDirectory.replace(fileArea, "").concat(moduleXmlFileName);
242-
if (fileDirectory.endsWith(Package.MODULE_BASE_AREA_DIR)) {
243-
moduleXmlFile = fileDirectory.concat(File.separator).concat(moduleXmlFileName);
244-
}
245-
return moduleXmlFile.replace("PsiDirectory:", "file:");
190+
modulesName.add(Pair.create(scope, moduleName));
246191
}
247192

248193
@Nullable
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0"?>
2+
<config>
3+
<type name="Magento\Backend\App\AbstractAction">
4+
<plugin name=<warning descr="The plugin name \"adminAuthentication\" for targeted \"Magento\Backend\App\AbstractAction\" class is already used in the module \"Magento_Backend\" (adminhtml scope). For more details see Inspection Description.">"adminAuthentication"</warning> type="Vendor\Module\Plugin\Authentication" />
5+
</type>
6+
</config>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0"?>
2+
<config>
3+
<type name="Magento\CatalogRule\Model\Rule">
4+
<plugin name=<warning descr="The plugin name \"configurableChildValidation\" for targeted \"Magento\CatalogRule\Model\Rule\" class is already used in the module \"Magento_CatalogRuleConfigurable\" (crontab scope). For more details see Inspection Description.">"configurableChildValidation"</warning> type="Magento\CatalogRuleConfigurable\Plugin\CatalogRule\Model\Rule\Validation"/>
5+
</type>
6+
</config>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0"?>
2+
<config>
3+
<type name="Magento\Framework\View\Layout">
4+
<plugin name=<warning descr="The plugin name \"catalog-session-depersonalize\" for targeted \"Magento\Framework\View\Layout\" class is already used in the module \"Magento_Catalog\" (frontend scope). For more details see Inspection Description.">"catalog-session-depersonalize"</warning> type="Vendor\Module\Plugin\Layout" />
5+
</type>
6+
</config>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0"?>
2+
<config>
3+
<type name="Magento\Theme\Block\Html\Topmenu">
4+
<plugin name=<warning descr="The plugin name \"catalogTopmenu\" for targeted \"Magento\Theme\Block\Html\Topmenu\" class is already used in the module \"Magento_Catalog\" (base scope). For more details see Inspection Description.">"catalogTopmenu"</warning> type="Vendor\Module\Plugin\Block\Topmenu" />
5+
</type>
6+
</config>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0"?>
2+
<config>
3+
<type name="Magento\Catalog\Block\Product\ImageFactory">
4+
<plugin name=<warning descr="The plugin name \"designLoader\" for targeted \"Magento\Catalog\Block\Product\ImageFactory\" class is already used in the module \"Magento_CatalogGraphQl\" (graphql scope). For more details see Inspection Description.">"designLoader"</warning> type="Magento\CatalogGraphQl\Plugin\DesignLoader" />
5+
</type>
6+
</config>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0"?>
2+
<config>
3+
<type name="Magento\Framework\App\ResourceConnection">
4+
<plugin name=<warning descr="The plugin name \"get_catalog_category_product_index_table_name_rest_api\" for targeted \"Magento\Framework\App\ResourceConnection\" class is already used in the module \"Magento_Catalog\" (webapi_rest scope). For more details see Inspection Description.">"get_catalog_category_product_index_table_name_rest_api"</warning> type="Vendor\Module\Plugin\Resource" />
5+
</type>
6+
</config>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0"?>
2+
<config>
3+
<type name="Magento\Framework\App\ResourceConnection">
4+
<plugin name=<warning descr="The plugin name \"get_catalog_category_product_index_table_name_soap_api\" for targeted \"Magento\Framework\App\ResourceConnection\" class is already used in the module \"Magento_Catalog\" (webapi_soap scope). For more details see Inspection Description.">"get_catalog_category_product_index_table_name_soap_api"</warning> type="Vendor\Module\Plugin\Resource" />
5+
</type>
6+
</config>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0"?>
2+
<config>
3+
<type name="Magento\CatalogSearch\Helper\Data">
4+
<plugin name="unique_plugin_name" type="Vendor\Module\Plugin" />
5+
</type>
6+
</config>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?xml version="1.0"?>
2+
<config>
3+
<type name="Magento\CatalogSearch\Helper\Data">
4+
<plugin name="plugin_name" type="Vendor\Module\Plugin" />
5+
<plugin name=<warning descr="The plugin name already used in this file. For more details see Inspection Description.">"plugin_name"</warning> type="Vendor\Module\Plugin" />
6+
</type>
7+
</config>

0 commit comments

Comments
 (0)