-
-
Notifications
You must be signed in to change notification settings - Fork 142
Expand file tree
/
Copy pathServiceNamedArgumentExistsInspection.java
More file actions
80 lines (67 loc) · 3.43 KB
/
ServiceNamedArgumentExistsInspection.java
File metadata and controls
80 lines (67 loc) · 3.43 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
package fr.adrienbrault.idea.symfony2plugin.dic.inspection;
import com.intellij.codeInspection.LocalInspectionTool;
import com.intellij.codeInspection.ProblemHighlightType;
import com.intellij.patterns.ElementPattern;
import com.intellij.codeInspection.ProblemsHolder;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiElementVisitor;
import fr.adrienbrault.idea.symfony2plugin.Symfony2ProjectComponent;
import fr.adrienbrault.idea.symfony2plugin.config.yaml.YamlElementPatternHelper;
import fr.adrienbrault.idea.symfony2plugin.dic.container.util.ServiceContainerUtil;
import fr.adrienbrault.idea.symfony2plugin.stubs.ContainerCollectionResolver;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.yaml.psi.YAMLKeyValue;
import org.jetbrains.yaml.psi.YAMLMapping;
/**
* @author Daniel Espendiller <daniel@espendiller.net>
*/
public class ServiceNamedArgumentExistsInspection extends LocalInspectionTool {
public static final String INSPECTION_MESSAGE = "Symfony: named argument does not exists";
@NotNull
public PsiElementVisitor buildVisitor(final @NotNull ProblemsHolder holder, boolean isOnTheFly) {
if (!Symfony2ProjectComponent.isEnabled(holder.getProject())) {
return super.buildVisitor(holder, isOnTheFly);
}
return new MyPsiElementVisitor(holder);
}
private static class MyPsiElementVisitor extends PsiElementVisitor {
@NotNull private final ProblemsHolder holder;
private ContainerCollectionResolver.LazyServiceCollector lazyServiceCollector;
private ElementPattern<?> namedArgumentPattern;
MyPsiElementVisitor(@NotNull ProblemsHolder holder) {
this.holder = holder;
}
@Override
public void visitElement(@NotNull PsiElement element) {
if (getNamedArgumentPattern().accepts(element)) {
if (isSupportedDefinition(element) && ServiceContainerUtil.hasMissingYamlNamedArgumentForInspection(element, getLazyServiceCollector())) {
holder.registerProblem(element, INSPECTION_MESSAGE, ProblemHighlightType.GENERIC_ERROR_OR_WARNING);
}
}
super.visitElement(element);
}
private ContainerCollectionResolver.LazyServiceCollector getLazyServiceCollector() {
return lazyServiceCollector != null ? lazyServiceCollector : (lazyServiceCollector = new ContainerCollectionResolver.LazyServiceCollector(holder.getProject()));
}
private ElementPattern<?> getNamedArgumentPattern() {
return namedArgumentPattern != null ? namedArgumentPattern : (namedArgumentPattern = YamlElementPatternHelper.getNamedArgumentPattern());
}
}
private static boolean isSupportedDefinition(@NotNull PsiElement element) {
PsiElement context = element.getContext();
if (context instanceof YAMLKeyValue) {
// arguments: ['$foobar': '@foo']
PsiElement yamlMapping = context.getParent();
if (yamlMapping instanceof YAMLMapping) {
PsiElement yamlKeyValue = yamlMapping.getParent();
if (yamlKeyValue instanceof YAMLKeyValue) {
YAMLMapping parentMapping = ((YAMLKeyValue) yamlKeyValue).getParentMapping();
if (parentMapping != null) {
return parentMapping.getKeyValueByKey("factory") == null;
}
}
}
}
return true;
}
}