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

Skip to content

Commit e55978a

Browse files
committed
bug #32199 [EventDispatcher] improve error messages in the event dispatcher (xabbuh)
This PR was merged into the 4.3 branch. Discussion ---------- [EventDispatcher] improve error messages in the event dispatcher | Q | A | ------------- | --- | Branch? | 4.3 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #32189 | License | MIT | Doc PR | Commits ------- 0b381db improve error messages in the event dispatcher
2 parents 1985a5c + 0b381db commit e55978a

File tree

4 files changed

+66
-13
lines changed

4 files changed

+66
-13
lines changed

src/Symfony/Component/EventDispatcher/EventDispatcher.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,15 +54,13 @@ public function dispatch($event/*, string $eventName = null*/)
5454

5555
if (\is_object($event)) {
5656
$eventName = $eventName ?? \get_class($event);
57-
} else {
58-
@trigger_error(sprintf('Calling the "%s::dispatch()" method with the event name as first argument is deprecated since Symfony 4.3, pass it second and provide the event object first instead.', EventDispatcherInterface::class), E_USER_DEPRECATED);
57+
} elseif (\is_string($event) && (null === $eventName || $eventName instanceof Event)) {
58+
@trigger_error(sprintf('Calling the "%s::dispatch()" method with the event name as the first argument is deprecated since Symfony 4.3, pass it as the second argument and provide the event object as the first argument instead.', EventDispatcherInterface::class), E_USER_DEPRECATED);
5959
$swap = $event;
6060
$event = $eventName ?? new Event();
6161
$eventName = $swap;
62-
63-
if (!$event instanceof Event) {
64-
throw new \TypeError(sprintf('Argument 1 passed to "%s::dispatch()" must be an instance of %s, %s given.', EventDispatcherInterface::class, Event::class, \is_object($event) ? \get_class($event) : \gettype($event)));
65-
}
62+
} else {
63+
throw new \TypeError(sprintf('Argument 1 passed to "%s::dispatch()" must be an object, %s given.', EventDispatcherInterface::class, \is_object($event) ? \get_class($event) : \gettype($event)));
6664
}
6765

6866
if (null !== $this->optimized && null !== $eventName) {

src/Symfony/Component/EventDispatcher/LegacyEventDispatcherProxy.php

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface as ContractsEventDispatcherInterface;
1717

1818
/**
19-
* An helper class to provide BC/FC with the legacy signature of EventDispatcherInterface::dispatch().
19+
* A helper class to provide BC/FC with the legacy signature of EventDispatcherInterface::dispatch().
2020
*
2121
* This class should be deprecated in Symfony 5.1
2222
*
@@ -57,15 +57,13 @@ public function dispatch($event/*, string $eventName = null*/)
5757

5858
if (\is_object($event)) {
5959
$eventName = $eventName ?? \get_class($event);
60-
} else {
61-
@trigger_error(sprintf('Calling the "%s::dispatch()" method with the event name as first argument is deprecated since Symfony 4.3, pass it second and provide the event object first instead.', ContractsEventDispatcherInterface::class), E_USER_DEPRECATED);
60+
} elseif (\is_string($event) && (null === $eventName || $eventName instanceof Event)) {
61+
@trigger_error(sprintf('Calling the "%s::dispatch()" method with the event name as the first argument is deprecated since Symfony 4.3, pass it as the second argument and provide the event object as the first argument instead.', ContractsEventDispatcherInterface::class), E_USER_DEPRECATED);
6262
$swap = $event;
6363
$event = $eventName ?? new Event();
6464
$eventName = $swap;
65-
66-
if (!$event instanceof Event) {
67-
throw new \TypeError(sprintf('Argument 1 passed to "%s::dispatch()" must be an instance of %s, %s given.', ContractsEventDispatcherInterface::class, Event::class, \is_object($event) ? \get_class($event) : \gettype($event)));
68-
}
65+
} else {
66+
throw new \TypeError(sprintf('Argument 1 passed to "%s::dispatch()" must be an object, %s given.', ContractsEventDispatcherInterface::class, \is_object($event) ? \get_class($event) : \gettype($event)));
6967
}
7068

7169
$listeners = $this->getListeners($eventName);

src/Symfony/Component/EventDispatcher/Tests/EventDispatcherTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,33 @@ public function testMutatingWhilePropagationIsStopped()
412412

413413
$this->assertTrue($testLoaded);
414414
}
415+
416+
/**
417+
* @group legacy
418+
* @expectedDeprecation Calling the "Symfony\Component\EventDispatcher\EventDispatcherInterface::dispatch()" method with the event name as the first argument is deprecated since Symfony 4.3, pass it as the second argument and provide the event object as the first argument instead.
419+
*/
420+
public function testLegacySignatureWithoutEvent()
421+
{
422+
$this->dispatcher->dispatch('foo');
423+
}
424+
425+
/**
426+
* @group legacy
427+
* @expectedDeprecation Calling the "Symfony\Component\EventDispatcher\EventDispatcherInterface::dispatch()" method with the event name as the first argument is deprecated since Symfony 4.3, pass it as the second argument and provide the event object as the first argument instead.
428+
*/
429+
public function testLegacySignatureWithEvent()
430+
{
431+
$this->dispatcher->dispatch('foo', new Event());
432+
}
433+
434+
/**
435+
* @expectedException \TypeError
436+
* @expectedExceptionMessage Argument 1 passed to "Symfony\Component\EventDispatcher\EventDispatcherInterface::dispatch()" must be an object, string given.
437+
*/
438+
public function testLegacySignatureWithNewEventObject()
439+
{
440+
$this->dispatcher->dispatch('foo', new ContractsEvent());
441+
}
415442
}
416443

417444
class CallableClass

src/Symfony/Component/EventDispatcher/Tests/LegacyEventDispatcherTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,42 @@
1414
use Symfony\Component\EventDispatcher\Event;
1515
use Symfony\Component\EventDispatcher\EventDispatcher;
1616
use Symfony\Component\EventDispatcher\LegacyEventDispatcherProxy;
17+
use Symfony\Contracts\EventDispatcher\Event as ContractsEvent;
1718

1819
/**
1920
* @group legacy
2021
*/
2122
class LegacyEventDispatcherTest extends EventDispatcherTest
2223
{
24+
/**
25+
* @group legacy
26+
* @expectedDeprecation The signature of the "Symfony\Component\EventDispatcher\Tests\TestLegacyEventDispatcher::dispatch()" method should be updated to "dispatch($event, string $eventName = null)", not doing so is deprecated since Symfony 4.3.
27+
* @expectedDeprecation Calling the "Symfony\Contracts\EventDispatcher\EventDispatcherInterface::dispatch()" method with the event name as the first argument is deprecated since Symfony 4.3, pass it as the second argument and provide the event object as the first argument instead.
28+
*/
29+
public function testLegacySignatureWithoutEvent()
30+
{
31+
$this->createEventDispatcher()->dispatch('foo');
32+
}
33+
34+
/**
35+
* @group legacy
36+
* @expectedDeprecation The signature of the "Symfony\Component\EventDispatcher\Tests\TestLegacyEventDispatcher::dispatch()" method should be updated to "dispatch($event, string $eventName = null)", not doing so is deprecated since Symfony 4.3.
37+
* @expectedDeprecation Calling the "Symfony\Contracts\EventDispatcher\EventDispatcherInterface::dispatch()" method with the event name as the first argument is deprecated since Symfony 4.3, pass it as the second argument and provide the event object as the first argument instead.
38+
*/
39+
public function testLegacySignatureWithEvent()
40+
{
41+
$this->createEventDispatcher()->dispatch('foo', new Event());
42+
}
43+
44+
/**
45+
* @expectedException \TypeError
46+
* @expectedExceptionMessage Argument 1 passed to "Symfony\Contracts\EventDispatcher\EventDispatcherInterface::dispatch()" must be an object, string given.
47+
*/
48+
public function testLegacySignatureWithNewEventObject()
49+
{
50+
$this->createEventDispatcher()->dispatch('foo', new ContractsEvent());
51+
}
52+
2353
protected function createEventDispatcher()
2454
{
2555
return LegacyEventDispatcherProxy::decorate(new TestLegacyEventDispatcher());

0 commit comments

Comments
 (0)