diff --git a/resources/META-INF/plugin.xml b/resources/META-INF/plugin.xml index 04e5261d4..0d09c515a 100644 --- a/resources/META-INF/plugin.xml +++ b/resources/META-INF/plugin.xml @@ -315,6 +315,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..7a47145af 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 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 new file mode 100644 index 000000000..f0719f191 --- /dev/null +++ b/src/com/magento/idea/magento2plugin/inspections/xml/ModuleScopeInspection.java @@ -0,0 +1,75 @@ +/* + * 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.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.packages.Areas; +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; + +public class ModuleScopeInspection extends XmlSuppressableInspectionTool { + + /** + * Inspection for the module config area. + */ + @NotNull + @SuppressWarnings({"PMD.AvoidInstantiatingObjectsInLoops", "PMD.CognitiveComplexity"}) + public PsiElementVisitor buildVisitor( + final @NotNull ProblemsHolder problemsHolder, + final boolean isOnTheFly + ) { + return new XmlElementVisitor() { + private final InspectionBundle inspectionBundle = new InspectionBundle(); + private final ProblemHighlightType errorSeverity = ProblemHighlightType.WARNING; + + @Override + public void visitFile(final @NotNull PsiFile file) { + 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.toString())) { + problemsHolder.registerProblem( + file, + inspectionBundle.message( + "inspection.config.wrong.area" + ), + errorSeverity + ); + } + } + }; + } +} 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 @@ +