Skip to content

Commit e48a3c5

Browse files
authored
Merge pull request #1988 from Haehnchen/feature/1984-missing-autowire-attribute
#1984 detected missing services inside "Autowire" attribute
2 parents 930a8c4 + 6f09e45 commit e48a3c5

File tree

3 files changed

+54
-9
lines changed

3 files changed

+54
-9
lines changed

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

+23-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,34 @@ 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+
// #[Autowire(service: 'foobar')]
62+
// get leaf element
63+
PsiElement leafText = Arrays.stream(YamlHelper.getChildrenFix(element))
64+
.filter(p -> p.getNode().getElementType() == PhpTokenTypes.STRING_LITERAL_SINGLE_QUOTE || p.getNode().getElementType() == PhpTokenTypes.STRING_LITERAL)
65+
.findFirst()
66+
.orElse(null);
67+
68+
if (leafText != null && PhpElementsUtil.getAttributeNamedArgumentStringPattern(ServiceContainerUtil.AUTOWIRE_ATTRIBUTE_CLASS, "service").accepts(leafText)) {
69+
String serviceName = ((StringLiteralExpression) element).getContents();
70+
if (StringUtils.isNotBlank(serviceName) && !hasService(serviceName)) {
71+
holder.registerProblem(element, INSPECTION_MESSAGE, ProblemHighlightType.GENERIC_ERROR_OR_WARNING);
5772
}
5873
}
74+
5975
} else if(element.getLanguage() == YAMLLanguage.INSTANCE) {
6076
// yaml
6177

62-
if(YamlElementPatternHelper.getServiceDefinition().accepts(element) && YamlElementPatternHelper.getInsideServiceKeyPattern().accepts(element)) {
78+
if (YamlElementPatternHelper.getServiceDefinition().accepts(element) && YamlElementPatternHelper.getInsideServiceKeyPattern().accepts(element)) {
6379
String serviceName = YamlHelper.trimSpecialSyntaxServiceName(PsiElementUtils.getText(element));
6480

6581
// 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-
}
82+
if (serviceName.length() > 2 && !serviceName.startsWith("=") && !serviceName.startsWith("@") && !hasService(serviceName)) {
83+
holder.registerProblem(element, INSPECTION_MESSAGE, ProblemHighlightType.GENERIC_ERROR_OR_WARNING);
7084
}
7185
}
7286
}

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

+26
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,32 @@ 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+
assertLocalInspectionContains("test.php", "<?php\n" +
49+
"use Symfony\\Component\\DependencyInjection\\Attribute\\Autowire;\n" +
50+
"\n" +
51+
"class HandlerCollection\n" +
52+
"{\n" +
53+
" public function __construct(\n" +
54+
" #[Autowire(service: \"fo<caret>obar\")]" +
55+
" ) {}\n" +
56+
"}",
57+
MissingServiceInspection.INSPECTION_MESSAGE
58+
);
59+
}
60+
3561
public void testThatYamlServiceInterfaceForGetMethodIsInspected() {
3662
assertLocalInspectionContains("services.yml", "services:\n @args<caret>_unknown", MissingServiceInspection.INSPECTION_MESSAGE);
3763
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)