Skip to content

Commit d1e3fd5

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

File tree

4 files changed

+138
-0
lines changed

4 files changed

+138
-0
lines changed

resources/META-INF/plugin.xml

Lines changed: 7 additions & 0 deletions
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"
Lines changed: 21 additions & 0 deletions
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

Lines changed: 1 addition & 0 deletions
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.
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
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+
public class InvalidVirtualTypeSourceClassInspection extends XmlSuppressableInspectionTool {
25+
26+
@Override
27+
public @NotNull PsiElementVisitor buildVisitor(
28+
final @NotNull ProblemsHolder problemsHolder,
29+
final boolean isOnTheFly
30+
) {
31+
return new XmlElementVisitor() {
32+
33+
private final InspectionBundle inspectionBundle = new InspectionBundle();
34+
private final InspectionValidator phpClassExistenceValidator =
35+
new PhpClassExistenceValidator(problemsHolder.getProject());
36+
private final InspectionValidator notEmptyValidator = new NotEmptyValidator();
37+
38+
@Override
39+
public void visitXmlAttribute(final XmlAttribute attribute) {
40+
final PsiFile file = attribute.getContainingFile();
41+
final XmlTag tag = attribute.getParent();
42+
43+
if (file == null
44+
|| tag == null
45+
|| !file.getName().equals(ModuleDiXml.FILE_NAME)
46+
|| !attribute.getName().equals(ModuleDiXml.TYPE_ATTR)
47+
|| !tag.getName().equals(ModuleDiXml.VIRTUAL_TYPE_TAG)
48+
) {
49+
return;
50+
}
51+
52+
if (attribute.getValue() == null
53+
|| attribute.getValueElement() == null
54+
|| attribute.getValueElement().getText().isEmpty()
55+
) {
56+
return;
57+
}
58+
59+
if (!notEmptyValidator.validate(attribute.getValue())) {
60+
reportCouldNotBeEmpty(attribute.getValueElement(), attribute.getName());
61+
}
62+
63+
if (!phpClassExistenceValidator.validate(attribute.getValue())) {
64+
reportClassDoesNotExists(attribute.getValueElement(), attribute.getValue());
65+
}
66+
}
67+
68+
/**
69+
* Report attribute value could not be empty.
70+
*
71+
* @param psiElement PsiElement
72+
* @param messageParams Object...
73+
*/
74+
private void reportCouldNotBeEmpty(
75+
final @NotNull PsiElement psiElement,
76+
final Object... messageParams
77+
) {
78+
problemsHolder.registerProblem(
79+
psiElement,
80+
inspectionBundle.message(
81+
"inspection.error.idAttributeCanNotBeEmpty",
82+
messageParams
83+
),
84+
ProblemHighlightType.ERROR
85+
);
86+
}
87+
88+
/**
89+
* Report class does not exist.
90+
*
91+
* @param psiElement PsiElement
92+
* @param messageParams Object...
93+
*/
94+
private void reportClassDoesNotExists(
95+
final @NotNull PsiElement psiElement,
96+
final Object... messageParams
97+
) {
98+
problemsHolder.registerProblem(
99+
psiElement,
100+
inspectionBundle.message(
101+
"inspection.warning.class.does.not.exist",
102+
messageParams
103+
),
104+
ProblemHighlightType.WARNING
105+
);
106+
}
107+
};
108+
}
109+
}

0 commit comments

Comments
 (0)