-
-
Notifications
You must be signed in to change notification settings - Fork 142
Expand file tree
/
Copy pathPhpTemplateMissingInspection.java
More file actions
97 lines (84 loc) · 3.83 KB
/
PhpTemplateMissingInspection.java
File metadata and controls
97 lines (84 loc) · 3.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package fr.adrienbrault.idea.symfony2plugin.templating;
import com.intellij.codeInspection.LocalInspectionTool;
import com.intellij.codeInspection.LocalQuickFix;
import com.intellij.codeInspection.ProblemHighlightType;
import com.intellij.codeInspection.ProblemsHolder;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiElementVisitor;
import com.jetbrains.php.lang.psi.elements.MethodReference;
import com.jetbrains.php.lang.psi.elements.ParameterList;
import com.jetbrains.php.lang.psi.elements.StringLiteralExpression;
import fr.adrienbrault.idea.symfony2plugin.Symfony2ProjectComponent;
import fr.adrienbrault.idea.symfony2plugin.config.SymfonyPhpReferenceContributor;
import fr.adrienbrault.idea.symfony2plugin.templating.inspection.TemplateCreateByNameLocalQuickFix;
import fr.adrienbrault.idea.symfony2plugin.templating.inspection.TemplateGuessTypoQuickFix;
import fr.adrienbrault.idea.symfony2plugin.templating.util.TwigUtil;
import fr.adrienbrault.idea.symfony2plugin.util.ParameterBag;
import fr.adrienbrault.idea.symfony2plugin.util.PhpElementsUtil;
import fr.adrienbrault.idea.symfony2plugin.util.PsiElementUtils;
import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
/**
* @author Daniel Espendiller <daniel@espendiller.net>
*/
public class PhpTemplateMissingInspection extends LocalInspectionTool {
@NotNull
@Override
public PsiElementVisitor buildVisitor(@NotNull ProblemsHolder holder, boolean isOnTheFly) {
if(!Symfony2ProjectComponent.isEnabled(holder.getProject())) {
return super.buildVisitor(holder, isOnTheFly);
}
return new PsiElementVisitor() {
@Override
public void visitElement(@NotNull PsiElement element) {
if (element instanceof StringLiteralExpression stringLiteralExpression) {
invoke(holder, stringLiteralExpression);
}
super.visitElement(element);
}
};
}
private void invoke(@NotNull ProblemsHolder holder, @NotNull StringLiteralExpression psiElement) {
String templateNameIfMissing = getTemplateNameIfMissing(psiElement);
if(templateNameIfMissing == null) {
return;
}
LocalQuickFix[] templateCreateByNameLocalQuickFix = new LocalQuickFix[]{
new TemplateCreateByNameLocalQuickFix(templateNameIfMissing),
new TemplateGuessTypoQuickFix(templateNameIfMissing)
};
holder.registerProblem(
psiElement,
"Twig: Missing Template",
ProblemHighlightType.GENERIC_ERROR_OR_WARNING,
templateCreateByNameLocalQuickFix
);
}
@Nullable
private String getTemplateNameIfMissing(@NotNull StringLiteralExpression psiElement) {
ParameterBag parameterBag = PsiElementUtils.getCurrentParameterIndex(psiElement);
if(parameterBag == null || parameterBag.getIndex() != 0) {
return null;
}
PsiElement parameterList = psiElement.getParent();
if (!(parameterList instanceof ParameterList)) {
return null;
}
PsiElement methodReference = parameterList.getParent();
if (!(methodReference instanceof MethodReference)) {
return null;
}
if (!PhpElementsUtil.isMethodReferenceInstanceOf((MethodReference) methodReference, SymfonyPhpReferenceContributor.TEMPLATE_SIGNATURES)) {
return null;
}
String templateName = PhpElementsUtil.getFirstArgumentStringValue((MethodReference) methodReference);
if(templateName == null || StringUtils.isBlank(templateName)) {
return null;
}
if(!TwigUtil.getTemplateFiles(psiElement.getProject(), templateName).isEmpty()) {
return null;
}
return templateName;
}
}