Skip to content

Commit 9f50f5c

Browse files
committed
fixed merge conflict
2 parents 3adf276 + 0b7041d commit 9f50f5c

File tree

14 files changed

+358
-2
lines changed

14 files changed

+358
-2
lines changed

resources/META-INF/plugin.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,13 @@
222222
enabledByDefault="true" level="WARNING"
223223
implementationClass="com.magento.idea.magento2plugin.inspections.xml.InvalidDependencyInjectionTypeInspection"/>
224224

225+
<localInspection language="XML" groupPath="XML"
226+
shortName="PreferenceXmlInspections"
227+
bundle="magento2.inspection" key="inspection.displayName.PreferenceXmlInspections"
228+
groupBundle="magento2.inspection" groupKey="inspection.group.name"
229+
enabledByDefault="true" level="WARNING"
230+
implementationClass="com.magento.idea.magento2plugin.inspections.xml.PreferenceDeclarationInspection"/>
231+
225232
<localInspection language="XML" groupPath="XML"
226233
shortName="PluginAttrTypeInspection"
227234
bundle="magento2.inspection" key="inspection.displayName.PluginAttrTypeInspection"
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<!--
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
-->
7+
<html>
8+
<body>
9+
<p>
10+
Validation of attributes inside the "preference" tag
11+
</p>
12+
<p>This inspection checks the "for" and "type" attribute of the &ltpreference/&gt; tag for:</p>
13+
<ul>
14+
<li>the correct path to the class or interface</li>
15+
<li>existence of attribute values</li>
16+
</ul>
17+
</body>
18+
</html>

resources/magento2/inspection.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ inspection.displayName.AclResourceXmlInspection=Inspection for the Title XML req
99
inspection.displayName.WebApiServiceInspection=Inspection for the Web API XML service declaration
1010
inspection.displayName.InvalidDiTypeInspection=Invalid type configuration in the `etc/di.xml` file
1111
inspection.displayName.PluginAttrTypeInspection=Inspection for the attribute `type` in the `plugin` tag
12+
inspection.displayName.PreferenceXmlInspections=Inspection for the Preference declaration
1213
inspection.plugin.duplicateInSameFile=The plugin name already used in this file. For more details see Inspection Description.
1314
inspection.plugin.duplicateInOtherPlaces=The plugin name "{0}" for targeted "{1}" class is already used in the module "{2}" ({3} scope). For more details see Inspection Description.
1415
inspection.plugin.disabledPluginDoesNotExist=This plugin does not exist to be disabled.

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ public void visitXmlTag(final @NotNull XmlTag xmlTag) {
5555

5656
if (nameAttribute == null
5757
|| nameAttribute.getValue() == null
58-
|| nameAttribute.getValueElement() == null) {
58+
|| nameAttribute.getValueElement() == null
59+
|| nameAttribute.getValueElement().getText().isEmpty()) {
5960
return;
6061
}
6162

@@ -100,8 +101,9 @@ private void checkObjectArgumentsRecursively(final @NotNull XmlTag tag) {
100101
final XmlAttribute xsiTypeAttr = tag.getAttribute(ModuleDiXml.XSI_TYPE_ATTR);
101102

102103
if (xsiTypeAttr == null
104+
|| xsiTypeAttr.getValue() == null
103105
|| xsiTypeAttr.getValueElement() == null
104-
|| xsiTypeAttr.getValue() == null) {
106+
|| xsiTypeAttr.getValueElement().getText().isEmpty()) {
105107
return;
106108
}
107109
final String xsiTypeValue = xsiTypeAttr.getValue();
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
/*
2+
* Copyright © Magento, Inc. All rights reserved.
3+
* See COPYING.txt for license details.
4+
*/
5+
6+
package com.magento.idea.magento2plugin.inspections.xml;
7+
8+
import com.intellij.codeInspection.ProblemHighlightType;
9+
import com.intellij.codeInspection.ProblemsHolder;
10+
import com.intellij.codeInspection.XmlSuppressableInspectionTool;
11+
import com.intellij.psi.PsiElement;
12+
import com.intellij.psi.PsiElementVisitor;
13+
import com.intellij.psi.PsiFile;
14+
import com.intellij.psi.XmlElementVisitor;
15+
import com.intellij.psi.xml.XmlAttribute;
16+
import com.intellij.psi.xml.XmlTag;
17+
import com.magento.idea.magento2plugin.bundles.InspectionBundle;
18+
import com.magento.idea.magento2plugin.inspections.validator.InspectionValidator;
19+
import com.magento.idea.magento2plugin.inspections.validator.NotEmptyValidator;
20+
import com.magento.idea.magento2plugin.inspections.validator.PhpClassExistenceValidator;
21+
import com.magento.idea.magento2plugin.magento.files.ModuleDiXml;
22+
import org.jetbrains.annotations.NotNull;
23+
24+
@SuppressWarnings({"PMD.ExcessiveMethodLength", "PMD.NPathComplexity"})
25+
public class PreferenceDeclarationInspection extends XmlSuppressableInspectionTool {
26+
27+
@Override
28+
public @NotNull PsiElementVisitor buildVisitor(
29+
final @NotNull ProblemsHolder problemsHolder,
30+
final boolean isOnTheFly
31+
) {
32+
return new XmlElementVisitor() {
33+
34+
private final InspectionBundle inspectionBundle = new InspectionBundle();
35+
private final InspectionValidator phpClassExistenceValidator =
36+
new PhpClassExistenceValidator(problemsHolder.getProject());
37+
private final InspectionValidator notEmptyValidator = new NotEmptyValidator();
38+
39+
@Override
40+
public void visitXmlTag(final XmlTag xmlTag) {
41+
final PsiFile file = xmlTag.getContainingFile();
42+
43+
if (!file.getName().equals(ModuleDiXml.FILE_NAME)
44+
|| !xmlTag.getName().equals(ModuleDiXml.PREFERENCE_TAG_NAME)) {
45+
return;
46+
}
47+
48+
final XmlAttribute preferenceForAttribute =
49+
xmlTag.getAttribute(ModuleDiXml.PREFERENCE_ATTR_FOR);
50+
51+
if (preferenceForAttribute == null
52+
|| preferenceForAttribute.getValue() == null
53+
|| preferenceForAttribute.getValueElement() == null
54+
|| preferenceForAttribute.getValueElement().getText().isEmpty()) {
55+
return;
56+
}
57+
58+
if (!notEmptyValidator.validate(preferenceForAttribute.getValue())) {
59+
reportCouldNotBeEmpty(
60+
preferenceForAttribute.getValueElement(),
61+
preferenceForAttribute.getName()
62+
);
63+
}
64+
65+
final XmlAttribute preferenceTypeAttribute =
66+
xmlTag.getAttribute(ModuleDiXml.TYPE_ATTR);
67+
68+
if (preferenceTypeAttribute == null
69+
|| preferenceTypeAttribute.getValue() == null
70+
|| preferenceTypeAttribute.getValueElement() == null
71+
|| preferenceTypeAttribute.getValueElement().getText().isEmpty()) {
72+
return;
73+
}
74+
75+
if (!notEmptyValidator.validate(preferenceTypeAttribute.getValue())) {
76+
reportCouldNotBeEmpty(
77+
preferenceTypeAttribute.getValueElement(),
78+
preferenceTypeAttribute.getName()
79+
);
80+
}
81+
82+
if (!phpClassExistenceValidator.validate(preferenceForAttribute.getValue())) {
83+
reportClassDoesNotExists(
84+
preferenceForAttribute.getValueElement(),
85+
preferenceForAttribute.getValue()
86+
);
87+
}
88+
89+
if (!phpClassExistenceValidator.validate(preferenceTypeAttribute.getValue())) {
90+
reportClassDoesNotExists(
91+
preferenceTypeAttribute.getValueElement(),
92+
preferenceTypeAttribute.getValue()
93+
);
94+
}
95+
}
96+
97+
/**
98+
* Report Attribute Value could not be empty.
99+
*
100+
* @param psiElement PsiElement
101+
* @param messageParams Object...
102+
*/
103+
private void reportCouldNotBeEmpty(
104+
final @NotNull PsiElement psiElement,
105+
final Object... messageParams
106+
) {
107+
problemsHolder.registerProblem(
108+
psiElement,
109+
inspectionBundle.message(
110+
"inspection.error.idAttributeCanNotBeEmpty",
111+
messageParams
112+
),
113+
ProblemHighlightType.ERROR
114+
);
115+
}
116+
117+
/**
118+
* Report class does not exists.
119+
*
120+
* @param psiElement PsiElement
121+
* @param messageParams Object...
122+
*/
123+
private void reportClassDoesNotExists(
124+
final @NotNull PsiElement psiElement,
125+
final Object... messageParams
126+
) {
127+
problemsHolder.registerProblem(
128+
psiElement,
129+
inspectionBundle.message(
130+
"inspection.warning.class.does.not.exist",
131+
messageParams
132+
),
133+
ProblemHighlightType.WARNING
134+
);
135+
}
136+
};
137+
}
138+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?xml version="1.0"?>
2+
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
4+
<preference for="" type="Foo\Bar\Model\Logger"/>
5+
</config>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?xml version="1.0"?>
2+
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
4+
<preference for="Foo\Bar\Model\Logger" type=""/>
5+
</config>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?xml version="1.0"?>
2+
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
4+
<preference for="Foo\Bar\Model\Logger" type=""/>
5+
</config>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?xml version="1.0"?>
2+
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
4+
<preference for="" type="Foo\Bar\Model\Logger"/>
5+
</config>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?xml version="1.0"?>
2+
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
4+
<preference for="Not\Existent\Class" type=""/>
5+
</config>

0 commit comments

Comments
 (0)