Skip to content

Commit c8d6170

Browse files
committed
#1984 detected missing services inside "Autowire" attribute
1 parent ae68004 commit c8d6170

File tree

3 files changed

+41
-9
lines changed

3 files changed

+41
-9
lines changed

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

+22-9
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import com.intellij.psi.PsiElement;
77
import com.intellij.psi.PsiElementVisitor;
88
import com.jetbrains.php.lang.PhpLanguage;
9+
import com.jetbrains.php.lang.lexer.PhpTokenTypes;
910
import com.jetbrains.php.lang.psi.elements.MethodReference;
1011
import com.jetbrains.php.lang.psi.elements.StringLiteralExpression;
1112
import fr.adrienbrault.idea.symfony2plugin.Symfony2ProjectComponent;
@@ -19,6 +20,8 @@
1920
import org.jetbrains.annotations.NotNull;
2021
import org.jetbrains.yaml.YAMLLanguage;
2122

23+
import java.util.Arrays;
24+
2225
/**
2326
* @author Daniel Espendiller <daniel@espendiller.net>
2427
*/
@@ -50,23 +53,33 @@ public void visitElement(PsiElement element) {
5053
MethodReference methodReference = PsiElementUtils.getMethodReferenceWithFirstStringParameter((StringLiteralExpression) element);
5154
if (methodReference != null && PhpElementsUtil.isMethodReferenceInstanceOf(methodReference, ServiceContainerUtil.SERVICE_GET_SIGNATURES)) {
5255
String serviceName = PhpElementsUtil.getFirstArgumentStringValue(methodReference);
53-
if(StringUtils.isNotBlank(serviceName)) {
54-
if(!hasService(serviceName)) {
55-
holder.registerProblem(element, INSPECTION_MESSAGE, ProblemHighlightType.GENERIC_ERROR_OR_WARNING);
56-
}
56+
if (StringUtils.isNotBlank(serviceName) && !hasService(serviceName)) {
57+
holder.registerProblem(element, INSPECTION_MESSAGE, ProblemHighlightType.GENERIC_ERROR_OR_WARNING);
58+
}
59+
}
60+
61+
// get leaf element
62+
PsiElement leafText = Arrays.stream(YamlHelper.getChildrenFix(element))
63+
.filter(p -> p.getNode().getElementType() == PhpTokenTypes.STRING_LITERAL_SINGLE_QUOTE || p.getNode().getElementType() == PhpTokenTypes.STRING_LITERAL)
64+
.findFirst()
65+
.orElse(null);
66+
67+
if (leafText != null && PhpElementsUtil.getAttributeNamedArgumentStringPattern(ServiceContainerUtil.AUTOWIRE_ATTRIBUTE_CLASS, "service").accepts(leafText)) {
68+
String serviceName = ((StringLiteralExpression) element).getContents();
69+
if (StringUtils.isNotBlank(serviceName) && !hasService(serviceName)) {
70+
holder.registerProblem(element, INSPECTION_MESSAGE, ProblemHighlightType.GENERIC_ERROR_OR_WARNING);
5771
}
5872
}
73+
5974
} else if(element.getLanguage() == YAMLLanguage.INSTANCE) {
6075
// yaml
6176

62-
if(YamlElementPatternHelper.getServiceDefinition().accepts(element) && YamlElementPatternHelper.getInsideServiceKeyPattern().accepts(element)) {
77+
if (YamlElementPatternHelper.getServiceDefinition().accepts(element) && YamlElementPatternHelper.getInsideServiceKeyPattern().accepts(element)) {
6378
String serviceName = YamlHelper.trimSpecialSyntaxServiceName(PsiElementUtils.getText(element));
6479

6580
// dont mark "@", "@?", "@@" escaping and expressions
66-
if(serviceName.length() > 2 && !serviceName.startsWith("=") && !serviceName.startsWith("@")) {
67-
if(!hasService(serviceName)) {
68-
holder.registerProblem(element, INSPECTION_MESSAGE, ProblemHighlightType.GENERIC_ERROR_OR_WARNING);
69-
}
81+
if (serviceName.length() > 2 && !serviceName.startsWith("=") && !serviceName.startsWith("@") && !hasService(serviceName)) {
82+
holder.registerProblem(element, INSPECTION_MESSAGE, ProblemHighlightType.GENERIC_ERROR_OR_WARNING);
7083
}
7184
}
7285
}

src/test/java/fr/adrienbrault/idea/symfony2plugin/tests/dic/inspection/MissingServiceInspectionTest.java

+14
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,20 @@ public void testThatPhpServiceInterfaceForGetMethodIsInspected() {
3232
);
3333
}
3434

35+
public void testThatPhpAttributesForServiceAutowireIsInspected() {
36+
assertLocalInspectionContains("test.php", "<?php\n" +
37+
"use Symfony\\Component\\DependencyInjection\\Attribute\\Autowire;\n" +
38+
"\n" +
39+
"class HandlerCollection\n" +
40+
"{\n" +
41+
" public function __construct(\n" +
42+
" #[Autowire(service: 'fo<caret>obar')]" +
43+
" ) {}\n" +
44+
"}",
45+
MissingServiceInspection.INSPECTION_MESSAGE
46+
);
47+
}
48+
3549
public void testThatYamlServiceInterfaceForGetMethodIsInspected() {
3650
assertLocalInspectionContains("services.yml", "services:\n @args<caret>_unknown", MissingServiceInspection.INSPECTION_MESSAGE);
3751
assertLocalInspectionContains("services.yml", "services:\n @Args<caret>_unknown", MissingServiceInspection.INSPECTION_MESSAGE);

src/test/java/fr/adrienbrault/idea/symfony2plugin/tests/dic/inspection/fixtures/classes.php

+5
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,9 @@ public function __construct($foobar)
2121
{
2222
}
2323
}
24+
}
25+
26+
namespace Symfony\Component\DependencyInjection\Attribute
27+
{
28+
class Autowire {}
2429
}

0 commit comments

Comments
 (0)