From 3adf276768b89e3e6853bff7ff017fd37d41f2c0 Mon Sep 17 00:00:00 2001 From: Serhii Akulov Date: Thu, 6 May 2021 17:43:14 +0300 Subject: [PATCH 1/3] Develop plugin declaration types inspections --- resources/META-INF/plugin.xml | 7 ++ .../PluginAttrTypeInspection.html | 19 +++ resources/magento2/inspection.properties | 1 + .../xml/PluginAttrTypeInspection.java | 114 ++++++++++++++++++ .../attrArgTypeValueIsEmpty/di.xml | 6 + .../attrTypeClassExists/di.xml | 6 + .../classAttrTypeDoesNotExists/di.xml | 6 + .../classAttrTypeIsExist/di.xml | 6 + .../xml/PluginAttrTypeInspectionTest.java | 89 ++++++++++++++ 9 files changed, 254 insertions(+) create mode 100644 resources/inspectionDescriptions/PluginAttrTypeInspection.html create mode 100644 src/com/magento/idea/magento2plugin/inspections/xml/PluginAttrTypeInspection.java create mode 100644 testData/inspections/xml/PluginAttrTypeInspection/attrArgTypeValueIsEmpty/di.xml create mode 100644 testData/inspections/xml/PluginAttrTypeInspection/attrTypeClassExists/di.xml create mode 100644 testData/inspections/xml/PluginAttrTypeInspection/classAttrTypeDoesNotExists/di.xml create mode 100644 testData/inspections/xml/PluginAttrTypeInspection/classAttrTypeIsExist/di.xml create mode 100644 tests/com/magento/idea/magento2plugin/inspections/xml/PluginAttrTypeInspectionTest.java diff --git a/resources/META-INF/plugin.xml b/resources/META-INF/plugin.xml index 1ff54b24d..b1afd7d72 100644 --- a/resources/META-INF/plugin.xml +++ b/resources/META-INF/plugin.xml @@ -222,6 +222,13 @@ enabledByDefault="true" level="WARNING" implementationClass="com.magento.idea.magento2plugin.inspections.xml.InvalidDependencyInjectionTypeInspection"/> + + diff --git a/resources/inspectionDescriptions/PluginAttrTypeInspection.html b/resources/inspectionDescriptions/PluginAttrTypeInspection.html new file mode 100644 index 000000000..a81d6880d --- /dev/null +++ b/resources/inspectionDescriptions/PluginAttrTypeInspection.html @@ -0,0 +1,19 @@ + + + +

+ Validates if attribute `type` in the <plugin/> tag of di.xml files contains valid classes or interfaces + and that it cannot be empty. +

+

This inspection inspects: +

    +
  • The existence of the class on the specified path
  • +
  • Tag `type` is not empty
  • +
+ + diff --git a/resources/magento2/inspection.properties b/resources/magento2/inspection.properties index 8ed815dc5..0f7bcd43e 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.PluginAttrTypeInspection=Inspection for the attribute `type` in the `plugin` tag inspection.plugin.duplicateInSameFile=The plugin name already used in this file. For more details see Inspection Description. 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. inspection.plugin.disabledPluginDoesNotExist=This plugin does not exist to be disabled. diff --git a/src/com/magento/idea/magento2plugin/inspections/xml/PluginAttrTypeInspection.java b/src/com/magento/idea/magento2plugin/inspections/xml/PluginAttrTypeInspection.java new file mode 100644 index 000000000..41d2f684f --- /dev/null +++ b/src/com/magento/idea/magento2plugin/inspections/xml/PluginAttrTypeInspection.java @@ -0,0 +1,114 @@ +/* + * 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.PsiElement; +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 PluginAttrTypeInspection 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 visitXmlTag(final XmlTag xmlTag) { + final PsiFile file = xmlTag.getContainingFile(); + + if (!file.getName().equals(ModuleDiXml.FILE_NAME) + || !xmlTag.getName().equals(ModuleDiXml.PLUGIN_TAG_NAME)) { + return; + } + + final XmlAttribute pluginTypeAttribute = + xmlTag.getAttribute(ModuleDiXml.TYPE_ATTR); + + if (pluginTypeAttribute == null + || pluginTypeAttribute.getValue() == null + || pluginTypeAttribute.getValueElement() == null + || pluginTypeAttribute.getValueElement().getText().isEmpty()) { + return; + } + + if (!notEmptyValidator.validate(pluginTypeAttribute.getValue())) { + reportCouldNotBeEmpty( + pluginTypeAttribute.getValueElement(), + pluginTypeAttribute.getName() + ); + } + + if (!phpClassExistenceValidator.validate(pluginTypeAttribute.getValue())) { + reportClassDoesNotExists( + pluginTypeAttribute.getValueElement(), + pluginTypeAttribute.getValue() + ); + } + } + + /** + * Report Attribute Value could not be empty. + * + * @param psiElement PsiElement + * @param messageParams Object... + */ + private void reportCouldNotBeEmpty( + final @NotNull PsiElement psiElement, + final Object... messageParams + ) { + problemsHolder.registerProblem( + psiElement, + inspectionBundle.message( + "inspection.error.idAttributeCanNotBeEmpty", + messageParams + ), + ProblemHighlightType.ERROR + ); + } + + /** + * Report class does not exists. + * + * @param psiElement PsiElement + * @param messageParams Object... + */ + private void reportClassDoesNotExists( + final @NotNull PsiElement psiElement, + final Object... messageParams + ) { + problemsHolder.registerProblem( + psiElement, + inspectionBundle.message( + "inspection.warning.class.does.not.exist", + messageParams + ), + ProblemHighlightType.WARNING + ); + } + }; + } +} diff --git a/testData/inspections/xml/PluginAttrTypeInspection/attrArgTypeValueIsEmpty/di.xml b/testData/inspections/xml/PluginAttrTypeInspection/attrArgTypeValueIsEmpty/di.xml new file mode 100644 index 000000000..d59a47dd8 --- /dev/null +++ b/testData/inspections/xml/PluginAttrTypeInspection/attrArgTypeValueIsEmpty/di.xml @@ -0,0 +1,6 @@ + + + + + + diff --git a/testData/inspections/xml/PluginAttrTypeInspection/attrTypeClassExists/di.xml b/testData/inspections/xml/PluginAttrTypeInspection/attrTypeClassExists/di.xml new file mode 100644 index 000000000..69ae9c322 --- /dev/null +++ b/testData/inspections/xml/PluginAttrTypeInspection/attrTypeClassExists/di.xml @@ -0,0 +1,6 @@ + + + + + + diff --git a/testData/inspections/xml/PluginAttrTypeInspection/classAttrTypeDoesNotExists/di.xml b/testData/inspections/xml/PluginAttrTypeInspection/classAttrTypeDoesNotExists/di.xml new file mode 100644 index 000000000..1cd190a2d --- /dev/null +++ b/testData/inspections/xml/PluginAttrTypeInspection/classAttrTypeDoesNotExists/di.xml @@ -0,0 +1,6 @@ + + + + + + diff --git a/testData/inspections/xml/PluginAttrTypeInspection/classAttrTypeIsExist/di.xml b/testData/inspections/xml/PluginAttrTypeInspection/classAttrTypeIsExist/di.xml new file mode 100644 index 000000000..69ae9c322 --- /dev/null +++ b/testData/inspections/xml/PluginAttrTypeInspection/classAttrTypeIsExist/di.xml @@ -0,0 +1,6 @@ + + + + + + diff --git a/tests/com/magento/idea/magento2plugin/inspections/xml/PluginAttrTypeInspectionTest.java b/tests/com/magento/idea/magento2plugin/inspections/xml/PluginAttrTypeInspectionTest.java new file mode 100644 index 000000000..3e19229c5 --- /dev/null +++ b/tests/com/magento/idea/magento2plugin/inspections/xml/PluginAttrTypeInspectionTest.java @@ -0,0 +1,89 @@ +/* + * Copyright © Magento, Inc. All rights reserved. + * See COPYING.txt for license details. + */ + +package com.magento.idea.magento2plugin.inspections.xml; + +import com.magento.idea.magento2plugin.magento.files.ModuleDiXml; + +public class PluginAttrTypeInspectionTest extends InspectionXmlFixtureTestCase { + + private static final String ARGUMENT_VALUE_IS_EMPTY = + "inspection.error.idAttributeCanNotBeEmpty"; + private static final String CLASS_DOES_NOT_EXIST = + "inspection.warning.class.does.not.exist"; + private static final String EXISTENT_CLASS = + "Magento\\Catalog\\Plugin\\PluginClass"; + private static final String NOT_EXISTENT_CLASS = + "Not\\Existent\\Class"; + + @Override + public void setUp() throws Exception { + super.setUp(); + myFixture.enableInspections(PluginAttrTypeInspection.class); + } + + /** + * Test for an error for the "type" attribute because it is empty. + * + */ + public void testAttrArgTypeValueIsEmpty() { + configureFixture(); + + final String forAttrIsEmptyMessage = inspectionBundle.message( + ARGUMENT_VALUE_IS_EMPTY, + ModuleDiXml.TYPE_ATTR + ); + + assertHasHighlighting(forAttrIsEmptyMessage); + } + + /** + * Test for an no error for the "type" attribute because this class exists. + * + */ + public void testAttrTypeClassExists() { + configureFixture(); + + final String typeAttrIsEmptyMessage = inspectionBundle.message( + ARGUMENT_VALUE_IS_EMPTY, + ModuleDiXml.TYPE_ATTR + ); + + assertHasNoHighlighting(typeAttrIsEmptyMessage); + } + + /** + * Test for throwing an error for a class that does not exist for the "type" attribute. + */ + public void testClassAttrTypeDoesNotExists() { + configureFixture(); + + final String forClassDoesNotExists = inspectionBundle.message( + CLASS_DOES_NOT_EXIST, + NOT_EXISTENT_CLASS + ); + + assertHasHighlighting(forClassDoesNotExists); + } + + /** + * Test for the absence of an error in the presence of + * classes or interfaces specified for plugins. + */ + public void testClassAttrTypeIsExist() { + configureFixture(); + + final String classOneExists = inspectionBundle.message( + CLASS_DOES_NOT_EXIST, + EXISTENT_CLASS + ); + + assertHasNoHighlighting(classOneExists); + } + + private void configureFixture() { + myFixture.configureByFile(getFixturePath(ModuleDiXml.FILE_NAME)); + } +} \ No newline at end of file From 758d6a26df28302bd3e750328ceb9c7685ccbf28 Mon Sep 17 00:00:00 2001 From: Serhii Akulov Date: Thu, 6 May 2021 21:11:22 +0300 Subject: [PATCH 2/3] added space --- .../inspections/xml/PluginAttrTypeInspectionTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/com/magento/idea/magento2plugin/inspections/xml/PluginAttrTypeInspectionTest.java b/tests/com/magento/idea/magento2plugin/inspections/xml/PluginAttrTypeInspectionTest.java index 3e19229c5..51c43390e 100644 --- a/tests/com/magento/idea/magento2plugin/inspections/xml/PluginAttrTypeInspectionTest.java +++ b/tests/com/magento/idea/magento2plugin/inspections/xml/PluginAttrTypeInspectionTest.java @@ -86,4 +86,4 @@ public void testClassAttrTypeIsExist() { private void configureFixture() { myFixture.configureByFile(getFixturePath(ModuleDiXml.FILE_NAME)); } -} \ No newline at end of file +} From 22c01a109e073e3749c8e4f9a199698319a61040 Mon Sep 17 00:00:00 2001 From: Serhii Akulov Date: Mon, 10 May 2021 16:11:04 +0300 Subject: [PATCH 3/3] fixed code after review --- resources/META-INF/plugin.xml | 2 +- ...peInspection.java => PluginAttributeTypeInspection.java} | 2 +- .../attrArgTypeValueIsEmpty/di.xml | 0 .../attrTypeClassExists/di.xml | 0 .../classAttrTypeDoesNotExists/di.xml | 0 .../classAttrTypeIsExist/di.xml | 0 ...tionTest.java => PluginAttributeTypeInspectionTest.java} | 6 +++--- 7 files changed, 5 insertions(+), 5 deletions(-) rename src/com/magento/idea/magento2plugin/inspections/xml/{PluginAttrTypeInspection.java => PluginAttributeTypeInspection.java} (98%) rename testData/inspections/xml/{PluginAttrTypeInspection => PluginAttributeTypeInspection}/attrArgTypeValueIsEmpty/di.xml (100%) rename testData/inspections/xml/{PluginAttrTypeInspection => PluginAttributeTypeInspection}/attrTypeClassExists/di.xml (100%) rename testData/inspections/xml/{PluginAttrTypeInspection => PluginAttributeTypeInspection}/classAttrTypeDoesNotExists/di.xml (100%) rename testData/inspections/xml/{PluginAttrTypeInspection => PluginAttributeTypeInspection}/classAttrTypeIsExist/di.xml (100%) rename tests/com/magento/idea/magento2plugin/inspections/xml/{PluginAttrTypeInspectionTest.java => PluginAttributeTypeInspectionTest.java} (91%) diff --git a/resources/META-INF/plugin.xml b/resources/META-INF/plugin.xml index 67d73f98c..e1a0a5ef5 100644 --- a/resources/META-INF/plugin.xml +++ b/resources/META-INF/plugin.xml @@ -234,7 +234,7 @@ bundle="magento2.inspection" key="inspection.displayName.PluginAttrTypeInspection" groupBundle="magento2.inspection" groupKey="inspection.group.name" enabledByDefault="true" level="WARNING" - implementationClass="com.magento.idea.magento2plugin.inspections.xml.PluginAttrTypeInspection"/> + implementationClass="com.magento.idea.magento2plugin.inspections.xml.PluginAttributeTypeInspection"/> diff --git a/src/com/magento/idea/magento2plugin/inspections/xml/PluginAttrTypeInspection.java b/src/com/magento/idea/magento2plugin/inspections/xml/PluginAttributeTypeInspection.java similarity index 98% rename from src/com/magento/idea/magento2plugin/inspections/xml/PluginAttrTypeInspection.java rename to src/com/magento/idea/magento2plugin/inspections/xml/PluginAttributeTypeInspection.java index 41d2f684f..54649a79d 100644 --- a/src/com/magento/idea/magento2plugin/inspections/xml/PluginAttrTypeInspection.java +++ b/src/com/magento/idea/magento2plugin/inspections/xml/PluginAttributeTypeInspection.java @@ -21,7 +21,7 @@ import com.magento.idea.magento2plugin.magento.files.ModuleDiXml; import org.jetbrains.annotations.NotNull; -public class PluginAttrTypeInspection extends XmlSuppressableInspectionTool { +public class PluginAttributeTypeInspection extends XmlSuppressableInspectionTool { @Override public @NotNull diff --git a/testData/inspections/xml/PluginAttrTypeInspection/attrArgTypeValueIsEmpty/di.xml b/testData/inspections/xml/PluginAttributeTypeInspection/attrArgTypeValueIsEmpty/di.xml similarity index 100% rename from testData/inspections/xml/PluginAttrTypeInspection/attrArgTypeValueIsEmpty/di.xml rename to testData/inspections/xml/PluginAttributeTypeInspection/attrArgTypeValueIsEmpty/di.xml diff --git a/testData/inspections/xml/PluginAttrTypeInspection/attrTypeClassExists/di.xml b/testData/inspections/xml/PluginAttributeTypeInspection/attrTypeClassExists/di.xml similarity index 100% rename from testData/inspections/xml/PluginAttrTypeInspection/attrTypeClassExists/di.xml rename to testData/inspections/xml/PluginAttributeTypeInspection/attrTypeClassExists/di.xml diff --git a/testData/inspections/xml/PluginAttrTypeInspection/classAttrTypeDoesNotExists/di.xml b/testData/inspections/xml/PluginAttributeTypeInspection/classAttrTypeDoesNotExists/di.xml similarity index 100% rename from testData/inspections/xml/PluginAttrTypeInspection/classAttrTypeDoesNotExists/di.xml rename to testData/inspections/xml/PluginAttributeTypeInspection/classAttrTypeDoesNotExists/di.xml diff --git a/testData/inspections/xml/PluginAttrTypeInspection/classAttrTypeIsExist/di.xml b/testData/inspections/xml/PluginAttributeTypeInspection/classAttrTypeIsExist/di.xml similarity index 100% rename from testData/inspections/xml/PluginAttrTypeInspection/classAttrTypeIsExist/di.xml rename to testData/inspections/xml/PluginAttributeTypeInspection/classAttrTypeIsExist/di.xml diff --git a/tests/com/magento/idea/magento2plugin/inspections/xml/PluginAttrTypeInspectionTest.java b/tests/com/magento/idea/magento2plugin/inspections/xml/PluginAttributeTypeInspectionTest.java similarity index 91% rename from tests/com/magento/idea/magento2plugin/inspections/xml/PluginAttrTypeInspectionTest.java rename to tests/com/magento/idea/magento2plugin/inspections/xml/PluginAttributeTypeInspectionTest.java index 51c43390e..712c69233 100644 --- a/tests/com/magento/idea/magento2plugin/inspections/xml/PluginAttrTypeInspectionTest.java +++ b/tests/com/magento/idea/magento2plugin/inspections/xml/PluginAttributeTypeInspectionTest.java @@ -7,7 +7,7 @@ import com.magento.idea.magento2plugin.magento.files.ModuleDiXml; -public class PluginAttrTypeInspectionTest extends InspectionXmlFixtureTestCase { +public class PluginAttributeTypeInspectionTest extends InspectionXmlFixtureTestCase { private static final String ARGUMENT_VALUE_IS_EMPTY = "inspection.error.idAttributeCanNotBeEmpty"; @@ -21,7 +21,7 @@ public class PluginAttrTypeInspectionTest extends InspectionXmlFixtureTestCase { @Override public void setUp() throws Exception { super.setUp(); - myFixture.enableInspections(PluginAttrTypeInspection.class); + myFixture.enableInspections(PluginAttributeTypeInspection.class); } /** @@ -40,7 +40,7 @@ public void testAttrArgTypeValueIsEmpty() { } /** - * Test for an no error for the "type" attribute because this class exists. + * Test for no error for the "type" attribute because this class exists. * */ public void testAttrTypeClassExists() {