|
| 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