-
-
Notifications
You must be signed in to change notification settings - Fork 137
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2361 from Haehnchen/feature/service-missing-inspe…
…ction-language split MissingServiceInspection into language implentations to reduce wall time calls
- Loading branch information
Showing
4 changed files
with
89 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,81 +17,102 @@ | |
import fr.adrienbrault.idea.symfony2plugin.util.yaml.YamlHelper; | ||
import org.apache.commons.lang3.StringUtils; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.yaml.YAMLLanguage; | ||
|
||
/** | ||
* @author Daniel Espendiller <[email protected]> | ||
*/ | ||
public class MissingServiceInspection extends LocalInspectionTool { | ||
public class MissingServiceInspection { | ||
|
||
public static final String INSPECTION_MESSAGE = "Symfony: Missing Service"; | ||
|
||
@NotNull | ||
public PsiElementVisitor buildVisitor(final @NotNull ProblemsHolder holder, boolean isOnTheFly) { | ||
if (!Symfony2ProjectComponent.isEnabled(holder.getProject())) { | ||
return super.buildVisitor(holder, isOnTheFly); | ||
} | ||
public static class PhpLocalInspectionTool extends LocalInspectionTool { | ||
@Override | ||
public @NotNull PsiElementVisitor buildVisitor(@NotNull ProblemsHolder holder, boolean isOnTheFly) { | ||
if (!Symfony2ProjectComponent.isEnabled(holder.getProject())) { | ||
return super.buildVisitor(holder, isOnTheFly); | ||
} | ||
|
||
return new MyPsiElementVisitor(holder); | ||
} | ||
return new PsiElementVisitor() { | ||
private ContainerCollectionResolver.LazyServiceCollector lazyServiceCollector; | ||
|
||
@Override | ||
public void visitElement(@NotNull PsiElement element) { | ||
if(element.getLanguage() == PhpLanguage.INSTANCE && element instanceof StringLiteralExpression) { | ||
// PHP | ||
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) && !hasService(serviceName)) { | ||
holder.registerProblem(element, INSPECTION_MESSAGE, ProblemHighlightType.GENERIC_ERROR_OR_WARNING); | ||
} | ||
} | ||
|
||
// #[Autowire(service: 'foobar')] | ||
PsiElement leafText = PsiElementUtils.getTextLeafElementFromStringLiteralExpression((StringLiteralExpression) element); | ||
|
||
boolean isAttributeLeaf = leafText != null && ( | ||
PhpElementsUtil.getAttributeNamedArgumentStringPattern(ServiceContainerUtil.AUTOWIRE_ATTRIBUTE_CLASS, "service").accepts(leafText) | ||
|| PhpElementsUtil.getFirstAttributeStringPattern(ServiceContainerUtil.DECORATOR_ATTRIBUTE_CLASS).accepts(leafText) | ||
); | ||
|
||
if (isAttributeLeaf) { | ||
String serviceName = ((StringLiteralExpression) element).getContents(); | ||
if (StringUtils.isNotBlank(serviceName) && !hasService(serviceName)) { | ||
holder.registerProblem(element, INSPECTION_MESSAGE, ProblemHighlightType.GENERIC_ERROR_OR_WARNING); | ||
} | ||
} | ||
|
||
private static class MyPsiElementVisitor extends PsiElementVisitor { | ||
private final ProblemsHolder holder; | ||
private ContainerCollectionResolver.LazyServiceCollector lazyServiceCollector; | ||
} | ||
|
||
MyPsiElementVisitor(@NotNull ProblemsHolder holder) { | ||
this.holder = holder; | ||
} | ||
super.visitElement(element); | ||
} | ||
|
||
@Override | ||
public void visitElement(PsiElement element) { | ||
if(element.getLanguage() == PhpLanguage.INSTANCE && element instanceof StringLiteralExpression) { | ||
// PHP | ||
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) && !hasService(serviceName)) { | ||
holder.registerProblem(element, INSPECTION_MESSAGE, ProblemHighlightType.GENERIC_ERROR_OR_WARNING); | ||
private boolean hasService(@NotNull String serviceName) { | ||
if (this.lazyServiceCollector == null) { | ||
this.lazyServiceCollector = new ContainerCollectionResolver.LazyServiceCollector(holder.getProject()); | ||
} | ||
|
||
return ContainerCollectionResolver.hasServiceName(lazyServiceCollector, serviceName); | ||
} | ||
}; | ||
|
||
// #[Autowire(service: 'foobar')] | ||
PsiElement leafText = PsiElementUtils.getTextLeafElementFromStringLiteralExpression((StringLiteralExpression) element); | ||
} | ||
|
||
boolean isAttributeLeaf = leafText != null && ( | ||
PhpElementsUtil.getAttributeNamedArgumentStringPattern(ServiceContainerUtil.AUTOWIRE_ATTRIBUTE_CLASS, "service").accepts(leafText) | ||
|| PhpElementsUtil.getFirstAttributeStringPattern(ServiceContainerUtil.DECORATOR_ATTRIBUTE_CLASS).accepts(leafText) | ||
); | ||
} | ||
|
||
if (isAttributeLeaf) { | ||
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 | ||
public static class YamlLocalInspectionTool extends LocalInspectionTool { | ||
@Override | ||
public @NotNull PsiElementVisitor buildVisitor(@NotNull ProblemsHolder holder, boolean isOnTheFly) { | ||
if (!Symfony2ProjectComponent.isEnabled(holder.getProject())) { | ||
return super.buildVisitor(holder, isOnTheFly); | ||
} | ||
|
||
return new PsiElementVisitor() { | ||
private ContainerCollectionResolver.LazyServiceCollector lazyServiceCollector; | ||
|
||
if (YamlElementPatternHelper.getServiceDefinition().accepts(element) && YamlElementPatternHelper.getInsideServiceKeyPattern().accepts(element)) { | ||
String serviceName = YamlHelper.trimSpecialSyntaxServiceName(PsiElementUtils.getText(element)); | ||
@Override | ||
public void visitElement(@NotNull PsiElement 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("@") && !hasService(serviceName)) { | ||
holder.registerProblem(element, INSPECTION_MESSAGE, ProblemHighlightType.GENERIC_ERROR_OR_WARNING); | ||
// dont mark "@", "@?", "@@" escaping and expressions | ||
if (serviceName.length() > 2 && !serviceName.startsWith("=") && !serviceName.startsWith("@") && !hasService(serviceName)) { | ||
holder.registerProblem(element, INSPECTION_MESSAGE, ProblemHighlightType.GENERIC_ERROR_OR_WARNING); | ||
} | ||
} | ||
} | ||
} | ||
|
||
super.visitElement(element); | ||
} | ||
super.visitElement(element); | ||
} | ||
|
||
private boolean hasService(@NotNull String serviceName) { | ||
if (this.lazyServiceCollector == null) { | ||
this.lazyServiceCollector = new ContainerCollectionResolver.LazyServiceCollector(holder.getProject()); | ||
} | ||
private boolean hasService(@NotNull String serviceName) { | ||
if (this.lazyServiceCollector == null) { | ||
this.lazyServiceCollector = new ContainerCollectionResolver.LazyServiceCollector(holder.getProject()); | ||
} | ||
|
||
return ContainerCollectionResolver.hasServiceName(lazyServiceCollector, serviceName); | ||
return ContainerCollectionResolver.hasServiceName(lazyServiceCollector, serviceName); | ||
} | ||
}; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
5 changes: 5 additions & 0 deletions
5
src/main/resources/inspectionDescriptions/MissingServiceYaml.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<html> | ||
<body> | ||
No Symfony service with given id was found. See <a href="https://symfony.com/doc/current/service_container.html?phpstorm">Symfony documentation</a> for more help | ||
</body> | ||
</html> |