From c8409889fdbf48b99271930ea0ebcf3ee5e1ceae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20Pineau?= Date: Thu, 25 Jul 2024 15:57:40 +0200 Subject: [PATCH] [DependencyInjection] Do not try to load default method name on interface --- Compiler/PriorityTaggedServiceTrait.php | 4 ++++ Tests/Compiler/PriorityTaggedServiceTraitTest.php | 8 ++++++++ 2 files changed, 12 insertions(+) diff --git a/Compiler/PriorityTaggedServiceTrait.php b/Compiler/PriorityTaggedServiceTrait.php index 8c4d841f5..8d27303ee 100644 --- a/Compiler/PriorityTaggedServiceTrait.php +++ b/Compiler/PriorityTaggedServiceTrait.php @@ -133,6 +133,10 @@ public static function getDefault(ContainerBuilder $container, string $serviceId return null; } + if ($r->isInterface()) { + return null; + } + if (null !== $indexAttribute) { $service = $class !== $serviceId ? sprintf('service "%s"', $serviceId) : 'on the corresponding service'; $message = [sprintf('Either method "%s::%s()" should ', $class, $defaultMethod), sprintf(' or tag "%s" on %s is missing attribute "%s".', $tagName, $service, $indexAttribute)]; diff --git a/Tests/Compiler/PriorityTaggedServiceTraitTest.php b/Tests/Compiler/PriorityTaggedServiceTraitTest.php index 4d5ee1fb4..c39d79f9e 100644 --- a/Tests/Compiler/PriorityTaggedServiceTraitTest.php +++ b/Tests/Compiler/PriorityTaggedServiceTraitTest.php @@ -151,6 +151,8 @@ public function testTheIndexedTagsByDefaultIndexMethod() $container->register('service3', IntTagClass::class)->addTag('my_custom_tag'); + $container->register('service4', HelloInterface::class)->addTag('my_custom_tag'); + $priorityTaggedServiceTraitImplementation = new PriorityTaggedServiceTraitImplementation(); $tag = new TaggedIteratorArgument('my_custom_tag', 'foo', 'getFooBar'); @@ -158,6 +160,7 @@ public function testTheIndexedTagsByDefaultIndexMethod() 'bar_tab_class_with_defaultmethod' => new TypedReference('service2', BarTagClass::class), 'service1' => new TypedReference('service1', FooTagClass::class), '10' => new TypedReference('service3', IntTagClass::class), + 'service4' => new TypedReference('service4', HelloInterface::class), ]; $services = $priorityTaggedServiceTraitImplementation->test($tag, $container); $this->assertSame(array_keys($expected), array_keys($services)); @@ -244,3 +247,8 @@ class HelloNamedService extends \stdClass class HelloNamedService2 { } + +interface HelloInterface +{ + public static function getFooBar(): string; +}