Skip to content

Commit 7e0ab87

Browse files
1100: Added inspection to check if type attr value in the virtual type tag exists
1 parent b5dce10 commit 7e0ab87

File tree

4 files changed

+111
-0
lines changed

4 files changed

+111
-0
lines changed

resources/META-INF/plugin.xml

+7
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,13 @@
293293
enabledByDefault="true" level="WARNING"
294294
implementationClass="com.magento.idea.magento2plugin.inspections.xml.InvalidDependencyInjectionTypeInspection"/>
295295

296+
<localInspection language="XML" groupPath="XML"
297+
shortName="InvalidVirtualTypeSourceClassInspection"
298+
bundle="magento2.inspection" key="inspection.displayName.InvalidVirtualTypeSourceClassInspection"
299+
groupBundle="magento2.inspection" groupKey="inspection.group.name"
300+
enabledByDefault="true" level="WARNING"
301+
implementationClass="com.magento.idea.magento2plugin.inspections.xml.InvalidVirtualTypeSourceClassInspection"/>
302+
296303
<localInspection language="XML" groupPath="XML"
297304
shortName="PreferenceXmlInspections"
298305
bundle="magento2.inspection" key="inspection.displayName.PreferenceXmlInspections"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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+
Validates if value in the type attribute inside the &lt;virtualType/&gt; tag of di.xml files contains valid class,
11+
interface, factory or proxy name.
12+
</p>
13+
<p>This inspection checks type attribute of the &lt;virtualType/&gt; tag.</p>
14+
<p>This inspection supports next types:</p>
15+
<ul>
16+
<li>PHP classes</li>
17+
<li>PHP interfaces</li>
18+
<li>PHP classes or interfaces with added Factory or \Proxy suffixes</li>
19+
</ul>
20+
</body>
21+
</html>

resources/magento2/inspection.properties

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ inspection.displayName.ModuleDeclarationInModuleXmlInspection=Inspection for the
88
inspection.displayName.AclResourceXmlInspection=Inspection for the Title XML required attribute in the `etc/acl.xml` file
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
11+
inspection.displayName.InvalidVirtualTypeSourceClassInspection=Invalid source type specified for virtual type in the `di.xml` file
1112
inspection.displayName.PluginAttrTypeInspection=Inspection for the attribute `type` in the `plugin` tag
1213
inspection.displayName.PreferenceXmlInspections=Inspection for the Preference declaration
1314
inspection.plugin.duplicateInSameFile=The plugin name already used in this file. For more details see Inspection Description.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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.PsiElementVisitor;
12+
import com.intellij.psi.PsiFile;
13+
import com.intellij.psi.XmlElementVisitor;
14+
import com.intellij.psi.xml.XmlAttribute;
15+
import com.intellij.psi.xml.XmlTag;
16+
import com.magento.idea.magento2plugin.bundles.InspectionBundle;
17+
import com.magento.idea.magento2plugin.inspections.validator.InspectionValidator;
18+
import com.magento.idea.magento2plugin.inspections.validator.NotEmptyValidator;
19+
import com.magento.idea.magento2plugin.inspections.validator.PhpClassExistenceValidator;
20+
import com.magento.idea.magento2plugin.magento.files.ModuleDiXml;
21+
import org.jetbrains.annotations.NotNull;
22+
23+
public class InvalidVirtualTypeSourceClassInspection extends XmlSuppressableInspectionTool {
24+
25+
@Override
26+
public @NotNull PsiElementVisitor buildVisitor(
27+
final @NotNull ProblemsHolder problemsHolder,
28+
final boolean isOnTheFly
29+
) {
30+
return new XmlElementVisitor() {
31+
32+
private final InspectionBundle inspectionBundle = new InspectionBundle();
33+
private final InspectionValidator phpClassExistenceValidator =
34+
new PhpClassExistenceValidator(problemsHolder.getProject());
35+
private final InspectionValidator notEmptyValidator = new NotEmptyValidator();
36+
37+
@Override
38+
public void visitXmlAttribute(final XmlAttribute attribute) {
39+
final PsiFile file = attribute.getContainingFile();
40+
final XmlTag tag = attribute.getParent();
41+
42+
if (file == null
43+
|| tag == null
44+
|| !file.getName().equals(ModuleDiXml.FILE_NAME)
45+
|| !attribute.getName().equals(ModuleDiXml.TYPE_ATTR)
46+
|| !tag.getName().equals(ModuleDiXml.VIRTUAL_TYPE_TAG)
47+
) {
48+
return;
49+
}
50+
51+
if (attribute.getValue() == null
52+
|| attribute.getValueElement() == null
53+
|| attribute.getValueElement().getText().isEmpty()
54+
) {
55+
return;
56+
}
57+
58+
if (!notEmptyValidator.validate(attribute.getValue())) {
59+
problemsHolder.registerProblem(
60+
attribute.getValueElement(),
61+
inspectionBundle.message(
62+
"inspection.error.idAttributeCanNotBeEmpty",
63+
attribute.getName()
64+
),
65+
ProblemHighlightType.ERROR
66+
);
67+
}
68+
69+
if (!phpClassExistenceValidator.validate(attribute.getValue())) {
70+
problemsHolder.registerProblem(
71+
attribute.getValueElement(),
72+
inspectionBundle.message(
73+
"inspection.warning.class.does.not.exist",
74+
attribute.getValue()
75+
),
76+
ProblemHighlightType.WARNING
77+
);
78+
}
79+
}
80+
};
81+
}
82+
}

0 commit comments

Comments
 (0)