Skip to content

Commit e820c40

Browse files
committed
Config Scope inspection implementation
1 parent 0be7719 commit e820c40

File tree

4 files changed

+112
-0
lines changed

4 files changed

+112
-0
lines changed

resources/META-INF/plugin.xml

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

317+
<localInspection language="XML" groupPath="XML"
318+
shortName="ModuleScopeInspection"
319+
bundle="magento2.inspection" key="inspection.displayName.ModuleScopeInspection"
320+
groupBundle="magento2.inspection" groupKey="inspection.group.name"
321+
enabledByDefault="true" level="WARNING"
322+
implementationClass="com.magento.idea.magento2plugin.inspections.xml.ModuleScopeInspection"/>
323+
317324
<!-- UCT inspection -->
318325
<localInspection language="PHP" groupPath="UCT"
319326
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 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.
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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.openapi.vfs.VirtualFile;
11+
import com.intellij.psi.*;
12+
import com.magento.idea.magento2plugin.bundles.InspectionBundle;
13+
import com.magento.idea.magento2plugin.magento.files.ModuleEventsXml;
14+
import com.magento.idea.magento2plugin.magento.packages.Areas;
15+
import com.jetbrains.php.lang.inspections.PhpInspection;
16+
import com.magento.idea.magento2plugin.magento.packages.ComponentType;
17+
import com.magento.idea.magento2plugin.magento.packages.Package;
18+
import com.magento.idea.magento2plugin.util.magento.GetMagentoModuleUtil;
19+
import org.jetbrains.annotations.NotNull;
20+
import java.util.*;
21+
22+
public class ModuleScopeInspection extends PhpInspection {
23+
24+
@NotNull
25+
@SuppressWarnings({"PMD.AvoidInstantiatingObjectsInLoops", "PMD.CognitiveComplexity"})
26+
public PsiElementVisitor buildVisitor(
27+
final @NotNull ProblemsHolder problemsHolder,
28+
final boolean isOnTheFly
29+
) {
30+
return new XmlElementVisitor() {
31+
private final HashMap<String, VirtualFile> loadedFileHash = new HashMap<>();//NOPMD
32+
private final InspectionBundle inspectionBundle = new InspectionBundle();
33+
private final ProblemHighlightType errorSeverity = ProblemHighlightType.WARNING;
34+
35+
@Override
36+
public void visitFile(final @NotNull PsiFile file) {
37+
if (!ModuleEventsXml.FILE_NAME.equals(file.getName())) {
38+
return;
39+
}
40+
41+
final PsiDirectory targetDirectory = file.getParent();
42+
if (targetDirectory == null) {
43+
return;
44+
}
45+
46+
final PsiDirectory parentDirectory = targetDirectory.getParent();
47+
if (parentDirectory == null) {
48+
return;
49+
}
50+
51+
if (!parentDirectory.getName().equals(Package.moduleBaseAreaDir)) {
52+
return;
53+
}
54+
55+
final GetMagentoModuleUtil.MagentoModuleData moduleData = GetMagentoModuleUtil
56+
.getByContext(targetDirectory, file.getProject());
57+
58+
if (moduleData == null || moduleData.getType() == null || !moduleData.getType().equals(ComponentType.module)) {
59+
return;
60+
}
61+
62+
final String directoryName = targetDirectory.getName();
63+
final Areas area = Areas.getAreaByString(targetDirectory.getName());
64+
if (area == null || directoryName.equals(Areas.base)) {
65+
problemsHolder.registerProblem(
66+
file,
67+
inspectionBundle.message(
68+
"inspection.config.wrong.area"
69+
),
70+
errorSeverity
71+
);
72+
}
73+
}
74+
};
75+
}
76+
}

0 commit comments

Comments
 (0)