Skip to content

Commit af10b92

Browse files
author
Vasilii Burlacu
committed
Added the missing area scope into the check
1 parent ae4c0e8 commit af10b92

File tree

1 file changed

+46
-18
lines changed

1 file changed

+46
-18
lines changed

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

+46-18
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,8 @@
2323
import org.jetbrains.annotations.NotNull;
2424
import java.net.MalformedURLException;
2525
import java.net.URL;
26-
import java.util.ArrayList;
27-
import java.util.Collection;
28-
import java.util.HashMap;
29-
import java.util.List;
26+
import java.util.*;
27+
3028
import com.intellij.openapi.vfs.VfsUtil;
3129
import org.jetbrains.annotations.Nullable;
3230

@@ -93,9 +91,12 @@ public void visitFile(PsiFile file) {
9391
}
9492
targetObserversHash.put(observerKey, observerXmlTag);
9593

96-
HashMap<String, String> modulesWithSameObserverName = fetchModuleNamesWhereSameObserverNameUsed(eventNameAttributeValue, observerName, eventIndex, file);
97-
for (String moduleName: modulesWithSameObserverName.keySet()) {
98-
String problemKey = observerKey.concat("_").concat(moduleName);
94+
List<HashMap<String, String>> modulesWithSameObserverName = fetchModuleNamesWhereSameObserverNameUsed(eventNameAttributeValue, observerName, eventIndex, file);
95+
for (HashMap<String, String> moduleEntry: modulesWithSameObserverName) {
96+
Map.Entry<String, String> module = moduleEntry.entrySet().iterator().next();
97+
String moduleName = module.getKey();
98+
String scope = module.getValue();
99+
String problemKey = observerKey.concat("_").concat(moduleName).concat("_").concat(scope);
99100
if (!eventProblems.containsKey(problemKey)){
100101
problemsHolder.registerProblem(
101102
observerNameAttribute.getValueElement(),
@@ -104,7 +105,7 @@ public void visitFile(PsiFile file) {
104105
observerName,
105106
eventNameAttributeValue,
106107
moduleName,
107-
modulesWithSameObserverName.get(moduleName)
108+
scope
108109
),
109110
errorSeverity
110111
);
@@ -115,11 +116,10 @@ public void visitFile(PsiFile file) {
115116
}
116117
}
117118

118-
private HashMap<String, String> fetchModuleNamesWhereSameObserverNameUsed(String eventNameAttributeValue, String observerName, EventIndex eventIndex, PsiFile file) {
119-
HashMap<String, String> modulesName = new HashMap<String, String>();
119+
private List<HashMap<String, String>> fetchModuleNamesWhereSameObserverNameUsed(String eventNameAttributeValue, String observerName, EventIndex eventIndex, PsiFile file) {
120+
List<HashMap<String, String>> modulesName = new ArrayList<>();
120121
String currentFileDirectory = file.getContainingDirectory().toString();
121122
String currentFileFullPath = currentFileDirectory.concat("/").concat(file.getName());
122-
String scope = MagentoPackages.AREA_BASE;
123123

124124
Collection<PsiElement> indexedEvents = eventIndex.getEventElements(eventNameAttributeValue, GlobalSearchScope.getScopeRestrictedByFileTypes(
125125
GlobalSearchScope.allScope(file.getProject()),
@@ -143,11 +143,7 @@ private HashMap<String, String> fetchModuleNamesWhereSameObserverNameUsed(String
143143
continue;
144144
}
145145

146-
if (indexedFileDirectory.contains(MagentoPackages.AREA_ADMINHTML)) {
147-
scope = MagentoPackages.AREA_ADMINHTML;
148-
} else if (indexedFileDirectory.contains(MagentoPackages.AREA_FRONTEND)) {
149-
scope = MagentoPackages.AREA_FRONTEND;
150-
}
146+
String scope = getAreaFromFileDirectory(indexedAttributeParent);
151147

152148
List<XmlTag> indexObserversTags = fetchObserverTagsFromEventTag((XmlTag) indexedEvent.getParent().getParent());
153149
for (XmlTag indexObserversTag: indexObserversTags) {
@@ -183,7 +179,7 @@ private List<XmlTag> fetchObserverTagsFromEventTag(XmlTag eventXmlTag) {
183179
return result;
184180
}
185181

186-
private void addModuleNameWhereSameObserverUsed(HashMap<String, String> modulesName, PsiFile indexedFile, String scope) {
182+
private void addModuleNameWhereSameObserverUsed(List<HashMap<String, String>> modulesName, PsiFile indexedFile, String scope) {
187183
XmlTag moduleDeclarationTag = getModuleDeclarationTagByConfigFile(indexedFile);
188184
if (moduleDeclarationTag == null) return;
189185

@@ -195,7 +191,10 @@ private void addModuleNameWhereSameObserverUsed(HashMap<String, String> modulesN
195191
return;
196192
}
197193

198-
modulesName.put(moduleNameAttribute.getValue(), scope);
194+
HashMap<String, String> moduleEntry = new HashMap<>();
195+
196+
moduleEntry.put(moduleNameAttribute.getValue(), scope);
197+
modulesName.add(moduleEntry);
199198
}
200199

201200
@Nullable
@@ -247,6 +246,35 @@ private XmlTag[] getFileXmlTags(PsiFile file) {
247246
XmlTag xmlRootTag = PsiTreeUtil.getChildOfType(xmlDocument, XmlTag.class);
248247
return PsiTreeUtil.getChildrenOfType(xmlRootTag, XmlTag.class);
249248
}
249+
250+
private String getAreaFromFileDirectory(@NotNull PsiFile file) {
251+
if (file.getParent() == null) {
252+
return "";
253+
}
254+
255+
String areaFromFileDirectory = file.getParent().getName();
256+
257+
if (areaFromFileDirectory.equals("etc")) {
258+
return MagentoPackages.AREA_BASE;
259+
}
260+
261+
List<String> possibleAreas = new ArrayList<>(Arrays.asList(
262+
MagentoPackages.AREA_ADMINHTML,
263+
MagentoPackages.AREA_FRONTEND,
264+
MagentoPackages.AREA_CRON,
265+
MagentoPackages.AREA_API_REST,
266+
MagentoPackages.AREA_API_SOAP,
267+
MagentoPackages.AREA_GRAPHQL
268+
));
269+
270+
for (String area: possibleAreas) {
271+
if (area.equals(areaFromFileDirectory)) {
272+
return area;
273+
}
274+
}
275+
276+
return "";
277+
}
250278
};
251279
}
252280
}

0 commit comments

Comments
 (0)