Skip to content

Commit 53ee1ef

Browse files
committed
missing service argument inspection should only visit the working context
1 parent 9f0218e commit 53ee1ef

File tree

3 files changed

+16
-46
lines changed

3 files changed

+16
-46
lines changed

src/main/java/fr/adrienbrault/idea/symfony2plugin/action/ServiceActionUtil.java

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -165,28 +165,6 @@ public static Set<String> getPossibleServices(@NotNull PhpClass phpClass, @NotNu
165165
.collect(Collectors.toCollection(LinkedHashSet::new));
166166
}
167167

168-
@NotNull
169-
public static Collection<XmlTag> getXmlContainerServiceDefinition(PsiFile psiFile) {
170-
171-
Collection<XmlTag> xmlTags = new ArrayList<>();
172-
173-
for(XmlTag xmlTag: PsiTreeUtil.getChildrenOfTypeAsList(psiFile.getFirstChild(), XmlTag.class)) {
174-
if(xmlTag.getName().equals("container")) {
175-
for(XmlTag servicesTag: xmlTag.getSubTags()) {
176-
if(servicesTag.getName().equals("services")) {
177-
for(XmlTag parameterTag: servicesTag.getSubTags()) {
178-
if(parameterTag.getName().equals("service")) {
179-
xmlTags.add(parameterTag);
180-
}
181-
}
182-
}
183-
}
184-
}
185-
}
186-
187-
return xmlTags;
188-
}
189-
190168
public static class ServiceYamlContainer {
191169

192170
@NotNull

src/main/java/fr/adrienbrault/idea/symfony2plugin/codeInspection/service/ServiceDeprecatedClassesInspection.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,6 @@ public PsiElementVisitor buildVisitor(final @NotNull ProblemsHolder holder, bool
4545
}
4646

4747
private static class ProblemRegistrar {
48-
49-
private ContainerCollectionResolver.LazyServiceCollector lazyServiceCollector;
50-
5148
public static void attachDeprecatedProblem(@NotNull PsiElement element, @NotNull String text, @NotNull ProblemsHolder holder, @NotNull NotNullLazyValue<ContainerCollectionResolver.LazyServiceCollector> lazyServiceCollector) {
5249
PhpClass phpClass = ServiceUtil.getResolvedClassDefinition(element.getProject(), text, lazyServiceCollector.get());
5350
if(phpClass == null) {
@@ -58,7 +55,6 @@ public static void attachDeprecatedProblem(@NotNull PsiElement element, @NotNull
5855
if(docComment != null && docComment.getTagElementsByName("@deprecated").length > 0) {
5956
holder.registerProblem(element, String.format("Class '%s' is deprecated", phpClass.getName()), ProblemHighlightType.LIKE_DEPRECATED);
6057
}
61-
6258
}
6359

6460
public static void attachServiceDeprecatedProblem(@NotNull PsiElement element, @NotNull String serviceName, @NotNull ProblemsHolder holder, @NotNull NotNullLazyValue<ContainerCollectionResolver.LazyServiceCollector> lazyServiceCollector) {

src/main/java/fr/adrienbrault/idea/symfony2plugin/config/xml/inspection/XmlServiceArgumentInspection.java

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
import com.intellij.codeInspection.LocalInspectionTool;
44
import com.intellij.codeInspection.ProblemHighlightType;
55
import com.intellij.codeInspection.ProblemsHolder;
6+
import com.intellij.openapi.util.NotNullLazyValue;
67
import com.intellij.patterns.PlatformPatterns;
78
import com.intellij.psi.PsiElement;
89
import com.intellij.psi.PsiElementVisitor;
9-
import com.intellij.psi.PsiFile;
1010
import com.intellij.psi.xml.XmlTag;
1111
import com.intellij.psi.xml.XmlTokenType;
1212
import fr.adrienbrault.idea.symfony2plugin.Symfony2ProjectComponent;
@@ -17,7 +17,6 @@
1717
import org.jetbrains.annotations.NotNull;
1818

1919
import java.util.List;
20-
import java.util.Objects;
2120

2221
/**
2322
* @author Daniel Espendiller <daniel@espendiller.net>
@@ -35,39 +34,28 @@ public PsiElementVisitor buildVisitor(final @NotNull ProblemsHolder holder, bool
3534
}
3635

3736
private static class MyPsiElementVisitor extends PsiElementVisitor {
38-
3937
private final ProblemsHolder holder;
40-
private ContainerCollectionResolver.LazyServiceCollector lazyServiceCollector;
38+
private NotNullLazyValue<ContainerCollectionResolver.LazyServiceCollector> serviceCollector;
4139

4240
MyPsiElementVisitor(@NotNull ProblemsHolder holder) {
4341
this.holder = holder;
4442
}
4543

4644
@Override
47-
public void visitFile(PsiFile file) {
48-
for (XmlTag xmlTag : ServiceActionUtil.getXmlContainerServiceDefinition(file)) {
49-
visitService(xmlTag, holder);
45+
public void visitElement(@NotNull PsiElement element) {
46+
if (element instanceof XmlTag xmlTag) {
47+
visitService(xmlTag, holder, this.createLazyServiceCollector());
5048
}
5149

52-
this.lazyServiceCollector = null;
53-
54-
super.visitFile(file);
55-
}
56-
57-
private ContainerCollectionResolver.LazyServiceCollector getLazyServiceCollector(XmlTag xmlTag) {
58-
return Objects.requireNonNullElseGet(
59-
this.lazyServiceCollector,
60-
() -> this.lazyServiceCollector = new ContainerCollectionResolver.LazyServiceCollector(xmlTag.getProject())
61-
);
62-
50+
super.visitElement(element);
6351
}
6452

65-
private void visitService(XmlTag xmlTag, @NotNull ProblemsHolder holder) {
53+
private void visitService(XmlTag xmlTag, @NotNull ProblemsHolder holder, @NotNull NotNullLazyValue<ContainerCollectionResolver.LazyServiceCollector> lazyServiceCollector) {
6654
if(!ServiceActionUtil.isValidXmlParameterInspectionService(xmlTag)) {
6755
return;
6856
}
6957

70-
final List<String> args = ServiceActionUtil.getXmlMissingArgumentTypes(xmlTag, false, getLazyServiceCollector(xmlTag));
58+
final List<String> args = ServiceActionUtil.getXmlMissingArgumentTypes(xmlTag, false, lazyServiceCollector.get());
7159
if (args.size() == 0) {
7260
return;
7361
}
@@ -79,5 +67,13 @@ private void visitService(XmlTag xmlTag, @NotNull ProblemsHolder holder) {
7967

8068
holder.registerProblem(childrenOfType, "Missing argument", ProblemHighlightType.GENERIC_ERROR_OR_WARNING, new AddServiceXmlArgumentLocalQuickFix(args));
8169
}
70+
71+
private NotNullLazyValue<ContainerCollectionResolver.LazyServiceCollector> createLazyServiceCollector() {
72+
if (this.serviceCollector == null) {
73+
this.serviceCollector = NotNullLazyValue.lazy(() -> new ContainerCollectionResolver.LazyServiceCollector(holder.getProject()));
74+
}
75+
76+
return this.serviceCollector;
77+
}
8278
}
8379
}

0 commit comments

Comments
 (0)