Skip to content

Commit

Permalink
Merge pull request #1988 from Haehnchen/feature/1984-missing-autowire…
Browse files Browse the repository at this point in the history
…-attribute

#1984 detected missing services inside "Autowire" attribute
  • Loading branch information
Haehnchen committed Jul 23, 2022
2 parents 930a8c4 + 6f09e45 commit e48a3c5
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiElementVisitor;
import com.jetbrains.php.lang.PhpLanguage;
import com.jetbrains.php.lang.lexer.PhpTokenTypes;
import com.jetbrains.php.lang.psi.elements.MethodReference;
import com.jetbrains.php.lang.psi.elements.StringLiteralExpression;
import fr.adrienbrault.idea.symfony2plugin.Symfony2ProjectComponent;
Expand All @@ -19,6 +20,8 @@
import org.jetbrains.annotations.NotNull;
import org.jetbrains.yaml.YAMLLanguage;

import java.util.Arrays;

/**
* @author Daniel Espendiller <daniel@espendiller.net>
*/
Expand Down Expand Up @@ -50,23 +53,34 @@ public void visitElement(PsiElement element) {
MethodReference methodReference = PsiElementUtils.getMethodReferenceWithFirstStringParameter((StringLiteralExpression) element);
if (methodReference != null && PhpElementsUtil.isMethodReferenceInstanceOf(methodReference, ServiceContainerUtil.SERVICE_GET_SIGNATURES)) {
String serviceName = PhpElementsUtil.getFirstArgumentStringValue(methodReference);
if(StringUtils.isNotBlank(serviceName)) {
if(!hasService(serviceName)) {
holder.registerProblem(element, INSPECTION_MESSAGE, ProblemHighlightType.GENERIC_ERROR_OR_WARNING);
}
if (StringUtils.isNotBlank(serviceName) && !hasService(serviceName)) {
holder.registerProblem(element, INSPECTION_MESSAGE, ProblemHighlightType.GENERIC_ERROR_OR_WARNING);
}
}

// #[Autowire(service: 'foobar')]
// get leaf element
PsiElement leafText = Arrays.stream(YamlHelper.getChildrenFix(element))
.filter(p -> p.getNode().getElementType() == PhpTokenTypes.STRING_LITERAL_SINGLE_QUOTE || p.getNode().getElementType() == PhpTokenTypes.STRING_LITERAL)
.findFirst()
.orElse(null);

if (leafText != null && PhpElementsUtil.getAttributeNamedArgumentStringPattern(ServiceContainerUtil.AUTOWIRE_ATTRIBUTE_CLASS, "service").accepts(leafText)) {
String serviceName = ((StringLiteralExpression) element).getContents();
if (StringUtils.isNotBlank(serviceName) && !hasService(serviceName)) {
holder.registerProblem(element, INSPECTION_MESSAGE, ProblemHighlightType.GENERIC_ERROR_OR_WARNING);
}
}

} else if(element.getLanguage() == YAMLLanguage.INSTANCE) {
// yaml

if(YamlElementPatternHelper.getServiceDefinition().accepts(element) && YamlElementPatternHelper.getInsideServiceKeyPattern().accepts(element)) {
if (YamlElementPatternHelper.getServiceDefinition().accepts(element) && YamlElementPatternHelper.getInsideServiceKeyPattern().accepts(element)) {
String serviceName = YamlHelper.trimSpecialSyntaxServiceName(PsiElementUtils.getText(element));

// dont mark "@", "@?", "@@" escaping and expressions
if(serviceName.length() > 2 && !serviceName.startsWith("=") && !serviceName.startsWith("@")) {
if(!hasService(serviceName)) {
holder.registerProblem(element, INSPECTION_MESSAGE, ProblemHighlightType.GENERIC_ERROR_OR_WARNING);
}
if (serviceName.length() > 2 && !serviceName.startsWith("=") && !serviceName.startsWith("@") && !hasService(serviceName)) {
holder.registerProblem(element, INSPECTION_MESSAGE, ProblemHighlightType.GENERIC_ERROR_OR_WARNING);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,32 @@ public void testThatPhpServiceInterfaceForGetMethodIsInspected() {
);
}

public void testThatPhpAttributesForServiceAutowireIsInspected() {
assertLocalInspectionContains("test.php", "<?php\n" +
"use Symfony\\Component\\DependencyInjection\\Attribute\\Autowire;\n" +
"\n" +
"class HandlerCollection\n" +
"{\n" +
" public function __construct(\n" +
" #[Autowire(service: 'fo<caret>obar')]" +
" ) {}\n" +
"}",
MissingServiceInspection.INSPECTION_MESSAGE
);

assertLocalInspectionContains("test.php", "<?php\n" +
"use Symfony\\Component\\DependencyInjection\\Attribute\\Autowire;\n" +
"\n" +
"class HandlerCollection\n" +
"{\n" +
" public function __construct(\n" +
" #[Autowire(service: \"fo<caret>obar\")]" +
" ) {}\n" +
"}",
MissingServiceInspection.INSPECTION_MESSAGE
);
}

public void testThatYamlServiceInterfaceForGetMethodIsInspected() {
assertLocalInspectionContains("services.yml", "services:\n @args<caret>_unknown", MissingServiceInspection.INSPECTION_MESSAGE);
assertLocalInspectionContains("services.yml", "services:\n @Args<caret>_unknown", MissingServiceInspection.INSPECTION_MESSAGE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,9 @@ public function __construct($foobar)
{
}
}
}

namespace Symfony\Component\DependencyInjection\Attribute
{
class Autowire {}
}

0 comments on commit e48a3c5

Please sign in to comment.