Skip to content

Commit f122ef3

Browse files
committed
#1984 detected deprecated services usage inside "Autowire" attribute
1 parent 6f09e45 commit f122ef3

File tree

6 files changed

+55
-9
lines changed

6 files changed

+55
-9
lines changed

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,20 @@ public void visitElement(PsiElement psiElement) {
169169
return;
170170
}
171171

172+
// #[Autowire(service: 'foobar')]
173+
PsiElement leafText = PsiElementUtils.getTextLeafElementFromStringLiteralExpression((StringLiteralExpression) psiElement);
174+
175+
if (leafText != null && PhpElementsUtil.getAttributeNamedArgumentStringPattern(ServiceContainerUtil.AUTOWIRE_ATTRIBUTE_CLASS, "service").accepts(leafText)) {
176+
String contents = ((StringLiteralExpression) psiElement).getContents();
177+
if(StringUtils.isNotBlank(contents)) {
178+
this.problemRegistrar.attachDeprecatedProblem(psiElement, contents, holder);
179+
this.problemRegistrar.attachServiceDeprecatedProblem(psiElement, contents, holder);
180+
}
181+
182+
super.visitElement(psiElement);
183+
return;
184+
}
185+
172186
MethodReference methodReference = PsiElementUtils.getMethodReferenceWithFirstStringParameter((StringLiteralExpression) psiElement);
173187
if (methodReference == null || !PhpElementsUtil.isMethodReferenceInstanceOf(methodReference, ServiceContainerUtil.SERVICE_GET_SIGNATURES)) {
174188
super.visitElement(psiElement);

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

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
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;
109
import com.jetbrains.php.lang.psi.elements.MethodReference;
1110
import com.jetbrains.php.lang.psi.elements.StringLiteralExpression;
1211
import fr.adrienbrault.idea.symfony2plugin.Symfony2ProjectComponent;
@@ -20,8 +19,6 @@
2019
import org.jetbrains.annotations.NotNull;
2120
import org.jetbrains.yaml.YAMLLanguage;
2221

23-
import java.util.Arrays;
24-
2522
/**
2623
* @author Daniel Espendiller <daniel@espendiller.net>
2724
*/
@@ -59,11 +56,7 @@ public void visitElement(PsiElement element) {
5956
}
6057

6158
// #[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);
59+
PsiElement leafText = PsiElementUtils.getTextLeafElementFromStringLiteralExpression((StringLiteralExpression) element);
6760

6861
if (leafText != null && PhpElementsUtil.getAttributeNamedArgumentStringPattern(ServiceContainerUtil.AUTOWIRE_ATTRIBUTE_CLASS, "service").accepts(leafText)) {
6962
String serviceName = ((StringLiteralExpression) element).getContents();

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@
1212
import com.intellij.psi.util.PsiTreeUtil;
1313
import com.intellij.util.Processor;
1414
import com.jetbrains.php.lang.PhpLanguage;
15+
import com.jetbrains.php.lang.lexer.PhpTokenTypes;
1516
import com.jetbrains.php.lang.psi.elements.*;
17+
import fr.adrienbrault.idea.symfony2plugin.util.yaml.YamlHelper;
1618
import org.jetbrains.annotations.NotNull;
1719
import org.jetbrains.annotations.Nullable;
1820

@@ -389,6 +391,14 @@ public static String getStringBeforeCursor(StringLiteralExpression literal, int
389391
return content.length() >= cursorOffsetClean ? content.substring(0, cursorOffsetClean) : null;
390392
}
391393

394+
@Nullable
395+
public static PsiElement getTextLeafElementFromStringLiteralExpression(@NotNull StringLiteralExpression psiElement) {
396+
return Arrays.stream(YamlHelper.getChildrenFix(psiElement))
397+
.filter(p -> p.getNode().getElementType() == PhpTokenTypes.STRING_LITERAL_SINGLE_QUOTE || p.getNode().getElementType() == PhpTokenTypes.STRING_LITERAL)
398+
.findFirst()
399+
.orElse(null);
400+
}
401+
392402
@NotNull
393403
public static Collection<PsiFile> convertVirtualFilesToPsiFiles(@NotNull Project project, @NotNull Collection<VirtualFile> files) {
394404

src/test/java/fr/adrienbrault/idea/symfony2plugin/tests/codeInspection/service/ServiceDeprecatedClassesInspectionTest.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,30 @@ public void testPhpServiceDeprecated() {
3636
);
3737
}
3838

39+
public void testPhpServiceInsideAutowireAttributeDeprecated() {
40+
assertLocalInspectionContains("foo.php", "<?php" +
41+
"\n" +
42+
"class HandlerCollection\n" +
43+
"{\n" +
44+
" public function __construct(\n" +
45+
" #[\\Symfony\\Component\\DependencyInjection\\Attribute\\Autowire(service: 'foo_depr<caret>ecated')] $handlers\n" +
46+
" ) {}\n" +
47+
"}",
48+
"Service 'foo_deprecated' is deprecated"
49+
);
50+
51+
assertLocalInspectionContains("foo.php", "<?php" +
52+
"\n" +
53+
"class HandlerCollection\n" +
54+
"{\n" +
55+
" public function __construct(\n" +
56+
" #[\\Symfony\\Component\\DependencyInjection\\Attribute\\Autowire(service: \"foo_depr<caret>ecated\")] $handlers\n" +
57+
" ) {}\n" +
58+
"}",
59+
"Service 'foo_deprecated' is deprecated"
60+
);
61+
}
62+
3963
public void testYmlClassDocBlockDeprecated() {
4064
assertLocalInspectionContains("foo.yml", "@f<caret>oo", "Class 'FooBar' is deprecated");
4165
assertLocalInspectionContains("foo.yml", "class: Foo\\Bar<caret>\\FooBar", "Class 'FooBar' is deprecated");

src/test/java/fr/adrienbrault/idea/symfony2plugin/tests/codeInspection/service/fixtures/classes.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,9 @@ interface Twig_ExtensionInterface {}
2525
namespace Tag\InstanceCheck
2626
{
2727
class EmptyClass {}
28+
}
29+
30+
namespace Symfony\Component\DependencyInjection\Attribute
31+
{
32+
class Autowire {}
2833
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public function __construct($foobar)
2323
}
2424
}
2525

26-
namespace Symfony\Component\DependencyInjection\Attribute
26+
namespace Symfony\Component\DependencyInjection\Attribute
2727
{
2828
class Autowire {}
2929
}

0 commit comments

Comments
 (0)