|
| 1 | +/* |
| 2 | + * Copyright © Magento, Inc. All rights reserved. |
| 3 | + * See COPYING.txt for license details. |
| 4 | + */ |
| 5 | + |
| 6 | +package com.magento.idea.magento2plugin.inspections.graphqls; |
| 7 | + |
| 8 | +import com.intellij.codeInspection.LocalInspectionTool; |
| 9 | +import com.intellij.codeInspection.ProblemHighlightType; |
| 10 | +import com.intellij.codeInspection.ProblemsHolder; |
| 11 | +import com.intellij.lang.jsgraphql.psi.GraphQLArgument; |
| 12 | +import com.intellij.lang.jsgraphql.psi.GraphQLArguments; |
| 13 | +import com.intellij.psi.PsiElement; |
| 14 | +import com.intellij.psi.PsiElementVisitor; |
| 15 | +import com.jetbrains.php.lang.psi.elements.PhpClass; |
| 16 | +import com.magento.idea.magento2plugin.util.GetPhpClassByFQN; |
| 17 | +import com.magento.idea.magento2plugin.util.magento.graphql.GraphQlUtil; |
| 18 | +import org.jetbrains.annotations.NotNull; |
| 19 | + |
| 20 | +public class SchemaResolverInspection extends LocalInspectionTool { |
| 21 | + |
| 22 | + public static final String GraphQlResolverProblemDescription = "Class must implements \\Magento\\Framework\\GraphQl\\Query\\ResolverInterface"; |
| 23 | + |
| 24 | + @NotNull |
| 25 | + @Override |
| 26 | + public PsiElementVisitor buildVisitor(@NotNull final ProblemsHolder holder, boolean isOnTheFly) { |
| 27 | + return new PsiElementVisitor() { |
| 28 | + @Override |
| 29 | + public void visitElement(PsiElement element) { |
| 30 | + PsiElement[] directiveChildren = element.getChildren(); |
| 31 | + |
| 32 | + for (PsiElement directiveChild : directiveChildren) { |
| 33 | + if (!(directiveChild instanceof GraphQLArguments)) { |
| 34 | + continue; |
| 35 | + } |
| 36 | + |
| 37 | + PsiElement[] argumentsChildren = directiveChild.getChildren(); |
| 38 | + for (PsiElement argumentsChild : argumentsChildren) { |
| 39 | + if (!(argumentsChild instanceof GraphQLArgument)) { |
| 40 | + continue; |
| 41 | + } |
| 42 | + |
| 43 | + PsiElement argumentStringValue = GraphQlUtil.fetchResolverQuotedStringFromArgument(argumentsChild); |
| 44 | + if (argumentStringValue == null) continue; |
| 45 | + |
| 46 | + String resolverFQN = argumentStringValue.getText(); |
| 47 | + if (resolverFQN == null) { |
| 48 | + continue; |
| 49 | + } |
| 50 | + |
| 51 | + resolverFQN = GraphQlUtil.resolverStringToPhpFQN(resolverFQN); |
| 52 | + GetPhpClassByFQN getPhpClassByFQN = GetPhpClassByFQN.getInstance(holder.getProject()); |
| 53 | + PhpClass resolverClass = getPhpClassByFQN.execute(resolverFQN); |
| 54 | + if (resolverClass == null) { |
| 55 | + continue; |
| 56 | + } |
| 57 | + if (GraphQlUtil.isNotResolver(resolverClass)) { |
| 58 | + holder.registerProblem(argumentStringValue, |
| 59 | + GraphQlResolverProblemDescription, |
| 60 | + ProblemHighlightType.ERROR); |
| 61 | + } |
| 62 | + } |
| 63 | + } |
| 64 | + } |
| 65 | + }; |
| 66 | + } |
| 67 | +} |
0 commit comments