Skip to content

Commit 21c1a01

Browse files
committed
[DI] fix ServiceSubscriberTrait bug where parent has __call()
1 parent d760c3a commit 21c1a01

File tree

2 files changed

+41
-2
lines changed

2 files changed

+41
-2
lines changed

Service/ServiceSubscriberTrait.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public static function getSubscribedServices(): array
3535
return $services;
3636
}
3737

38-
$services = \is_callable(['parent', __FUNCTION__]) ? parent::getSubscribedServices() : [];
38+
$services = method_exists(get_parent_class(self::class) ?: '', __FUNCTION__) ? parent::getSubscribedServices() : [];
3939

4040
foreach ((new \ReflectionClass(self::class))->getMethods() as $method) {
4141
if ($method->isStatic() || $method->isAbstract() || $method->isGenerator() || $method->isInternal() || $method->getNumberOfRequiredParameters()) {
@@ -69,7 +69,7 @@ public function setContainer(ContainerInterface $container)
6969
{
7070
$this->container = $container;
7171

72-
if (\is_callable(['parent', __FUNCTION__])) {
72+
if (method_exists(get_parent_class(self::class) ?: '', __FUNCTION__)) {
7373
return parent::setContainer($container);
7474
}
7575

Tests/Service/ServiceSubscriberTraitTest.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,32 @@ public function testSetContainerIsCalledOnParent()
3636
$this->assertSame($container, (new TestService())->setContainer($container));
3737
}
3838

39+
public function testParentNotCalledIfHasMagicCall()
40+
{
41+
$container = new class([]) implements ContainerInterface {
42+
use ServiceLocatorTrait;
43+
};
44+
$service = new class() extends ParentWithMagicCall {
45+
use ServiceSubscriberTrait;
46+
};
47+
48+
$this->assertNull($service->setContainer($container));
49+
$this->assertSame([], $service::getSubscribedServices());
50+
}
51+
52+
public function testParentNotCalledIfNoParent()
53+
{
54+
$container = new class([]) implements ContainerInterface {
55+
use ServiceLocatorTrait;
56+
};
57+
$service = new class() {
58+
use ServiceSubscriberTrait;
59+
};
60+
61+
$this->assertNull($service->setContainer($container));
62+
$this->assertSame([], $service::getSubscribedServices());
63+
}
64+
3965
/**
4066
* @requires PHP 8
4167
*/
@@ -77,3 +103,16 @@ public function aChildService(): Service3
77103
{
78104
}
79105
}
106+
107+
class ParentWithMagicCall
108+
{
109+
public function __call($method, $args)
110+
{
111+
throw new \BadMethodCallException('Should not be called.');
112+
}
113+
114+
public static function __callStatic($method, $args)
115+
{
116+
throw new \BadMethodCallException('Should not be called.');
117+
}
118+
}

0 commit comments

Comments
 (0)