|
17 | 17 | import fr.adrienbrault.idea.symfony2plugin.util.yaml.YamlHelper;
|
18 | 18 | import org.apache.commons.lang3.StringUtils;
|
19 | 19 | import org.jetbrains.annotations.NotNull;
|
20 |
| -import org.jetbrains.yaml.YAMLLanguage; |
21 | 20 |
|
22 | 21 | /**
|
23 | 22 | * @author Daniel Espendiller <daniel@espendiller.net>
|
24 | 23 | */
|
25 |
| -public class MissingServiceInspection extends LocalInspectionTool { |
| 24 | +public class MissingServiceInspection { |
26 | 25 |
|
27 | 26 | public static final String INSPECTION_MESSAGE = "Symfony: Missing Service";
|
28 | 27 |
|
29 |
| - @NotNull |
30 |
| - public PsiElementVisitor buildVisitor(final @NotNull ProblemsHolder holder, boolean isOnTheFly) { |
31 |
| - if (!Symfony2ProjectComponent.isEnabled(holder.getProject())) { |
32 |
| - return super.buildVisitor(holder, isOnTheFly); |
33 |
| - } |
| 28 | + public static class PhpLocalInspectionTool extends LocalInspectionTool { |
| 29 | + @Override |
| 30 | + public @NotNull PsiElementVisitor buildVisitor(@NotNull ProblemsHolder holder, boolean isOnTheFly) { |
| 31 | + if (!Symfony2ProjectComponent.isEnabled(holder.getProject())) { |
| 32 | + return super.buildVisitor(holder, isOnTheFly); |
| 33 | + } |
34 | 34 |
|
35 |
| - return new MyPsiElementVisitor(holder); |
36 |
| - } |
| 35 | + return new PsiElementVisitor() { |
| 36 | + private ContainerCollectionResolver.LazyServiceCollector lazyServiceCollector; |
| 37 | + |
| 38 | + @Override |
| 39 | + public void visitElement(@NotNull PsiElement element) { |
| 40 | + if(element.getLanguage() == PhpLanguage.INSTANCE && element instanceof StringLiteralExpression) { |
| 41 | + // PHP |
| 42 | + MethodReference methodReference = PsiElementUtils.getMethodReferenceWithFirstStringParameter((StringLiteralExpression) element); |
| 43 | + if (methodReference != null && PhpElementsUtil.isMethodReferenceInstanceOf(methodReference, ServiceContainerUtil.SERVICE_GET_SIGNATURES)) { |
| 44 | + String serviceName = PhpElementsUtil.getFirstArgumentStringValue(methodReference); |
| 45 | + if (StringUtils.isNotBlank(serviceName) && !hasService(serviceName)) { |
| 46 | + holder.registerProblem(element, INSPECTION_MESSAGE, ProblemHighlightType.GENERIC_ERROR_OR_WARNING); |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + // #[Autowire(service: 'foobar')] |
| 51 | + PsiElement leafText = PsiElementUtils.getTextLeafElementFromStringLiteralExpression((StringLiteralExpression) element); |
| 52 | + |
| 53 | + boolean isAttributeLeaf = leafText != null && ( |
| 54 | + PhpElementsUtil.getAttributeNamedArgumentStringPattern(ServiceContainerUtil.AUTOWIRE_ATTRIBUTE_CLASS, "service").accepts(leafText) |
| 55 | + || PhpElementsUtil.getFirstAttributeStringPattern(ServiceContainerUtil.DECORATOR_ATTRIBUTE_CLASS).accepts(leafText) |
| 56 | + ); |
| 57 | + |
| 58 | + if (isAttributeLeaf) { |
| 59 | + String serviceName = ((StringLiteralExpression) element).getContents(); |
| 60 | + if (StringUtils.isNotBlank(serviceName) && !hasService(serviceName)) { |
| 61 | + holder.registerProblem(element, INSPECTION_MESSAGE, ProblemHighlightType.GENERIC_ERROR_OR_WARNING); |
| 62 | + } |
| 63 | + } |
37 | 64 |
|
38 |
| - private static class MyPsiElementVisitor extends PsiElementVisitor { |
39 |
| - private final ProblemsHolder holder; |
40 |
| - private ContainerCollectionResolver.LazyServiceCollector lazyServiceCollector; |
| 65 | + } |
41 | 66 |
|
42 |
| - MyPsiElementVisitor(@NotNull ProblemsHolder holder) { |
43 |
| - this.holder = holder; |
44 |
| - } |
| 67 | + super.visitElement(element); |
| 68 | + } |
45 | 69 |
|
46 |
| - @Override |
47 |
| - public void visitElement(PsiElement element) { |
48 |
| - if(element.getLanguage() == PhpLanguage.INSTANCE && element instanceof StringLiteralExpression) { |
49 |
| - // PHP |
50 |
| - MethodReference methodReference = PsiElementUtils.getMethodReferenceWithFirstStringParameter((StringLiteralExpression) element); |
51 |
| - if (methodReference != null && PhpElementsUtil.isMethodReferenceInstanceOf(methodReference, ServiceContainerUtil.SERVICE_GET_SIGNATURES)) { |
52 |
| - String serviceName = PhpElementsUtil.getFirstArgumentStringValue(methodReference); |
53 |
| - if (StringUtils.isNotBlank(serviceName) && !hasService(serviceName)) { |
54 |
| - holder.registerProblem(element, INSPECTION_MESSAGE, ProblemHighlightType.GENERIC_ERROR_OR_WARNING); |
| 70 | + private boolean hasService(@NotNull String serviceName) { |
| 71 | + if (this.lazyServiceCollector == null) { |
| 72 | + this.lazyServiceCollector = new ContainerCollectionResolver.LazyServiceCollector(holder.getProject()); |
55 | 73 | }
|
| 74 | + |
| 75 | + return ContainerCollectionResolver.hasServiceName(lazyServiceCollector, serviceName); |
56 | 76 | }
|
| 77 | + }; |
57 | 78 |
|
58 |
| - // #[Autowire(service: 'foobar')] |
59 |
| - PsiElement leafText = PsiElementUtils.getTextLeafElementFromStringLiteralExpression((StringLiteralExpression) element); |
| 79 | + } |
60 | 80 |
|
61 |
| - boolean isAttributeLeaf = leafText != null && ( |
62 |
| - PhpElementsUtil.getAttributeNamedArgumentStringPattern(ServiceContainerUtil.AUTOWIRE_ATTRIBUTE_CLASS, "service").accepts(leafText) |
63 |
| - || PhpElementsUtil.getFirstAttributeStringPattern(ServiceContainerUtil.DECORATOR_ATTRIBUTE_CLASS).accepts(leafText) |
64 |
| - ); |
| 81 | + } |
65 | 82 |
|
66 |
| - if (isAttributeLeaf) { |
67 |
| - String serviceName = ((StringLiteralExpression) element).getContents(); |
68 |
| - if (StringUtils.isNotBlank(serviceName) && !hasService(serviceName)) { |
69 |
| - holder.registerProblem(element, INSPECTION_MESSAGE, ProblemHighlightType.GENERIC_ERROR_OR_WARNING); |
70 |
| - } |
71 |
| - } |
72 | 83 |
|
73 |
| - } else if(element.getLanguage() == YAMLLanguage.INSTANCE) { |
74 |
| - // yaml |
| 84 | + public static class YamlLocalInspectionTool extends LocalInspectionTool { |
| 85 | + @Override |
| 86 | + public @NotNull PsiElementVisitor buildVisitor(@NotNull ProblemsHolder holder, boolean isOnTheFly) { |
| 87 | + if (!Symfony2ProjectComponent.isEnabled(holder.getProject())) { |
| 88 | + return super.buildVisitor(holder, isOnTheFly); |
| 89 | + } |
| 90 | + |
| 91 | + return new PsiElementVisitor() { |
| 92 | + private ContainerCollectionResolver.LazyServiceCollector lazyServiceCollector; |
75 | 93 |
|
76 |
| - if (YamlElementPatternHelper.getServiceDefinition().accepts(element) && YamlElementPatternHelper.getInsideServiceKeyPattern().accepts(element)) { |
77 |
| - String serviceName = YamlHelper.trimSpecialSyntaxServiceName(PsiElementUtils.getText(element)); |
| 94 | + @Override |
| 95 | + public void visitElement(@NotNull PsiElement element) { |
| 96 | + if (YamlElementPatternHelper.getServiceDefinition().accepts(element) && YamlElementPatternHelper.getInsideServiceKeyPattern().accepts(element)) { |
| 97 | + String serviceName = YamlHelper.trimSpecialSyntaxServiceName(PsiElementUtils.getText(element)); |
78 | 98 |
|
79 |
| - // dont mark "@", "@?", "@@" escaping and expressions |
80 |
| - if (serviceName.length() > 2 && !serviceName.startsWith("=") && !serviceName.startsWith("@") && !hasService(serviceName)) { |
81 |
| - holder.registerProblem(element, INSPECTION_MESSAGE, ProblemHighlightType.GENERIC_ERROR_OR_WARNING); |
| 99 | + // dont mark "@", "@?", "@@" escaping and expressions |
| 100 | + if (serviceName.length() > 2 && !serviceName.startsWith("=") && !serviceName.startsWith("@") && !hasService(serviceName)) { |
| 101 | + holder.registerProblem(element, INSPECTION_MESSAGE, ProblemHighlightType.GENERIC_ERROR_OR_WARNING); |
| 102 | + } |
82 | 103 | }
|
83 |
| - } |
84 |
| - } |
85 | 104 |
|
86 |
| - super.visitElement(element); |
87 |
| - } |
| 105 | + super.visitElement(element); |
| 106 | + } |
88 | 107 |
|
89 |
| - private boolean hasService(@NotNull String serviceName) { |
90 |
| - if (this.lazyServiceCollector == null) { |
91 |
| - this.lazyServiceCollector = new ContainerCollectionResolver.LazyServiceCollector(holder.getProject()); |
92 |
| - } |
| 108 | + private boolean hasService(@NotNull String serviceName) { |
| 109 | + if (this.lazyServiceCollector == null) { |
| 110 | + this.lazyServiceCollector = new ContainerCollectionResolver.LazyServiceCollector(holder.getProject()); |
| 111 | + } |
93 | 112 |
|
94 |
| - return ContainerCollectionResolver.hasServiceName(lazyServiceCollector, serviceName); |
| 113 | + return ContainerCollectionResolver.hasServiceName(lazyServiceCollector, serviceName); |
| 114 | + } |
| 115 | + }; |
95 | 116 | }
|
96 | 117 | }
|
97 | 118 | }
|
0 commit comments