Skip to content

Commit 24f90ee

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

File tree

4 files changed

+53
-0
lines changed

4 files changed

+53
-0
lines changed

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

+19
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
*/
@@ -56,6 +59,22 @@ public void visitElement(PsiElement element) {
5659
}
5760
}
5861
}
62+
63+
// get leaf element
64+
PsiElement leafText = Arrays.stream(YamlHelper.getChildrenFix(element))
65+
.filter(p -> p.getNode().getElementType() == PhpTokenTypes.STRING_LITERAL_SINGLE_QUOTE || p.getNode().getElementType() == PhpTokenTypes.STRING_LITERAL)
66+
.findFirst()
67+
.orElse(null);
68+
69+
if (leafText != null && PhpElementsUtil.getAttributeNamedArgumentStringPattern(ServiceContainerUtil.AUTOWIRE_ATTRIBUTE_CLASS, "service").accepts(leafText)) {
70+
String serviceName = ((StringLiteralExpression) element).getContents();
71+
if (StringUtils.isNotBlank(serviceName)) {
72+
if (!hasService(serviceName)) {
73+
holder.registerProblem(element, INSPECTION_MESSAGE, ProblemHighlightType.GENERIC_ERROR_OR_WARNING);
74+
}
75+
}
76+
}
77+
5978
} else if(element.getLanguage() == YAMLLanguage.INSTANCE) {
6079
// yaml
6180

src/main/java/fr/adrienbrault/idea/symfony2plugin/util/PsiElementUtils.java

+15
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.intellij.openapi.project.Project;
44
import com.intellij.openapi.vfs.VirtualFile;
55
import com.intellij.patterns.ElementPattern;
6+
import com.intellij.patterns.IElementTypePattern;
67
import com.intellij.patterns.PlatformPatterns;
78
import com.intellij.psi.PsiElement;
89
import com.intellij.psi.PsiFile;
@@ -219,6 +220,20 @@ public static <T extends PsiElement> T getChildrenOfType(@Nullable PsiElement el
219220
return null;
220221
}
221222

223+
@Nullable
224+
public static <T extends PsiElement> T getChildrenOfType(@Nullable PsiElement element, IElementTypePattern pattern) {
225+
if (element == null) return null;
226+
227+
for (PsiElement child = element.getFirstChild(); child != null; child = child.getNextSibling()) {
228+
if (pattern.accepts(child)) {
229+
//noinspection unchecked
230+
return (T)child;
231+
}
232+
}
233+
234+
return null;
235+
}
236+
222237
@NotNull
223238
public static <T extends PsiElement> Collection<T> getChildrenOfTypeAsList(@Nullable PsiElement element, ElementPattern<T> pattern) {
224239

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)