Skip to content

Commit 695db83

Browse files
authored
Merge pull request #432 from drpayyne/issue-424
Added inspection warning for disabled observer
2 parents c67fc7a + 07fc246 commit 695db83

File tree

4 files changed

+39
-4
lines changed

4 files changed

+39
-4
lines changed

resources/magento2/inspection.properties

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ inspection.plugin.error.redundantParameter=Redundant parameter
1515
inspection.plugin.error.typeIncompatibility=Possible type incompatibility. Consider changing the parameter according to the target method.
1616
inspection.observer.duplicateInSameFile=The observer name already used in this file. For more details see Inspection Description.
1717
inspection.observer.duplicateInOtherPlaces=The observer name "{0}" for event "{1}" is already used in the module "{2}" ({3} scope). For more details see Inspection Description.
18+
inspection.observer.disabledObserverDoesNotExist=This observer does not exist to be disabled. For more details, see Inspection Description.
1819
inspection.cache.disabledCache=Cacheable false attribute on the default layout will disable cache site-wide
1920
inspection.moduleDeclaration.warning.wrongModuleName=Provided module name "{0}" does not match expected "{1}"
2021
inspection.moduleDeclaration.fix=Fix module name

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

+16-4
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,7 @@ public void visitFile(final PsiFile file) {
9494
final XmlAttribute observerDisabledAttribute =
9595
observerXmlTag.getAttribute("disabled");
9696

97-
if (observerNameAttribute == null || (
98-
observerDisabledAttribute != null//NOPMD
99-
&& observerDisabledAttribute.getValue().equals("true"))
100-
) {
97+
if (observerNameAttribute == null) {
10198
continue;
10299
}
103100

@@ -122,6 +119,21 @@ public void visitFile(final PsiFile file) {
122119
eventIndex,
123120
file
124121
);
122+
123+
if (observerDisabledAttribute != null
124+
&& observerDisabledAttribute.getValue() != null
125+
&& observerDisabledAttribute.getValue().equals("true")
126+
&& modulesWithSameObserverName.isEmpty()
127+
) {
128+
problemsHolder.registerProblem(
129+
observerNameAttribute.getValueElement(),
130+
inspectionBundle.message(
131+
"inspection.observer.disabledObserverDoesNotExist"
132+
),
133+
errorSeverity
134+
);
135+
}
136+
125137
for (final HashMap<String, String> moduleEntry:
126138
modulesWithSameObserverName) {
127139
final Map.Entry<String, String> module = moduleEntry
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0"?>
2+
<!--
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
-->
8+
<config>
9+
<event name="test_event_in_test_class">
10+
<observer name=<warning descr="This observer does not exist to be disabled. For more details, see Inspection Description.">"test_non_existing_observer"</warning>
11+
instance="Magento\Catalog\Observer\TestObserver"
12+
disabled="true"/>
13+
</event>
14+
</config>

tests/com/magento/idea/magento2plugin/inspections/xml/ObserverDeclarationInspectionTest.java

+8
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,12 @@ public void testObserverNameUsedInDifferentFile() {
3131
myFixture.configureByFile(getFixturePath(ModuleEventsXml.FILE_NAME));
3232
myFixture.testHighlighting(true, false, false);
3333
}
34+
35+
/**
36+
* Tests warning for disabling of non-existing observer.
37+
*/
38+
public void testDisablingNonExistingObserver() {
39+
myFixture.configureByFile(getFixturePath(ModuleEventsXml.FILE_NAME));
40+
myFixture.testHighlighting(true, false, false);
41+
}
3442
}

0 commit comments

Comments
 (0)