Skip to content

Commit 542c7be

Browse files
authored
Merge pull request #2367 from Haehnchen/feature/inspection-language-2
provide inspection language attributes for tag implementations, container / route deprecated
2 parents 24f534a + 93d2e5a commit 542c7be

10 files changed

+234
-131
lines changed

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

Lines changed: 87 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
import com.intellij.codeInspection.LocalInspectionTool;
44
import com.intellij.codeInspection.ProblemHighlightType;
55
import com.intellij.codeInspection.ProblemsHolder;
6-
import com.intellij.lang.Language;
7-
import com.intellij.lang.xml.XMLLanguage;
86
import com.intellij.openapi.util.NotNullLazyValue;
97
import com.intellij.psi.PsiElement;
108
import com.intellij.psi.PsiElementVisitor;
@@ -23,7 +21,6 @@
2321
import org.apache.commons.lang3.StringUtils;
2422
import org.jetbrains.annotations.NotNull;
2523
import org.jetbrains.annotations.Nullable;
26-
import org.jetbrains.yaml.YAMLLanguage;
2724
import org.jetbrains.yaml.YAMLTokenTypes;
2825
import org.jetbrains.yaml.psi.YAMLCompoundValue;
2926
import org.jetbrains.yaml.psi.YAMLKeyValue;
@@ -34,98 +31,118 @@
3431
/**
3532
* @author Daniel Espendiller <daniel@espendiller.net>
3633
*/
37-
public class TaggedExtendsInterfaceClassInspection extends LocalInspectionTool {
34+
public class TaggedExtendsInterfaceClassInspection {
35+
public static class TaggedExtendsInterfaceClassInspectionYaml extends LocalInspectionTool {
36+
public @NotNull PsiElementVisitor buildVisitor(final @NotNull ProblemsHolder holder, boolean isOnTheFly) {
37+
if (!Symfony2ProjectComponent.isEnabled(holder.getProject())) {
38+
return super.buildVisitor(holder, isOnTheFly);
39+
}
3840

39-
@NotNull
40-
@Override
41-
public PsiElementVisitor buildVisitor(final @NotNull ProblemsHolder holder, boolean isOnTheFly) {
42-
if (!Symfony2ProjectComponent.isEnabled(holder.getProject())) {
43-
return super.buildVisitor(holder, isOnTheFly);
44-
}
41+
return new PsiElementVisitor() {
42+
private NotNullLazyValue<ContainerCollectionResolver.LazyServiceCollector> serviceCollector;
4543

46-
return new PsiElementVisitor() {
47-
private NotNullLazyValue<ContainerCollectionResolver.LazyServiceCollector> serviceCollector;
44+
@Override
45+
public void visitElement(@NotNull PsiElement element) {
46+
visitYamlElement(element, holder);
47+
super.visitElement(element);
48+
}
4849

49-
@Override
50-
public void visitElement(@NotNull PsiElement element) {
51-
Language language = element.getLanguage();
50+
private void visitYamlElement(@NotNull PsiElement psiElement, @NotNull ProblemsHolder holder) {
51+
if (YamlElementPatternHelper.getSingleLineScalarKey("class").accepts(psiElement)) {
5252

53-
if (language == YAMLLanguage.INSTANCE) {
54-
visitYamlElement(element, holder, this.createLazyServiceCollector());
55-
} else if (language == XMLLanguage.INSTANCE) {
56-
visitXmlElement(element, holder, this.createLazyServiceCollector());
57-
}
53+
// class: '\Foo'
54+
String text = PsiElementUtils.trimQuote(psiElement.getText());
55+
if (StringUtils.isBlank(text)) {
56+
return;
57+
}
5858

59-
super.visitElement(element);
60-
}
59+
PsiElement yamlScalar = psiElement.getParent();
60+
if (!(yamlScalar instanceof YAMLScalar)) {
61+
return;
62+
}
6163

62-
private NotNullLazyValue<ContainerCollectionResolver.LazyServiceCollector> createLazyServiceCollector() {
63-
if (this.serviceCollector == null) {
64-
this.serviceCollector = NotNullLazyValue.lazy(() -> new ContainerCollectionResolver.LazyServiceCollector(holder.getProject()));
64+
PsiElement classKey = yamlScalar.getParent();
65+
if (classKey instanceof YAMLKeyValue) {
66+
PsiElement yamlCompoundValue = classKey.getParent();
67+
if (yamlCompoundValue instanceof YAMLCompoundValue) {
68+
PsiElement serviceKeyValue = yamlCompoundValue.getParent();
69+
if (serviceKeyValue instanceof YAMLKeyValue) {
70+
Set<String> tags = YamlHelper.collectServiceTags((YAMLKeyValue) serviceKeyValue);
71+
if (!tags.isEmpty()) {
72+
registerTaggedProblems(psiElement, tags, text, holder, this.createLazyServiceCollector());
73+
}
74+
}
75+
}
76+
}
77+
} else if (psiElement.getNode().getElementType() == YAMLTokenTypes.SCALAR_KEY && YamlElementPatternHelper.getServiceIdKeyValuePattern().accepts(psiElement.getParent())) {
78+
// Foobar\Foo: ~
79+
String text = PsiElementUtils.getText(psiElement);
80+
if (StringUtils.isNotBlank(text) && YamlHelper.isClassServiceId(text) && text.contains("\\")) {
81+
PsiElement yamlKeyValue = psiElement.getParent();
82+
if (yamlKeyValue instanceof YAMLKeyValue && YamlHelper.getYamlKeyValue((YAMLKeyValue) yamlKeyValue, "resource") == null && YamlHelper.getYamlKeyValue((YAMLKeyValue) yamlKeyValue, "exclude") == null) {
83+
Set<String> tags = YamlHelper.collectServiceTags((YAMLKeyValue) yamlKeyValue);
84+
if (!tags.isEmpty()) {
85+
registerTaggedProblems(psiElement, tags, text, holder, this.createLazyServiceCollector());
86+
}
87+
}
88+
}
89+
}
6590
}
6691

67-
return this.serviceCollector;
68-
}
69-
};
70-
}
92+
private NotNullLazyValue<ContainerCollectionResolver.LazyServiceCollector> createLazyServiceCollector() {
93+
if (this.serviceCollector == null) {
94+
this.serviceCollector = NotNullLazyValue.lazy(() -> new ContainerCollectionResolver.LazyServiceCollector(holder.getProject()));
95+
}
7196

72-
private void visitXmlElement(@NotNull PsiElement element, @NotNull ProblemsHolder holder, @NotNull NotNullLazyValue<ContainerCollectionResolver.LazyServiceCollector> lazyServiceCollector) {
73-
String className = getClassNameFromServiceDefinition(element);
74-
if (className != null) {
75-
XmlTag parentOfType = PsiTreeUtil.getParentOfType(element, XmlTag.class);
76-
if (parentOfType != null) {
77-
// attach problems to string value only
78-
PsiElement[] psiElements = element.getChildren();
79-
if (psiElements.length > 2) {
80-
registerTaggedProblems(psiElements[1], FormUtil.getTags(parentOfType), className, holder, lazyServiceCollector);
97+
return this.serviceCollector;
8198
}
82-
}
99+
};
83100
}
84101
}
85102

86-
private void visitYamlElement(@NotNull PsiElement psiElement, @NotNull ProblemsHolder holder, @NotNull NotNullLazyValue<ContainerCollectionResolver.LazyServiceCollector> lazyServiceCollector) {
87-
if (YamlElementPatternHelper.getSingleLineScalarKey("class").accepts(psiElement)) {
103+
public static class TaggedExtendsInterfaceClassInspectionXml extends LocalInspectionTool {
104+
private NotNullLazyValue<ContainerCollectionResolver.LazyServiceCollector> serviceCollector;
88105

89-
// class: '\Foo'
90-
String text = PsiElementUtils.trimQuote(psiElement.getText());
91-
if (StringUtils.isBlank(text)) {
92-
return;
106+
public @NotNull PsiElementVisitor buildVisitor(final @NotNull ProblemsHolder holder, boolean isOnTheFly) {
107+
if (!Symfony2ProjectComponent.isEnabled(holder.getProject())) {
108+
return super.buildVisitor(holder, isOnTheFly);
93109
}
94110

95-
PsiElement yamlScalar = psiElement.getParent();
96-
if (!(yamlScalar instanceof YAMLScalar)) {
97-
return;
98-
}
111+
return new PsiElementVisitor() {
112+
private NotNullLazyValue<ContainerCollectionResolver.LazyServiceCollector> serviceCollector;
113+
114+
@Override
115+
public void visitElement(@NotNull PsiElement element) {
116+
visitXmlElement(element, holder);
117+
super.visitElement(element);
118+
}
99119

100-
PsiElement classKey = yamlScalar.getParent();
101-
if (classKey instanceof YAMLKeyValue) {
102-
PsiElement yamlCompoundValue = classKey.getParent();
103-
if (yamlCompoundValue instanceof YAMLCompoundValue) {
104-
PsiElement serviceKeyValue = yamlCompoundValue.getParent();
105-
if (serviceKeyValue instanceof YAMLKeyValue) {
106-
Set<String> tags = YamlHelper.collectServiceTags((YAMLKeyValue) serviceKeyValue);
107-
if (!tags.isEmpty()) {
108-
registerTaggedProblems(psiElement, tags, text, holder, lazyServiceCollector);
120+
private void visitXmlElement(@NotNull PsiElement element, @NotNull ProblemsHolder holder) {
121+
String className = getClassNameFromServiceDefinition(element);
122+
if (className != null) {
123+
XmlTag parentOfType = PsiTreeUtil.getParentOfType(element, XmlTag.class);
124+
if (parentOfType != null) {
125+
// attach problems to string value only
126+
PsiElement[] psiElements = element.getChildren();
127+
if (psiElements.length > 2) {
128+
registerTaggedProblems(psiElements[1], FormUtil.getTags(parentOfType), className, holder, createLazyServiceCollector());
129+
}
109130
}
110131
}
111132
}
112-
}
113-
} else if (psiElement.getNode().getElementType() == YAMLTokenTypes.SCALAR_KEY && YamlElementPatternHelper.getServiceIdKeyValuePattern().accepts(psiElement.getParent())) {
114-
// Foobar\Foo: ~
115-
String text = PsiElementUtils.getText(psiElement);
116-
if (StringUtils.isNotBlank(text) && YamlHelper.isClassServiceId(text) && text.contains("\\")) {
117-
PsiElement yamlKeyValue = psiElement.getParent();
118-
if (yamlKeyValue instanceof YAMLKeyValue && YamlHelper.getYamlKeyValue((YAMLKeyValue) yamlKeyValue, "resource") == null && YamlHelper.getYamlKeyValue((YAMLKeyValue) yamlKeyValue, "exclude") == null) {
119-
Set<String> tags = YamlHelper.collectServiceTags((YAMLKeyValue) yamlKeyValue);
120-
if (!tags.isEmpty()) {
121-
registerTaggedProblems(psiElement, tags, text, holder, lazyServiceCollector);
133+
134+
private NotNullLazyValue<ContainerCollectionResolver.LazyServiceCollector> createLazyServiceCollector() {
135+
if (this.serviceCollector == null) {
136+
this.serviceCollector = NotNullLazyValue.lazy(() -> new ContainerCollectionResolver.LazyServiceCollector(holder.getProject()));
122137
}
138+
139+
return this.serviceCollector;
123140
}
124-
}
141+
};
125142
}
126143
}
127144

128-
private void registerTaggedProblems(@NotNull PsiElement source, @NotNull Set<String> tags, @NotNull String serviceClass, @NotNull ProblemsHolder holder, @NotNull NotNullLazyValue<ContainerCollectionResolver.LazyServiceCollector> lazyServiceCollector) {
145+
private static void registerTaggedProblems(@NotNull PsiElement source, @NotNull Set<String> tags, @NotNull String serviceClass, @NotNull ProblemsHolder holder, @NotNull NotNullLazyValue<ContainerCollectionResolver.LazyServiceCollector> lazyServiceCollector) {
129146
if (tags.isEmpty()) {
130147
return;
131148
}

src/main/java/fr/adrienbrault/idea/symfony2plugin/dic/inspection/ContainerSettingDeprecatedInspection.java

Lines changed: 39 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -18,52 +18,69 @@
1818
/**
1919
* @author Daniel Espendiller <daniel@espendiller.net>
2020
*/
21-
public class ContainerSettingDeprecatedInspection extends LocalInspectionTool {
21+
public class ContainerSettingDeprecatedInspection {
22+
public static class ContainerSettingDeprecatedInspectionYaml extends LocalInspectionTool {
23+
public @NotNull PsiElementVisitor buildVisitor(final @NotNull ProblemsHolder holder, boolean isOnTheFly) {
24+
if (!Symfony2ProjectComponent.isEnabled(holder.getProject())) {
25+
return super.buildVisitor(holder, isOnTheFly);
26+
}
2227

23-
@NotNull
24-
public PsiElementVisitor buildVisitor(final @NotNull ProblemsHolder holder, boolean isOnTheFly) {
25-
if (!Symfony2ProjectComponent.isEnabled(holder.getProject())) {
26-
return super.buildVisitor(holder, isOnTheFly);
27-
}
28+
return new PsiElementVisitor() {
29+
@Override
30+
public void visitElement(@NotNull PsiElement element) {
31+
if (element instanceof YAMLKeyValue) {
32+
registerYmlRoutePatternProblem(holder, (YAMLKeyValue) element);
33+
}
2834

29-
return new PsiElementVisitor() {
30-
@Override
31-
public void visitElement(@NotNull PsiElement element) {
32-
if(element instanceof XmlAttribute) {
33-
registerXmlAttributeProblem(holder, (XmlAttribute) element);
34-
} else if(element instanceof YAMLKeyValue) {
35-
registerYmlRoutePatternProblem(holder, (YAMLKeyValue) element);
35+
super.visitElement(element);
3636
}
37+
};
38+
}
39+
}
3740

38-
super.visitElement(element);
41+
public static class ContainerSettingDeprecatedInspectionXml extends LocalInspectionTool {
42+
public @NotNull PsiElementVisitor buildVisitor(final @NotNull ProblemsHolder holder, boolean isOnTheFly) {
43+
if (!Symfony2ProjectComponent.isEnabled(holder.getProject())) {
44+
return super.buildVisitor(holder, isOnTheFly);
3945
}
40-
};
46+
47+
return new PsiElementVisitor() {
48+
@Override
49+
public void visitElement(@NotNull PsiElement element) {
50+
if (element instanceof XmlAttribute) {
51+
registerXmlAttributeProblem(holder, (XmlAttribute) element);
52+
}
53+
54+
super.visitElement(element);
55+
}
56+
};
57+
}
4158
}
4259

43-
private void registerYmlRoutePatternProblem(@NotNull ProblemsHolder holder, @NotNull YAMLKeyValue element) {
60+
private static void registerYmlRoutePatternProblem(@NotNull ProblemsHolder holder, @NotNull YAMLKeyValue element) {
4461
String s = PsiElementUtils.trimQuote(element.getKeyText());
45-
if(("factory_class".equals(s) || "factory_method".equals(s) || "factory_service".equals(s)) && YamlElementPatternHelper.getInsideServiceKeyPattern().accepts(element)) {
62+
if (("factory_class".equals(s) || "factory_method".equals(s) || "factory_service".equals(s)) && YamlElementPatternHelper.getInsideServiceKeyPattern().accepts(element)) {
4663
// services:
4764
// foo:
4865
// factory_*:
4966
registerProblem(holder, element.getKey());
5067
}
5168
}
5269

53-
private void registerXmlAttributeProblem(@NotNull ProblemsHolder holder, @NotNull XmlAttribute xmlAttribute) {
70+
private static void registerXmlAttributeProblem(@NotNull ProblemsHolder holder, @NotNull XmlAttribute xmlAttribute) {
5471
String name = xmlAttribute.getName();
55-
if(!("factory-class".equals(name) || "factory-method".equals(name) || "factory-service".equals(name))) {
72+
if (!("factory-class".equals(name) || "factory-method".equals(name) || "factory-service".equals(name))) {
5673
return;
5774
}
5875

5976
XmlTag xmlTagRoute = PsiElementAssertUtil.getParentOfTypeWithNameOrNull(xmlAttribute, XmlTag.class, "service");
60-
if(xmlTagRoute != null) {
77+
if (xmlTagRoute != null) {
6178
registerProblem(holder, xmlAttribute.getFirstChild());
6279
}
6380
}
6481

65-
private void registerProblem(@NotNull ProblemsHolder holder, @Nullable PsiElement target) {
66-
if(target == null) {
82+
private static void registerProblem(@NotNull ProblemsHolder holder, @Nullable PsiElement target) {
83+
if (target == null) {
6784
return;
6885
}
6986

0 commit comments

Comments
 (0)