|
| 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.PsiElementVisitor; |
| 12 | +import com.intellij.psi.PsiFile; |
| 13 | +import com.intellij.psi.XmlElementVisitor; |
| 14 | +import com.intellij.psi.xml.XmlAttribute; |
| 15 | +import com.intellij.psi.xml.XmlTag; |
| 16 | +import com.magento.idea.magento2plugin.bundles.InspectionBundle; |
| 17 | +import com.magento.idea.magento2plugin.inspections.validator.InspectionValidator; |
| 18 | +import com.magento.idea.magento2plugin.inspections.validator.NotEmptyValidator; |
| 19 | +import com.magento.idea.magento2plugin.inspections.validator.PhpClassExistenceValidator; |
| 20 | +import com.magento.idea.magento2plugin.magento.files.ModuleDiXml; |
| 21 | +import org.jetbrains.annotations.NotNull; |
| 22 | + |
| 23 | +public class InvalidVirtualTypeSourceClassInspection extends XmlSuppressableInspectionTool { |
| 24 | + |
| 25 | + @Override |
| 26 | + public @NotNull PsiElementVisitor buildVisitor( |
| 27 | + final @NotNull ProblemsHolder problemsHolder, |
| 28 | + final boolean isOnTheFly |
| 29 | + ) { |
| 30 | + return new XmlElementVisitor() { |
| 31 | + |
| 32 | + private final InspectionBundle inspectionBundle = new InspectionBundle(); |
| 33 | + private final InspectionValidator phpClassExistenceValidator = |
| 34 | + new PhpClassExistenceValidator(problemsHolder.getProject()); |
| 35 | + private final InspectionValidator notEmptyValidator = new NotEmptyValidator(); |
| 36 | + |
| 37 | + @Override |
| 38 | + public void visitXmlAttribute(final XmlAttribute attribute) { |
| 39 | + final PsiFile file = attribute.getContainingFile(); |
| 40 | + final XmlTag tag = attribute.getParent(); |
| 41 | + |
| 42 | + if (file == null |
| 43 | + || tag == null |
| 44 | + || !file.getName().equals(ModuleDiXml.FILE_NAME) |
| 45 | + || !attribute.getName().equals(ModuleDiXml.TYPE_ATTR) |
| 46 | + || !tag.getName().equals(ModuleDiXml.VIRTUAL_TYPE_TAG) |
| 47 | + ) { |
| 48 | + return; |
| 49 | + } |
| 50 | + |
| 51 | + if (attribute.getValue() == null |
| 52 | + || attribute.getValueElement() == null |
| 53 | + || attribute.getValueElement().getText().isEmpty() |
| 54 | + ) { |
| 55 | + return; |
| 56 | + } |
| 57 | + |
| 58 | + if (!notEmptyValidator.validate(attribute.getValue())) { |
| 59 | + problemsHolder.registerProblem( |
| 60 | + attribute.getValueElement(), |
| 61 | + inspectionBundle.message( |
| 62 | + "inspection.error.idAttributeCanNotBeEmpty", |
| 63 | + attribute.getName() |
| 64 | + ), |
| 65 | + ProblemHighlightType.ERROR |
| 66 | + ); |
| 67 | + } |
| 68 | + |
| 69 | + if (!phpClassExistenceValidator.validate(attribute.getValue())) { |
| 70 | + problemsHolder.registerProblem( |
| 71 | + attribute.getValueElement(), |
| 72 | + inspectionBundle.message( |
| 73 | + "inspection.warning.class.does.not.exist", |
| 74 | + attribute.getValue() |
| 75 | + ), |
| 76 | + ProblemHighlightType.WARNING |
| 77 | + ); |
| 78 | + } |
| 79 | + } |
| 80 | + }; |
| 81 | + } |
| 82 | +} |
0 commit comments