Skip to content

Commit d369cef

Browse files
committed
service argument inspection must respect _default configuration #948
1 parent 668c866 commit d369cef

File tree

4 files changed

+93
-7
lines changed

4 files changed

+93
-7
lines changed

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

+21-5
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import fr.adrienbrault.idea.symfony2plugin.action.ui.SymfonyCreateService;
2525
import fr.adrienbrault.idea.symfony2plugin.dic.ContainerService;
2626
import fr.adrienbrault.idea.symfony2plugin.dic.container.util.ServiceContainerUtil;
27+
import fr.adrienbrault.idea.symfony2plugin.intentions.yaml.YamlServiceArgumentInspection;
2728
import fr.adrienbrault.idea.symfony2plugin.stubs.ContainerCollectionResolver;
2829
import fr.adrienbrault.idea.symfony2plugin.util.PhpElementsUtil;
2930
import fr.adrienbrault.idea.symfony2plugin.util.SymfonyBundleUtil;
@@ -48,9 +49,13 @@ public class ServiceActionUtil {
4849
/**
4950
* Attributes which we should not support in missing arguments constructors for server definition
5051
*/
51-
public static final String[] INVALID_ARGUMENT_ATTRIBUTES = new String[]{
52-
"parent", "factory-class", "factory-service", "abstract", "autowire"
53-
};
52+
public static final String[] INVALID_ARGUMENT_ATTRIBUTES = getInvalidArgumentAttributes();
53+
54+
private static String[] getInvalidArgumentAttributes() {
55+
return Arrays.stream(YamlServiceArgumentInspection.INVALID_KEYS)
56+
.map(s -> s.replace("_", "-"))
57+
.toArray(String[]::new);
58+
}
5459

5560
public static void buildFile(AnActionEvent event, final Project project, String templatePath) {
5661
String extension = templatePath.endsWith(".yml") ? "yml" : "xml" ;
@@ -382,8 +387,19 @@ public static boolean isValidXmlParameterInspectionService(@NotNull XmlTag xmlTa
382387

383388
// <service><factory/></service>
384389
// symfony2 >= 2.6
385-
for (XmlTag tag : xmlTag.getSubTags()) {
386-
if("factory".equals(tag.getName())) {
390+
if(xmlTag.findSubTags("factory").length > 0) {
391+
return false;
392+
}
393+
394+
// <services><defaults/></services>
395+
PsiElement parent = xmlTag.getParent();
396+
if(!(parent instanceof XmlTag) || !"services".equals(((XmlTag) parent).getName())) {
397+
return true;
398+
}
399+
400+
// <defaults autowire="true" />
401+
for (XmlTag defaults : ((XmlTag) parent).findSubTags("defaults")) {
402+
if("true".equalsIgnoreCase(defaults.getAttributeValue("autowire"))) {
387403
return false;
388404
}
389405
}

src/fr/adrienbrault/idea/symfony2plugin/intentions/yaml/YamlServiceArgumentInspection.java

+21-2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import org.jetbrains.annotations.NotNull;
1515
import org.jetbrains.yaml.psi.YAMLFile;
1616
import org.jetbrains.yaml.psi.YAMLKeyValue;
17+
import org.jetbrains.yaml.psi.YAMLMapping;
1718

1819
import java.util.List;
1920
import java.util.Set;
@@ -23,7 +24,11 @@
2324
*/
2425
public class YamlServiceArgumentInspection extends LocalInspectionTool {
2526

26-
public static final String[] INVALID_KEYS = new String[]{"parent", "factory_class", "factory_service", "factory_method", "abstract", "factory", "autowire"};
27+
public static final String[] INVALID_KEYS = new String[]{
28+
"parent", "factory_class", "factory_service",
29+
"factory_method", "abstract", "factory",
30+
"autowire", "resource", "exclude"
31+
};
2732

2833
@NotNull
2934
public PsiElementVisitor buildVisitor(final @NotNull ProblemsHolder holder, boolean isOnTheFly) {
@@ -68,8 +73,9 @@ public void visitFile(PsiFile file) {
6873
}
6974

7075
private boolean isValidService(ServiceActionUtil.ServiceYamlContainer serviceYamlContainer) {
76+
YAMLKeyValue serviceKey = serviceYamlContainer.getServiceKey();
7177

72-
Set<String> keySet = YamlHelper.getKeySet(serviceYamlContainer.getServiceKey());
78+
Set<String> keySet = YamlHelper.getKeySet(serviceKey);
7379
if(keySet == null) {
7480
return true;
7581
}
@@ -80,6 +86,19 @@ private boolean isValidService(ServiceActionUtil.ServiceYamlContainer serviceYam
8086
}
8187
}
8288

89+
// find file scope defaults
90+
// defaults: [autowire: true]
91+
YAMLMapping key = serviceKey.getParentMapping();
92+
if(key != null) {
93+
YAMLKeyValue defaults = YamlHelper.getYamlKeyValue(key, "_defaults");
94+
if(defaults != null) {
95+
Boolean autowire = YamlHelper.getYamlKeyValueAsBoolean(defaults, "autowire");
96+
if(autowire != null && autowire) {
97+
return false;
98+
}
99+
}
100+
}
101+
83102
return true;
84103
}
85104

tests/fr/adrienbrault/idea/symfony2plugin/tests/config/xml/inspection/XmlServiceArgumentInspectionTest.java

+30
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,22 @@ public void testThatNotSupportServiceAttributeNotProvidesInspection() {
6161
}
6262
}
6363

64+
public void testThatServiceResourceMustNotProvideInspection() {
65+
assertLocalInspectionNotContains(
66+
"services.xml",
67+
createContainer("<serv<caret>ice resource=\"foo\" class=\"Foo\\Bar\"/>"),
68+
"Missing argument"
69+
);
70+
}
71+
72+
public void testThatServiceFactoryServiceMustNotProvideInspection() {
73+
assertLocalInspectionNotContains(
74+
"services.xml",
75+
createContainer("<serv<caret>ice factory-service=\"foo\" class=\"Foo\\Bar\"/>"),
76+
"Missing argument"
77+
);
78+
}
79+
6480
public void testThatFactoryServiceOfSymfony26NotProvidesInspection() {
6581
for (String s : ServiceActionUtil.INVALID_ARGUMENT_ATTRIBUTES) {
6682
assertLocalInspectionNotContains(
@@ -71,6 +87,20 @@ public void testThatFactoryServiceOfSymfony26NotProvidesInspection() {
7187
}
7288
}
7389

90+
public void testThatDefaultValueMustNotProvideInspection() {
91+
assertLocalInspectionNotContains(
92+
"services.xml",
93+
createContainer("<defaults autowire=\"true\" /><serv<caret>ice class=\"Foo\\Bar\"/>"),
94+
"Missing argument"
95+
);
96+
97+
assertLocalInspectionContains(
98+
"services.xml",
99+
createContainer("<defaults autowire=\"false\" /><serv<caret>ice class=\"Foo\\Bar\"/>"),
100+
"Missing argument"
101+
);
102+
}
103+
74104
private String createContainer(String serviceDefinition) {
75105
return "<container><services>" + serviceDefinition + "</services></container>";
76106
}

tests/fr/adrienbrault/idea/symfony2plugin/tests/config/yaml/YamlServiceArgumentInspectionTest.java

+21
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,25 @@ public void testThatNotSupportServiceAttributeNotProvidesInspection() {
5656
}
5757
}
5858

59+
public void testThatDefaultsWithAutoWireMustStopInspection() {
60+
assertLocalInspectionNotContains("services.yml", "" +
61+
"services:\n" +
62+
" _defaults:\n" +
63+
" autowire: true\n" +
64+
"" +
65+
" f<caret>oo:\n" +
66+
" class: \\Foo\\Bar\n",
67+
"Missing argument"
68+
);
69+
70+
assertLocalInspectionContains("services.yml", "" +
71+
"services:\n" +
72+
" _defaults:\n" +
73+
" autowire: false\n" +
74+
"" +
75+
" f<caret>oo:\n" +
76+
" class: \\Foo\\Bar\n",
77+
"Missing argument"
78+
);
79+
}
5980
}

0 commit comments

Comments
 (0)