Skip to content

Commit 710b4d2

Browse files
authored
Merge pull request magento#1261 from vitaliyboykocontributor/config-directory-inspection
Config Scope directory inspection
2 parents 77bc8e5 + d73dbf1 commit 710b4d2

File tree

9 files changed

+187
-0
lines changed

9 files changed

+187
-0
lines changed

resources/META-INF/plugin.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,13 @@
315315
enabledByDefault="true" level="WARNING"
316316
implementationClass="com.magento.idea.magento2plugin.inspections.xml.PluginAttributeTypeInspection"/>
317317

318+
<localInspection language="XML" groupPath="XML"
319+
shortName="ModuleScopeInspection"
320+
bundle="magento2.inspection" key="inspection.displayName.ModuleScopeInspection"
321+
groupBundle="magento2.inspection" groupKey="inspection.group.name"
322+
enabledByDefault="true" level="WARNING"
323+
implementationClass="com.magento.idea.magento2plugin.inspections.xml.ModuleScopeInspection"/>
324+
318325
<!-- UCT inspection -->
319326
<localInspection language="PHP" groupPath="UCT"
320327
shortName="ExtendingDeprecatedClass"
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<html>
2+
<body>
3+
<h1>Magento area type check.</h1>
4+
<p>
5+
Magento is organized into these main areas:
6+
7+
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.
8+
9+
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.
10+
11+
Basic (base): used as a fallback for files absent in adminhtml and frontend areas.
12+
13+
Cron (crontab): In pub/cron.php, the \Magento\Framework\App\Cron class always loads the ‘crontab’ area.
14+
15+
You can also send requests to Magento using the SOAP, REST and GraphQL APIs. These three areas
16+
17+
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.
18+
19+
GraphQL (graphql): entry point for this area is pub/index.php.
20+
21+
Web API SOAP (webapi_soap): entry point for this area is pub/index.php.
22+
</p>
23+
<p>
24+
See https://developer.adobe.com/commerce/php/architecture/modules/areas/ for more information.
25+
</p>
26+
</body>
27+
</html>

resources/magento2/inspection.properties

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,5 @@ inspection.warning.class.does.not.exist=The class "{0}" does not exist
3939
inspection.warning.method.does.not.exist=The method "{0}" does not exist in the service class
4040
inspection.warning.method.should.have.public.access=The method "{0}" should have public access
4141
inspection.warning.method.should.have.public.access.fix=Change the method access
42+
inspection.displayName.ModuleScopeInspection=Module Configuration Scope Inspection
43+
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.
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
* Copyright © Magento, Inc. All rights reserved.
3+
* See COPYING.txt for license details.
4+
*/
5+
6+
package com.magento.idea.magento2plugin.inspections.xml;
7+
8+
import com.intellij.codeInspection.ProblemHighlightType;
9+
import com.intellij.codeInspection.ProblemsHolder;
10+
import com.intellij.codeInspection.XmlSuppressableInspectionTool;
11+
import com.intellij.psi.PsiDirectory;
12+
import com.intellij.psi.PsiElementVisitor;
13+
import com.intellij.psi.PsiFile;
14+
import com.intellij.psi.XmlElementVisitor;
15+
import com.magento.idea.magento2plugin.bundles.InspectionBundle;
16+
import com.magento.idea.magento2plugin.magento.packages.Areas;
17+
import com.magento.idea.magento2plugin.magento.packages.ComponentType;
18+
import com.magento.idea.magento2plugin.magento.packages.Package;
19+
import com.magento.idea.magento2plugin.util.magento.GetMagentoModuleUtil;
20+
import org.jetbrains.annotations.NotNull;
21+
22+
public class ModuleScopeInspection extends XmlSuppressableInspectionTool {
23+
24+
/**
25+
* Inspection for the module config area.
26+
*/
27+
@NotNull
28+
@SuppressWarnings({"PMD.AvoidInstantiatingObjectsInLoops", "PMD.CognitiveComplexity"})
29+
public PsiElementVisitor buildVisitor(
30+
final @NotNull ProblemsHolder problemsHolder,
31+
final boolean isOnTheFly
32+
) {
33+
return new XmlElementVisitor() {
34+
private final InspectionBundle inspectionBundle = new InspectionBundle();
35+
private final ProblemHighlightType errorSeverity = ProblemHighlightType.WARNING;
36+
37+
@Override
38+
public void visitFile(final @NotNull PsiFile file) {
39+
final PsiDirectory targetDirectory = file.getParent();
40+
if (targetDirectory == null) {
41+
return;
42+
}
43+
44+
final PsiDirectory parentDirectory = targetDirectory.getParent();
45+
if (parentDirectory == null) {
46+
return;
47+
}
48+
49+
if (!parentDirectory.getName().equals(Package.moduleBaseAreaDir)) {
50+
return;
51+
}
52+
53+
final GetMagentoModuleUtil.MagentoModuleData moduleData = GetMagentoModuleUtil
54+
.getByContext(targetDirectory, file.getProject());
55+
56+
if (moduleData == null || moduleData.getType() == null
57+
|| !moduleData.getType().equals(ComponentType.module)) {
58+
return;
59+
}
60+
61+
final String directoryName = targetDirectory.getName();
62+
final Areas area = Areas.getAreaByString(targetDirectory.getName());
63+
if (area == null || directoryName.equals(Areas.base.toString())) {
64+
problemsHolder.registerProblem(
65+
file,
66+
inspectionBundle.message(
67+
"inspection.config.wrong.area"
68+
),
69+
errorSeverity
70+
);
71+
}
72+
}
73+
};
74+
}
75+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<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." />
2+
<?xml version="1.0"?>
3+
<!--
4+
/*
5+
* Copyright © Magento, Inc. All rights reserved.
6+
* See COPYING.txt for license details.
7+
*/
8+
-->
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
3+
use Magento\Framework\Component\ComponentRegistrar;
4+
5+
ComponentRegistrar::register(ComponentRegistrar::MODULE, 'Test_TestModule', __DIR__);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<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." />
2+
<?xml version="1.0"?>
3+
<!--
4+
/*
5+
* Copyright © Magento, Inc. All rights reserved.
6+
* See COPYING.txt for license details.
7+
*/
8+
-->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
3+
use Magento\Framework\Component\ComponentRegistrar;
4+
5+
ComponentRegistrar::register(ComponentRegistrar::MODULE, 'Test_TestModule', __DIR__);
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Copyright © Magento, Inc. All rights reserved.
3+
* See COPYING.txt for license details.
4+
*/
5+
6+
package com.magento.idea.magento2plugin.inspections.xml;
7+
8+
public class ModuleScopeInspectionTest extends InspectionXmlFixtureTestCase {
9+
10+
private static final String WRONG_AREA =
11+
"inspection.config.wrong.area";
12+
13+
@Override
14+
public void setUp() throws Exception {
15+
super.setUp();
16+
myFixture.enableInspections(ModuleScopeInspection.class);
17+
}
18+
19+
/**
20+
* Inspection highlights warning if the area of a config file is wrong.
21+
*/
22+
public void testIncorrectArea() {
23+
configureFixture("app/code/Test/TestModule/etc/adminhtmltypo/di.xml");
24+
25+
final String errorMessage = inspectionBundle.message(
26+
WRONG_AREA
27+
);
28+
29+
assertHasHighlighting(errorMessage);
30+
}
31+
32+
/**
33+
* Inspection skips warning if the area is correct.
34+
*/
35+
public void testCorrectArea() {
36+
configureFixture("app/code/Test/TestModule/etc/adminhtml/di.xml");
37+
38+
final String errorMessage = inspectionBundle.message(
39+
WRONG_AREA
40+
41+
);
42+
43+
assertHasNoHighlighting(errorMessage);
44+
}
45+
46+
private void configureFixture(final String fixturePath) {
47+
myFixture.copyFileToProject(getFixturePath("app/code/Test/TestModule/registration.php"));
48+
myFixture.configureByFile(getFixturePath(fixturePath));
49+
}
50+
}

0 commit comments

Comments
 (0)