Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,13 @@
enabledByDefault="true" level="WARNING"
implementationClass="com.magento.idea.magento2plugin.inspections.xml.PluginAttributeTypeInspection"/>

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

<!-- UCT inspection -->
<localInspection language="PHP" groupPath="UCT"
shortName="ExtendingDeprecatedClass"
Expand Down
27 changes: 27 additions & 0 deletions resources/inspectionDescriptions/ModuleScopeInspection.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<html>
<body>
<h1>Magento area type check.</h1>
<p>
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.
</p>
<p>
See https://developer.adobe.com/commerce/php/architecture/modules/areas/ for more information.
</p>
</body>
</html>
2 changes: 2 additions & 0 deletions resources/magento2/inspection.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Original file line number Diff line number Diff line change
@@ -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
);
}
}
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<warning descr="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." />
<?xml version="1.0"?>
<!--
/*
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
-->
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

use Magento\Framework\Component\ComponentRegistrar;

ComponentRegistrar::register(ComponentRegistrar::MODULE, 'Test_TestModule', __DIR__);
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<warning descr="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." />
<?xml version="1.0"?>
<!--
/*
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
-->
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

use Magento\Framework\Component\ComponentRegistrar;

ComponentRegistrar::register(ComponentRegistrar::MODULE, 'Test_TestModule', __DIR__);
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

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

public class ModuleScopeInspectionTest extends InspectionXmlFixtureTestCase {

private static final String WRONG_AREA =
"inspection.config.wrong.area";

@Override
public void setUp() throws Exception {
super.setUp();
myFixture.enableInspections(ModuleScopeInspection.class);
}

/**
* Inspection highlights warning if the area of a config file is wrong.
*/
public void testIncorrectArea() {
configureFixture("app/code/Test/TestModule/etc/adminhtmltypo/di.xml");

final String errorMessage = inspectionBundle.message(
WRONG_AREA
);

assertHasHighlighting(errorMessage);
}

/**
* Inspection skips warning if the area is correct.
*/
public void testCorrectArea() {
configureFixture("app/code/Test/TestModule/etc/adminhtml/di.xml");

final String errorMessage = inspectionBundle.message(
WRONG_AREA

);

assertHasNoHighlighting(errorMessage);
}

private void configureFixture(final String fixturePath) {
myFixture.copyFileToProject(getFixturePath("app/code/Test/TestModule/registration.php"));
myFixture.configureByFile(getFixturePath(fixturePath));
}
}