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](https://github.com/symfony/symfony/pull/31493#issuecomment-492921480).

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
-------

bc3f598bfe Allow WrappedListener to describe uncallable listeners.
This commit is contained in:
Fabien Potencier 2019-05-25 10:29:54 +02:00
commit 5ecf80ab3c
2 changed files with 4 additions and 3 deletions

View File

@ -41,7 +41,7 @@ class WrappedListener
public function __construct($listener, ?string $name, Stopwatch $stopwatch, EventDispatcherInterface $dispatcher = null)
{
$this->listener = $listener;
$this->optimizedListener = $listener instanceof \Closure ? $listener : \Closure::fromCallable($listener);
$this->optimizedListener = $listener instanceof \Closure ? $listener : (\is_callable($listener) ? \Closure::fromCallable($listener) : null);
$this->stopwatch = $stopwatch;
$this->dispatcher = $dispatcher;
$this->called = false;
@ -123,7 +123,7 @@ class WrappedListener
$e = $this->stopwatch->start($this->name, 'event_listener');
($this->optimizedListener)($event, $eventName, $dispatcher);
($this->optimizedListener ?? $this->listener)($event, $eventName, $dispatcher);
if ($e->isStarted()) {
$e->stop();

View File

@ -21,7 +21,7 @@ class WrappedListenerTest extends TestCase
/**
* @dataProvider provideListenersToDescribe
*/
public function testListenerDescription(callable $listener, $expected)
public function testListenerDescription($listener, $expected)
{
$wrappedListener = new WrappedListener($listener, null, $this->getMockBuilder(Stopwatch::class)->getMock(), $this->getMockBuilder(EventDispatcherInterface::class)->getMock());
@ -34,6 +34,7 @@ class WrappedListenerTest extends TestCase
[new FooListener(), 'Symfony\Component\EventDispatcher\Tests\Debug\FooListener::__invoke'],
[[new FooListener(), 'listen'], 'Symfony\Component\EventDispatcher\Tests\Debug\FooListener::listen'],
[['Symfony\Component\EventDispatcher\Tests\Debug\FooListener', 'listenStatic'], 'Symfony\Component\EventDispatcher\Tests\Debug\FooListener::listenStatic'],
[['Symfony\Component\EventDispatcher\Tests\Debug\FooListener', 'invalidMethod'], 'Symfony\Component\EventDispatcher\Tests\Debug\FooListener::invalidMethod'],
['var_dump', 'var_dump'],
[function () {}, 'closure'],
[\Closure::fromCallable([new FooListener(), 'listen']), 'Symfony\Component\EventDispatcher\Tests\Debug\FooListener::listen'],