From e820c4024624261395a7ea17bdf499fc4095a808 Mon Sep 17 00:00:00 2001 From: vitaliy Date: Sun, 6 Nov 2022 10:21:27 +0200 Subject: [PATCH 1/4] Config Scope inspection implementation --- resources/META-INF/plugin.xml | 7 ++ .../ModuleScopeInspection.html | 27 +++++++ resources/magento2/inspection.properties | 2 + .../xml/ModuleScopeInspection.java | 76 +++++++++++++++++++ 4 files changed, 112 insertions(+) create mode 100644 resources/inspectionDescriptions/ModuleScopeInspection.html create mode 100644 src/com/magento/idea/magento2plugin/inspections/xml/ModuleScopeInspection.java diff --git a/resources/META-INF/plugin.xml b/resources/META-INF/plugin.xml index 36b295aec..c2c0918c1 100644 --- a/resources/META-INF/plugin.xml +++ b/resources/META-INF/plugin.xml @@ -314,6 +314,13 @@ enabledByDefault="true" level="WARNING" implementationClass="com.magento.idea.magento2plugin.inspections.xml.PluginAttributeTypeInspection"/> + + + +

Magento area type check.

+

+ Magento is organized into these main areas: + + Admin (adminhtml): entry point for this area is pub/index.php. The Admin panel area includes the code needed for store management. The /app/design/adminhtml directory contains all the code for components you’ll see while working in the Admin. + + Storefront (frontend): entry point for this area is pub/index.php. The storefront (or frontend) contains template and layout files that define the appearance of your storefront. + + Basic (base): used as a fallback for files absent in adminhtml and frontend areas. + + Cron (crontab): In pub/cron.php, the \Magento\Framework\App\Cron class always loads the ‘crontab’ area. + + You can also send requests to Magento using the SOAP, REST and GraphQL APIs. These three areas + + Web API REST (webapi_rest): entry point for this area is pub/index.php. The REST area has a front controller that understands how to do URL lookups for REST-based URLs. + + GraphQL (graphql): entry point for this area is pub/index.php. + + Web API SOAP (webapi_soap): entry point for this area is pub/index.php. +

+

+ See https://developer.adobe.com/commerce/php/architecture/modules/areas/ for more information. +

+ + \ No newline at end of file diff --git a/resources/magento2/inspection.properties b/resources/magento2/inspection.properties index 63bf14e38..2b6b7ea86 100644 --- a/resources/magento2/inspection.properties +++ b/resources/magento2/inspection.properties @@ -39,3 +39,5 @@ inspection.warning.class.does.not.exist=The class "{0}" does not exist inspection.warning.method.does.not.exist=The method "{0}" does not exist in the service class inspection.warning.method.should.have.public.access=The method "{0}" should have public access inspection.warning.method.should.have.public.access.fix=Change the method access +inspection.displayName.ModuleScopeInspection=Module Configuration Scope Inspection +inspection.config.wrong.area = The config file area is wrong. Please check the spelling of the parent directory, it should be equal to the following: adminhtml, frontend, crontab, webapi_rest, webapi_soap, graphql. diff --git a/src/com/magento/idea/magento2plugin/inspections/xml/ModuleScopeInspection.java b/src/com/magento/idea/magento2plugin/inspections/xml/ModuleScopeInspection.java new file mode 100644 index 000000000..be4a995fa --- /dev/null +++ b/src/com/magento/idea/magento2plugin/inspections/xml/ModuleScopeInspection.java @@ -0,0 +1,76 @@ +/* + * 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.openapi.vfs.VirtualFile; +import com.intellij.psi.*; +import com.magento.idea.magento2plugin.bundles.InspectionBundle; +import com.magento.idea.magento2plugin.magento.files.ModuleEventsXml; +import com.magento.idea.magento2plugin.magento.packages.Areas; +import com.jetbrains.php.lang.inspections.PhpInspection; +import com.magento.idea.magento2plugin.magento.packages.ComponentType; +import com.magento.idea.magento2plugin.magento.packages.Package; +import com.magento.idea.magento2plugin.util.magento.GetMagentoModuleUtil; +import org.jetbrains.annotations.NotNull; +import java.util.*; + +public class ModuleScopeInspection extends PhpInspection { + + @NotNull + @SuppressWarnings({"PMD.AvoidInstantiatingObjectsInLoops", "PMD.CognitiveComplexity"}) + public PsiElementVisitor buildVisitor( + final @NotNull ProblemsHolder problemsHolder, + final boolean isOnTheFly + ) { + return new XmlElementVisitor() { + private final HashMap loadedFileHash = new HashMap<>();//NOPMD + private final InspectionBundle inspectionBundle = new InspectionBundle(); + private final ProblemHighlightType errorSeverity = ProblemHighlightType.WARNING; + + @Override + public void visitFile(final @NotNull PsiFile file) { + if (!ModuleEventsXml.FILE_NAME.equals(file.getName())) { + return; + } + + final PsiDirectory targetDirectory = file.getParent(); + if (targetDirectory == null) { + return; + } + + final PsiDirectory parentDirectory = targetDirectory.getParent(); + if (parentDirectory == null) { + return; + } + + if (!parentDirectory.getName().equals(Package.moduleBaseAreaDir)) { + return; + } + + final GetMagentoModuleUtil.MagentoModuleData moduleData = GetMagentoModuleUtil + .getByContext(targetDirectory, file.getProject()); + + if (moduleData == null || moduleData.getType() == null || !moduleData.getType().equals(ComponentType.module)) { + return; + } + + final String directoryName = targetDirectory.getName(); + final Areas area = Areas.getAreaByString(targetDirectory.getName()); + if (area == null || directoryName.equals(Areas.base)) { + problemsHolder.registerProblem( + file, + inspectionBundle.message( + "inspection.config.wrong.area" + ), + errorSeverity + ); + } + } + }; + } +} From 75e97d04ee55be69b1370813ca38cf45d3421ce8 Mon Sep 17 00:00:00 2001 From: vitaliy Date: Sun, 6 Nov 2022 11:31:37 +0200 Subject: [PATCH 2/4] added test coverage --- resources/magento2/inspection.properties | 2 +- .../xml/ModuleScopeInspection.java | 16 +++--- .../code/Test/TestModule/etc/adminhtml/di.xml | 8 +++ .../app/code/Test/TestModule/registration.php | 5 ++ .../Test/TestModule/etc/adminhtmltypo/di.xml | 8 +++ .../app/code/Test/TestModule/registration.php | 5 ++ .../xml/ModuleScopeInspectionTest.java | 50 +++++++++++++++++++ 7 files changed, 84 insertions(+), 10 deletions(-) create mode 100644 testData/inspections/xml/ModuleScopeInspection/correctArea/app/code/Test/TestModule/etc/adminhtml/di.xml create mode 100644 testData/inspections/xml/ModuleScopeInspection/correctArea/app/code/Test/TestModule/registration.php create mode 100644 testData/inspections/xml/ModuleScopeInspection/incorrectArea/app/code/Test/TestModule/etc/adminhtmltypo/di.xml create mode 100644 testData/inspections/xml/ModuleScopeInspection/incorrectArea/app/code/Test/TestModule/registration.php create mode 100644 tests/com/magento/idea/magento2plugin/inspections/xml/ModuleScopeInspectionTest.java diff --git a/resources/magento2/inspection.properties b/resources/magento2/inspection.properties index 2b6b7ea86..7a47145af 100644 --- a/resources/magento2/inspection.properties +++ b/resources/magento2/inspection.properties @@ -40,4 +40,4 @@ inspection.warning.method.does.not.exist=The method "{0}" does not exist in the inspection.warning.method.should.have.public.access=The method "{0}" should have public access inspection.warning.method.should.have.public.access.fix=Change the method access inspection.displayName.ModuleScopeInspection=Module Configuration Scope Inspection -inspection.config.wrong.area = The config file area is wrong. Please check the spelling of the parent directory, it should be equal to the following: adminhtml, frontend, crontab, webapi_rest, webapi_soap, graphql. +inspection.config.wrong.area = The area of this config file is wrong. Please check the spelling of the parent directory, it should be equal to one of the following: adminhtml, frontend, crontab, webapi_rest, webapi_soap, graphql. diff --git a/src/com/magento/idea/magento2plugin/inspections/xml/ModuleScopeInspection.java b/src/com/magento/idea/magento2plugin/inspections/xml/ModuleScopeInspection.java index be4a995fa..78a0da07f 100644 --- a/src/com/magento/idea/magento2plugin/inspections/xml/ModuleScopeInspection.java +++ b/src/com/magento/idea/magento2plugin/inspections/xml/ModuleScopeInspection.java @@ -7,19 +7,21 @@ import com.intellij.codeInspection.ProblemHighlightType; import com.intellij.codeInspection.ProblemsHolder; +import com.intellij.codeInspection.XmlSuppressableInspectionTool; import com.intellij.openapi.vfs.VirtualFile; -import com.intellij.psi.*; +import com.intellij.psi.PsiDirectory; +import com.intellij.psi.PsiElementVisitor; +import com.intellij.psi.PsiFile; +import com.intellij.psi.XmlElementVisitor; import com.magento.idea.magento2plugin.bundles.InspectionBundle; -import com.magento.idea.magento2plugin.magento.files.ModuleEventsXml; import com.magento.idea.magento2plugin.magento.packages.Areas; -import com.jetbrains.php.lang.inspections.PhpInspection; import com.magento.idea.magento2plugin.magento.packages.ComponentType; import com.magento.idea.magento2plugin.magento.packages.Package; import com.magento.idea.magento2plugin.util.magento.GetMagentoModuleUtil; import org.jetbrains.annotations.NotNull; import java.util.*; -public class ModuleScopeInspection extends PhpInspection { +public class ModuleScopeInspection extends XmlSuppressableInspectionTool { @NotNull @SuppressWarnings({"PMD.AvoidInstantiatingObjectsInLoops", "PMD.CognitiveComplexity"}) @@ -34,10 +36,6 @@ public PsiElementVisitor buildVisitor( @Override public void visitFile(final @NotNull PsiFile file) { - if (!ModuleEventsXml.FILE_NAME.equals(file.getName())) { - return; - } - final PsiDirectory targetDirectory = file.getParent(); if (targetDirectory == null) { return; @@ -61,7 +59,7 @@ public void visitFile(final @NotNull PsiFile file) { final String directoryName = targetDirectory.getName(); final Areas area = Areas.getAreaByString(targetDirectory.getName()); - if (area == null || directoryName.equals(Areas.base)) { + if (area == null || directoryName.equals(Areas.base.toString())) { problemsHolder.registerProblem( file, inspectionBundle.message( diff --git a/testData/inspections/xml/ModuleScopeInspection/correctArea/app/code/Test/TestModule/etc/adminhtml/di.xml b/testData/inspections/xml/ModuleScopeInspection/correctArea/app/code/Test/TestModule/etc/adminhtml/di.xml new file mode 100644 index 000000000..646a5af9c --- /dev/null +++ b/testData/inspections/xml/ModuleScopeInspection/correctArea/app/code/Test/TestModule/etc/adminhtml/di.xml @@ -0,0 +1,8 @@ + + + diff --git a/testData/inspections/xml/ModuleScopeInspection/correctArea/app/code/Test/TestModule/registration.php b/testData/inspections/xml/ModuleScopeInspection/correctArea/app/code/Test/TestModule/registration.php new file mode 100644 index 000000000..cfee220f7 --- /dev/null +++ b/testData/inspections/xml/ModuleScopeInspection/correctArea/app/code/Test/TestModule/registration.php @@ -0,0 +1,5 @@ + + + diff --git a/testData/inspections/xml/ModuleScopeInspection/incorrectArea/app/code/Test/TestModule/registration.php b/testData/inspections/xml/ModuleScopeInspection/incorrectArea/app/code/Test/TestModule/registration.php new file mode 100644 index 000000000..cfee220f7 --- /dev/null +++ b/testData/inspections/xml/ModuleScopeInspection/incorrectArea/app/code/Test/TestModule/registration.php @@ -0,0 +1,5 @@ + Date: Sun, 6 Nov 2022 11:45:54 +0200 Subject: [PATCH 3/4] Static fixes --- .../inspections/xml/ModuleScopeInspection.java | 9 +++++---- .../inspections/xml/ModuleScopeInspectionTest.java | 2 +- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/com/magento/idea/magento2plugin/inspections/xml/ModuleScopeInspection.java b/src/com/magento/idea/magento2plugin/inspections/xml/ModuleScopeInspection.java index 78a0da07f..f0719f191 100644 --- a/src/com/magento/idea/magento2plugin/inspections/xml/ModuleScopeInspection.java +++ b/src/com/magento/idea/magento2plugin/inspections/xml/ModuleScopeInspection.java @@ -8,7 +8,6 @@ import com.intellij.codeInspection.ProblemHighlightType; import com.intellij.codeInspection.ProblemsHolder; import com.intellij.codeInspection.XmlSuppressableInspectionTool; -import com.intellij.openapi.vfs.VirtualFile; import com.intellij.psi.PsiDirectory; import com.intellij.psi.PsiElementVisitor; import com.intellij.psi.PsiFile; @@ -19,10 +18,12 @@ import com.magento.idea.magento2plugin.magento.packages.Package; import com.magento.idea.magento2plugin.util.magento.GetMagentoModuleUtil; import org.jetbrains.annotations.NotNull; -import java.util.*; public class ModuleScopeInspection extends XmlSuppressableInspectionTool { + /** + * Inspection for the module config area. + */ @NotNull @SuppressWarnings({"PMD.AvoidInstantiatingObjectsInLoops", "PMD.CognitiveComplexity"}) public PsiElementVisitor buildVisitor( @@ -30,7 +31,6 @@ public PsiElementVisitor buildVisitor( final boolean isOnTheFly ) { return new XmlElementVisitor() { - private final HashMap loadedFileHash = new HashMap<>();//NOPMD private final InspectionBundle inspectionBundle = new InspectionBundle(); private final ProblemHighlightType errorSeverity = ProblemHighlightType.WARNING; @@ -53,7 +53,8 @@ public void visitFile(final @NotNull PsiFile file) { final GetMagentoModuleUtil.MagentoModuleData moduleData = GetMagentoModuleUtil .getByContext(targetDirectory, file.getProject()); - if (moduleData == null || moduleData.getType() == null || !moduleData.getType().equals(ComponentType.module)) { + if (moduleData == null || moduleData.getType() == null + || !moduleData.getType().equals(ComponentType.module)) { return; } diff --git a/tests/com/magento/idea/magento2plugin/inspections/xml/ModuleScopeInspectionTest.java b/tests/com/magento/idea/magento2plugin/inspections/xml/ModuleScopeInspectionTest.java index d63ffde6f..2f7e085c3 100644 --- a/tests/com/magento/idea/magento2plugin/inspections/xml/ModuleScopeInspectionTest.java +++ b/tests/com/magento/idea/magento2plugin/inspections/xml/ModuleScopeInspectionTest.java @@ -17,7 +17,7 @@ public void setUp() throws Exception { } /** - * Inspection highlights warning if the area of a config file is wrong + * Inspection highlights warning if the area of a config file is wrong. */ public void testIncorrectArea() { configureFixture("app/code/Test/TestModule/etc/adminhtmltypo/di.xml"); From b9304c06dca408fe8e92ef9a59d69352f650d9d9 Mon Sep 17 00:00:00 2001 From: vitaliy Date: Sun, 6 Nov 2022 11:49:49 +0200 Subject: [PATCH 4/4] Static fixes 2 --- .../inspections/xml/ModuleScopeInspectionTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/com/magento/idea/magento2plugin/inspections/xml/ModuleScopeInspectionTest.java b/tests/com/magento/idea/magento2plugin/inspections/xml/ModuleScopeInspectionTest.java index 2f7e085c3..cf3674ffe 100644 --- a/tests/com/magento/idea/magento2plugin/inspections/xml/ModuleScopeInspectionTest.java +++ b/tests/com/magento/idea/magento2plugin/inspections/xml/ModuleScopeInspectionTest.java @@ -43,7 +43,7 @@ public void testCorrectArea() { assertHasNoHighlighting(errorMessage); } - private void configureFixture(String fixturePath) { + private void configureFixture(final String fixturePath) { myFixture.copyFileToProject(getFixturePath("app/code/Test/TestModule/registration.php")); myFixture.configureByFile(getFixturePath(fixturePath)); }