|
1 | 1 | package com.magento.idea.magento2plugin.php.inspections;
|
2 | 2 |
|
| 3 | +import com.intellij.codeInspection.ProblemHighlightType; |
| 4 | +import com.intellij.codeInspection.ProblemsHolder; |
| 5 | +import com.intellij.psi.PsiElement; |
| 6 | +import com.intellij.psi.PsiElementVisitor; |
| 7 | +import com.intellij.psi.PsiReference; |
| 8 | +import com.jetbrains.php.lang.inspections.PhpInspection; |
| 9 | +import com.jetbrains.php.lang.psi.elements.ClassReference; |
| 10 | +import com.jetbrains.php.lang.psi.elements.Method; |
| 11 | +import com.jetbrains.php.lang.psi.elements.MethodReference; |
| 12 | +import com.jetbrains.php.lang.psi.elements.PhpClass; |
| 13 | +import com.jetbrains.php.lang.psi.visitors.PhpElementVisitor; |
| 14 | +import com.magento.idea.magento2plugin.php.module.ModuleManager; |
| 15 | +import com.magento.idea.magento2plugin.php.util.MagentoTypes; |
| 16 | +import org.jetbrains.annotations.NotNull; |
| 17 | + |
3 | 18 | /**
|
4 | 19 | * Created by dkvashnin on 12/18/15.
|
5 | 20 | */
|
6 |
| -public class ObjectManagerInspection { |
| 21 | +public class ObjectManagerInspection extends PhpInspection { |
| 22 | + @NotNull |
| 23 | + @Override |
| 24 | + public PsiElementVisitor buildVisitor(ProblemsHolder problemsHolder, boolean b) { |
| 25 | + return new PhpElementVisitor() { |
| 26 | + @Override |
| 27 | + public void visitPhpMethodReference(MethodReference reference) { |
| 28 | + PsiElement referencedElement = reference.resolve(); |
| 29 | + |
| 30 | + if(referencedElement instanceof Method) { |
| 31 | + ModuleManager moduleManager = ModuleManager.getInstance(referencedElement.getProject()); |
| 32 | + if (moduleManager.getModuleForFile(reference.getContainingFile()) == null) { |
| 33 | + return; |
| 34 | + } |
| 35 | + |
| 36 | + PhpClass phpClass = ((Method) referencedElement).getContainingClass(); |
| 37 | + |
| 38 | + verifyPhpClass(phpClass, reference); |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + @Override |
| 43 | + public void visitPhpClassReference(ClassReference reference) { |
| 44 | + PsiElement referencedElement = reference.resolve(); |
| 45 | + |
| 46 | + if(referencedElement instanceof PhpClass) { |
| 47 | + ModuleManager moduleManager = ModuleManager.getInstance(referencedElement.getProject()); |
| 48 | + if (moduleManager.getModuleForFile(reference.getContainingFile()) == null) { |
| 49 | + return; |
| 50 | + } |
| 51 | + |
| 52 | + verifyPhpClass((PhpClass)referencedElement, reference); |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + private void verifyPhpClass(PhpClass phpClass, PsiReference reference) { |
| 57 | + if (phpClass == null) { |
| 58 | + return; |
| 59 | + } |
| 60 | + |
| 61 | + if (phpClass.getPresentableFQN().equals(MagentoTypes.OBJECT_MANAGER_TYPE)) { |
| 62 | + registerProblem(reference); |
| 63 | + return; |
| 64 | + } |
| 65 | + |
| 66 | + for (PhpClass implementedInterface : phpClass.getImplementedInterfaces()) { |
| 67 | + if (implementedInterface.getPresentableFQN().equals(MagentoTypes.OBJECT_MANAGER_TYPE)) { |
| 68 | + registerProblem(reference); |
| 69 | + return; |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + private void registerProblem(PsiReference reference) { |
| 75 | + problemsHolder.registerProblem(reference, "ObjectManager is not recommended to use in module", ProblemHighlightType.GENERIC_ERROR_OR_WARNING); |
| 76 | + } |
| 77 | + }; |
| 78 | + } |
7 | 79 | }
|
0 commit comments