Skip to content

Commit 5ecf80a

Browse files
committed
bug #31615 Allow WrappedListener to describe uncallable listeners (derrabus)
This PR was merged into the 4.3 branch. Discussion ---------- Allow WrappedListener to describe uncallable listeners | Q | A | ------------- | --- | Branch? | 4.3 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #31493 | License | MIT | Doc PR | N/A This is a follow-up to #31493. The previous PR did not fix the problem completely. We also need to make sure that a listener that is not callable is not passed to `Closure::fromCallable()`. Note: It would probably be a good idea to give the developer a hint about uncallable listeners. Currently, such a listener causes an exception at the worst possible point of time: when collecting the profile information. This breaks the toolbar without any helpful feedback, as I've described [here](#31493 (comment)). This PR allows the `WrappedListener` class to describe a listener for the profiler even if the listener is not callable, which was the behavior in Symfony 4.2. Commits ------- bc3f598 Allow WrappedListener to describe uncallable listeners.
2 parents af0f955 + bc3f598 commit 5ecf80a

File tree

2 files changed

+4
-3
lines changed

2 files changed

+4
-3
lines changed

src/Symfony/Component/EventDispatcher/Debug/WrappedListener.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class WrappedListener
4141
public function __construct($listener, ?string $name, Stopwatch $stopwatch, EventDispatcherInterface $dispatcher = null)
4242
{
4343
$this->listener = $listener;
44-
$this->optimizedListener = $listener instanceof \Closure ? $listener : \Closure::fromCallable($listener);
44+
$this->optimizedListener = $listener instanceof \Closure ? $listener : (\is_callable($listener) ? \Closure::fromCallable($listener) : null);
4545
$this->stopwatch = $stopwatch;
4646
$this->dispatcher = $dispatcher;
4747
$this->called = false;
@@ -123,7 +123,7 @@ public function __invoke(Event $event, $eventName, EventDispatcherInterface $dis
123123

124124
$e = $this->stopwatch->start($this->name, 'event_listener');
125125

126-
($this->optimizedListener)($event, $eventName, $dispatcher);
126+
($this->optimizedListener ?? $this->listener)($event, $eventName, $dispatcher);
127127

128128
if ($e->isStarted()) {
129129
$e->stop();

src/Symfony/Component/EventDispatcher/Tests/Debug/WrappedListenerTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class WrappedListenerTest extends TestCase
2121
/**
2222
* @dataProvider provideListenersToDescribe
2323
*/
24-
public function testListenerDescription(callable $listener, $expected)
24+
public function testListenerDescription($listener, $expected)
2525
{
2626
$wrappedListener = new WrappedListener($listener, null, $this->getMockBuilder(Stopwatch::class)->getMock(), $this->getMockBuilder(EventDispatcherInterface::class)->getMock());
2727

@@ -34,6 +34,7 @@ public function provideListenersToDescribe()
3434
[new FooListener(), 'Symfony\Component\EventDispatcher\Tests\Debug\FooListener::__invoke'],
3535
[[new FooListener(), 'listen'], 'Symfony\Component\EventDispatcher\Tests\Debug\FooListener::listen'],
3636
[['Symfony\Component\EventDispatcher\Tests\Debug\FooListener', 'listenStatic'], 'Symfony\Component\EventDispatcher\Tests\Debug\FooListener::listenStatic'],
37+
[['Symfony\Component\EventDispatcher\Tests\Debug\FooListener', 'invalidMethod'], 'Symfony\Component\EventDispatcher\Tests\Debug\FooListener::invalidMethod'],
3738
['var_dump', 'var_dump'],
3839
[function () {}, 'closure'],
3940
[\Closure::fromCallable([new FooListener(), 'listen']), 'Symfony\Component\EventDispatcher\Tests\Debug\FooListener::listen'],

0 commit comments

Comments
 (0)