Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ab136f0

Browse files
committed
bug #45677 [DependencyInjection] fix ServiceSubscriberTrait bug where parent has __call() (kbond)
This PR was merged into the 4.4 branch. Discussion ---------- [DependencyInjection] fix `ServiceSubscriberTrait` bug where parent has `__call()` | Q | A | ------------- | --- | Branch? | 4.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | n/a | License | MIT | Doc PR | n/a `is_callable(['parent', __FUNCTION__])` is true if the parent has magic `__call()/__callStatic` methods. Discovered this when trying to use `ServiceSubscriberTrait` with a Doctrine `ServiceEntityRepository`. Commits ------- 4b8cd52 [DI] fix `ServiceSubscriberTrait` bug where parent has `__call()`
2 parents d742b54 + 4b8cd52 commit ab136f0

File tree

2 files changed

+41
-2
lines changed

2 files changed

+41
-2
lines changed

src/Symfony/Contracts/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

src/Symfony/Contracts/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)