Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 4b8cd52

Browse filesBrowse files
committed
[DI] fix ServiceSubscriberTrait bug where parent has __call()
1 parent bea4df7 commit 4b8cd52
Copy full SHA for 4b8cd52

File tree

Expand file treeCollapse file tree

2 files changed

+41
-2
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+41
-2
lines changed

‎src/Symfony/Contracts/Service/ServiceSubscriberTrait.php

Copy file name to clipboardExpand all lines: src/Symfony/Contracts/Service/ServiceSubscriberTrait.php
+2-2Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Contracts/Tests/Service/ServiceSubscriberTraitTest.php
+39Lines 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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.