From 7e0ab870ec9a04988376de92cb553fbac95982e7 Mon Sep 17 00:00:00 2001 From: bohdan-harniuk Date: Thu, 8 Sep 2022 21:18:40 +0300 Subject: [PATCH] 1100: Added inspection to check if type attr value in the virtual type tag exists --- resources/META-INF/plugin.xml | 7 ++ ...validVirtualTypeSourceClassInspection.html | 21 +++++ resources/magento2/inspection.properties | 1 + ...validVirtualTypeSourceClassInspection.java | 82 +++++++++++++++++++ 4 files changed, 111 insertions(+) create mode 100644 resources/inspectionDescriptions/InvalidVirtualTypeSourceClassInspection.html create mode 100644 src/com/magento/idea/magento2plugin/inspections/xml/InvalidVirtualTypeSourceClassInspection.java diff --git a/resources/META-INF/plugin.xml b/resources/META-INF/plugin.xml index 6334bf93f..51a387590 100644 --- a/resources/META-INF/plugin.xml +++ b/resources/META-INF/plugin.xml @@ -293,6 +293,13 @@ enabledByDefault="true" level="WARNING" implementationClass="com.magento.idea.magento2plugin.inspections.xml.InvalidDependencyInjectionTypeInspection"/> + + + + +

+ Validates if value in the type attribute inside the <virtualType/> tag of di.xml files contains valid class, + interface, factory or proxy name. +

+

This inspection checks type attribute of the <virtualType/> tag.

+

This inspection supports next types:

+
    +
  • PHP classes
  • +
  • PHP interfaces
  • +
  • PHP classes or interfaces with added Factory or \Proxy suffixes
  • +
+ + diff --git a/resources/magento2/inspection.properties b/resources/magento2/inspection.properties index b41be33d5..63bf14e38 100644 --- a/resources/magento2/inspection.properties +++ b/resources/magento2/inspection.properties @@ -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. diff --git a/src/com/magento/idea/magento2plugin/inspections/xml/InvalidVirtualTypeSourceClassInspection.java b/src/com/magento/idea/magento2plugin/inspections/xml/InvalidVirtualTypeSourceClassInspection.java new file mode 100644 index 000000000..e717f8a0e --- /dev/null +++ b/src/com/magento/idea/magento2plugin/inspections/xml/InvalidVirtualTypeSourceClassInspection.java @@ -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 + ); + } + } + }; + } +}