Skip to content

Commit

Permalink
Merge pull request #1176 from bohdan-harniuk/1100-add-virtual-type-cl…
Browse files Browse the repository at this point in the history
…ass-existence-inspection

1100: Added inspection to check if type attr value in the virtual type tag exists
  • Loading branch information
Iamwade authored Sep 9, 2022
2 parents b4c4899 + ee1561e commit 1bbc95e
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 0 deletions.
7 changes: 7 additions & 0 deletions resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,13 @@
enabledByDefault="true" level="WARNING"
implementationClass="com.magento.idea.magento2plugin.inspections.xml.InvalidDependencyInjectionTypeInspection"/>

<localInspection language="XML" groupPath="XML"
shortName="InvalidVirtualTypeSourceClassInspection"
bundle="magento2.inspection" key="inspection.displayName.InvalidVirtualTypeSourceClassInspection"
groupBundle="magento2.inspection" groupKey="inspection.group.name"
enabledByDefault="true" level="WARNING"
implementationClass="com.magento.idea.magento2plugin.inspections.xml.InvalidVirtualTypeSourceClassInspection"/>

<localInspection language="XML" groupPath="XML"
shortName="PreferenceXmlInspections"
bundle="magento2.inspection" key="inspection.displayName.PreferenceXmlInspections"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<!--
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
-->
<html>
<body>
<p>
Validates if value in the type attribute inside the &lt;virtualType/&gt; tag of di.xml files contains valid class,
interface, factory or proxy name.
</p>
<p>This inspection checks type attribute of the &lt;virtualType/&gt; tag.</p>
<p>This inspection supports next types:</p>
<ul>
<li>PHP classes</li>
<li>PHP interfaces</li>
<li>PHP classes or interfaces with added Factory or \Proxy suffixes</li>
</ul>
</body>
</html>
1 change: 1 addition & 0 deletions resources/magento2/inspection.properties
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ inspection.displayName.ModuleDeclarationInModuleXmlInspection=Inspection for the
inspection.displayName.AclResourceXmlInspection=Inspection for the Title XML required attribute in the `etc/acl.xml` file
inspection.displayName.WebApiServiceInspection=Inspection for the Web API XML service declaration
inspection.displayName.InvalidDiTypeInspection=Invalid type configuration in the `etc/di.xml` file
inspection.displayName.InvalidVirtualTypeSourceClassInspection=Invalid source type specified for virtual type in the `di.xml` file
inspection.displayName.PluginAttrTypeInspection=Inspection for the attribute `type` in the `plugin` tag
inspection.displayName.PreferenceXmlInspections=Inspection for the Preference declaration
inspection.plugin.duplicateInSameFile=The plugin name already used in this file. For more details see Inspection Description.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

package com.magento.idea.magento2plugin.inspections.xml;

import com.intellij.codeInspection.ProblemHighlightType;
import com.intellij.codeInspection.ProblemsHolder;
import com.intellij.codeInspection.XmlSuppressableInspectionTool;
import com.intellij.psi.PsiElementVisitor;
import com.intellij.psi.PsiFile;
import com.intellij.psi.XmlElementVisitor;
import com.intellij.psi.xml.XmlAttribute;
import com.intellij.psi.xml.XmlTag;
import com.magento.idea.magento2plugin.bundles.InspectionBundle;
import com.magento.idea.magento2plugin.inspections.validator.InspectionValidator;
import com.magento.idea.magento2plugin.inspections.validator.NotEmptyValidator;
import com.magento.idea.magento2plugin.inspections.validator.PhpClassExistenceValidator;
import com.magento.idea.magento2plugin.magento.files.ModuleDiXml;
import org.jetbrains.annotations.NotNull;

public class InvalidVirtualTypeSourceClassInspection extends XmlSuppressableInspectionTool {

@Override
public @NotNull PsiElementVisitor buildVisitor(
final @NotNull ProblemsHolder problemsHolder,
final boolean isOnTheFly
) {
return new XmlElementVisitor() {

private final InspectionBundle inspectionBundle = new InspectionBundle();
private final InspectionValidator phpClassExistenceValidator =
new PhpClassExistenceValidator(problemsHolder.getProject());
private final InspectionValidator notEmptyValidator = new NotEmptyValidator();

@Override
public void visitXmlAttribute(final XmlAttribute attribute) {
final PsiFile file = attribute.getContainingFile();
final XmlTag tag = attribute.getParent();

if (file == null
|| tag == null
|| !file.getName().equals(ModuleDiXml.FILE_NAME)
|| !attribute.getName().equals(ModuleDiXml.TYPE_ATTR)
|| !tag.getName().equals(ModuleDiXml.VIRTUAL_TYPE_TAG)
) {
return;
}

if (attribute.getValue() == null
|| attribute.getValueElement() == null
|| attribute.getValueElement().getText().isEmpty()
) {
return;
}

if (!notEmptyValidator.validate(attribute.getValue())) {
problemsHolder.registerProblem(
attribute.getValueElement(),
inspectionBundle.message(
"inspection.error.idAttributeCanNotBeEmpty",
attribute.getName()
),
ProblemHighlightType.ERROR
);
}

if (!phpClassExistenceValidator.validate(attribute.getValue())) {
problemsHolder.registerProblem(
attribute.getValueElement(),
inspectionBundle.message(
"inspection.warning.class.does.not.exist",
attribute.getValue()
),
ProblemHighlightType.WARNING
);
}
}
};
}
}

0 comments on commit 1bbc95e

Please sign in to comment.