|
| 1 | +package fr.adrienbrault.idea.symfony2plugin.templating.inspection; |
| 2 | + |
| 3 | +import com.intellij.codeInspection.LocalInspectionTool; |
| 4 | +import com.intellij.codeInspection.LocalQuickFix; |
| 5 | +import com.intellij.codeInspection.ProblemsHolder; |
| 6 | +import com.intellij.psi.PsiElement; |
| 7 | +import com.intellij.psi.PsiElementVisitor; |
| 8 | +import com.intellij.psi.util.PsiTreeUtil; |
| 9 | +import com.intellij.util.containers.ContainerUtil; |
| 10 | +import com.jetbrains.php.lang.documentation.phpdoc.psi.PhpDocComment; |
| 11 | +import com.jetbrains.php.lang.documentation.phpdoc.psi.tags.PhpDocTag; |
| 12 | +import com.jetbrains.php.lang.psi.elements.Method; |
| 13 | +import com.jetbrains.php.lang.psi.elements.PhpAttribute; |
| 14 | +import com.jetbrains.php.lang.psi.elements.PhpAttributesList; |
| 15 | +import com.jetbrains.php.lang.psi.elements.PhpPsiElement; |
| 16 | +import de.espend.idea.php.annotation.dict.PhpDocTagAnnotation; |
| 17 | +import de.espend.idea.php.annotation.util.AnnotationUtil; |
| 18 | +import fr.adrienbrault.idea.symfony2plugin.Symfony2ProjectComponent; |
| 19 | +import fr.adrienbrault.idea.symfony2plugin.templating.util.TwigUtil; |
| 20 | +import fr.adrienbrault.idea.symfony2plugin.util.PhpElementsUtil; |
| 21 | +import fr.adrienbrault.idea.symfony2plugin.util.PhpPsiAttributesUtil; |
| 22 | +import org.apache.commons.lang.StringUtils; |
| 23 | +import org.jetbrains.annotations.NotNull; |
| 24 | +import org.jetbrains.annotations.Nullable; |
| 25 | + |
| 26 | +import java.util.ArrayList; |
| 27 | +import java.util.Arrays; |
| 28 | +import java.util.Collection; |
| 29 | +import java.util.HashSet; |
| 30 | + |
| 31 | +/** |
| 32 | + * @author Daniel Espendiller <[email protected]> |
| 33 | + */ |
| 34 | +public class TemplateExistsAnnotationPhpAttributeLocalInspection extends LocalInspectionTool { |
| 35 | + @NotNull |
| 36 | + public PsiElementVisitor buildVisitor(final @NotNull ProblemsHolder holder, boolean isOnTheFly) { |
| 37 | + return new PsiElementVisitor() { |
| 38 | + @Override |
| 39 | + public void visitElement(@NotNull PsiElement element) { |
| 40 | + if (element instanceof PhpDocTag) { |
| 41 | + annotate((PhpDocTag) element, holder); |
| 42 | + } |
| 43 | + |
| 44 | + if (element instanceof PhpAttribute) { |
| 45 | + String fqn = ((PhpAttribute) element).getFQN(); |
| 46 | + if (fqn != null && PhpElementsUtil.isInstanceOf(element.getProject(), fqn, TwigUtil.TEMPLATE_ANNOTATION_CLASS)) { |
| 47 | + annotate((PhpAttribute) element, holder); |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + super.visitElement(element); |
| 52 | + } |
| 53 | + }; |
| 54 | + } |
| 55 | + |
| 56 | + private void annotate(@NotNull PhpAttribute phpAttribute, @NotNull ProblemsHolder holder) { |
| 57 | + Collection<String> templateNames = new HashSet<>(); |
| 58 | + |
| 59 | + if (phpAttribute.getArguments().isEmpty()) { |
| 60 | + PsiElement phpAttributesList = phpAttribute.getParent(); |
| 61 | + if (phpAttributesList instanceof PhpAttributesList) { |
| 62 | + PsiElement method = phpAttributesList.getParent(); |
| 63 | + if (method instanceof Method) { |
| 64 | + templateNames.addAll(Arrays.asList(TwigUtil.getControllerMethodShortcut((Method) method))); |
| 65 | + } |
| 66 | + } |
| 67 | + } else { |
| 68 | + String attributeDefaultValue = PhpPsiAttributesUtil.getAttributeValueByNameAsStringWithDefaultParameterFallback(phpAttribute, "template"); |
| 69 | + if (attributeDefaultValue != null) { |
| 70 | + templateNames.add(attributeDefaultValue); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + if(!templateNames.isEmpty()) { |
| 75 | + extracted(phpAttribute, holder, templateNames); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + private void annotate(@NotNull PhpDocTag phpDocTag, @NotNull ProblemsHolder holder) { |
| 80 | + if(!Symfony2ProjectComponent.isEnabled(phpDocTag.getProject())) { |
| 81 | + return; |
| 82 | + } |
| 83 | + |
| 84 | + PhpDocTagAnnotation phpDocAnnotationContainer = AnnotationUtil.getPhpDocAnnotationContainer(phpDocTag); |
| 85 | + if (phpDocAnnotationContainer == null || !PhpElementsUtil.isEqualClassName(phpDocAnnotationContainer.getPhpClass(), TwigUtil.TEMPLATE_ANNOTATION_CLASS)) { |
| 86 | + return; |
| 87 | + } |
| 88 | + |
| 89 | + PhpPsiElement phpDocAttrList = phpDocTag.getFirstPsiChild(); |
| 90 | + if(phpDocAttrList == null) { |
| 91 | + return; |
| 92 | + } |
| 93 | + |
| 94 | + Collection<String> templateNames = new HashSet<>(); |
| 95 | + |
| 96 | + @Nullable String matcher = AnnotationUtil.getPropertyValueOrDefault(phpDocTag, "template"); |
| 97 | + if (matcher != null) { |
| 98 | + templateNames.add(matcher); |
| 99 | + } else { |
| 100 | + |
| 101 | + // find template name on last method |
| 102 | + PhpDocComment docComment = PsiTreeUtil.getParentOfType(phpDocTag, PhpDocComment.class); |
| 103 | + if(null == docComment) { |
| 104 | + return; |
| 105 | + } |
| 106 | + Method method = PsiTreeUtil.getNextSiblingOfType(docComment, Method.class); |
| 107 | + if(null == method) { |
| 108 | + return; |
| 109 | + } |
| 110 | + |
| 111 | + templateNames.addAll(Arrays.asList(TwigUtil.getControllerMethodShortcut(method))); |
| 112 | + } |
| 113 | + |
| 114 | + if(!templateNames.isEmpty()) { |
| 115 | + extracted(phpDocTag, holder, templateNames); |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + private void extracted(@NotNull PsiElement target, @NotNull ProblemsHolder holder, @NotNull Collection<String> templateNames) { |
| 120 | + if(templateNames.size() == 0) { |
| 121 | + return; |
| 122 | + } |
| 123 | + |
| 124 | + for (String templateName : templateNames) { |
| 125 | + if (TwigUtil.getTemplateFiles(holder.getProject(), templateName).size() > 0) { |
| 126 | + return; |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + // find html target, as this this our first priority for end users condition |
| 131 | + String templateName = ContainerUtil.find(templateNames, s -> s.toLowerCase().endsWith(".html.twig")); |
| 132 | + |
| 133 | + // fallback on first item |
| 134 | + if(templateName == null) { |
| 135 | + templateName = templateNames.iterator().next(); |
| 136 | + } |
| 137 | + |
| 138 | + Collection<LocalQuickFix> quickFixes = new ArrayList<>(); |
| 139 | + quickFixes.add(new TemplateCreateByNameLocalQuickFix(templateName)); |
| 140 | + |
| 141 | + if (StringUtils.isNotBlank(templateName)) { |
| 142 | + quickFixes.add(new TemplateGuessTypoQuickFix(templateName)); |
| 143 | + } |
| 144 | + |
| 145 | + holder.registerProblem(target, "Twig: Missing Template", quickFixes.toArray(new LocalQuickFix[0])); |
| 146 | + } |
| 147 | +} |
0 commit comments