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

Browse filesBrowse files
committed
Throw an exception when the method do not exist
1 parent f9c1e0d commit 9e7a97c
Copy full SHA for 9e7a97c

File tree

3 files changed

+57
-6
lines changed
Filter options

3 files changed

+57
-6
lines changed

‎src/Symfony/Component/Messenger/DependencyInjection/MessengerPass.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Messenger/DependencyInjection/MessengerPass.php
+10-4Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,16 +66,18 @@ private function registerHandlers(ContainerBuilder $container)
6666

6767
foreach ($container->findTaggedServiceIds($this->handlerTag, true) as $serviceId => $tags) {
6868
foreach ($tags as $tag) {
69+
$r = $container->getReflectionClass($container->getDefinition($serviceId)->getClass());
70+
6971
if (isset($tag['handles'])) {
7072
$handles = isset($tag['method']) ? array($tag['handles'] => $tag['method']) : array($tag['handles']);
7173
} else {
72-
$handles = $this->guessHandledClasses($r = $container->getReflectionClass($container->getDefinition($serviceId)->getClass()), $serviceId);
74+
$handles = $this->guessHandledClasses($r, $serviceId);
7375
}
7476

7577
$priority = $tag['priority'] ?? 0;
7678

7779
foreach ($handles as $messageClass => $method) {
78-
if (is_int($messageClass)) {
80+
if (\is_int($messageClass)) {
7981
$messageClass = $method;
8082
$method = '__invoke';
8183
}
@@ -87,17 +89,21 @@ private function registerHandlers(ContainerBuilder $container)
8789
$messagePriority = $priority;
8890
}
8991

90-
if (is_array($method)) {
92+
if (\is_array($method)) {
9193
$messagePriority = $method[1];
9294
$method = $method[0];
9395
}
9496

95-
if (!class_exists($messageClass)) {
97+
if (!\class_exists($messageClass)) {
9698
$messageClassLocation = isset($tag['handles']) ? 'declared in your tag attribute "handles"' : $r->implementsInterface(MessageHandlerInterface::class) ? sprintf('returned by method "%s::getHandledMessages()"', $r->getName()) : sprintf('used as argument type in method "%s::%s()"', $r->getName(), $method);
9799

98100
throw new RuntimeException(sprintf('Invalid handler service "%s": message class "%s" %s does not exist.', $serviceId, $messageClass, $messageClassLocation));
99101
}
100102

103+
if (!$r->hasMethod($method)) {
104+
throw new RuntimeException(sprintf('Invalid handler service "%s": method "%s::%s()" does not exist.', $serviceId, $r->getName(), $method));
105+
}
106+
101107
if ('__invoke' !== $method) {
102108
$wrapperDefinition = (new Definition(MethodOnObjectHandler::class))->setArguments(array(new Reference($serviceId), $method));
103109

‎src/Symfony/Component/Messenger/Handler/MethodOnObjectHandler.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Messenger/Handler/MethodOnObjectHandler.php
+4-2Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
* Used to transform an object and a method to a callable handler.
1616
*
1717
* @author Samuel Roze <samuel.roze@gmail.com>
18+
*
19+
* @internal
1820
*/
1921
final class MethodOnObjectHandler
2022
{
@@ -23,8 +25,8 @@ final class MethodOnObjectHandler
2325

2426
public function __construct($object, string $method)
2527
{
26-
if (!is_object($object)) {
27-
throw new \InvalidArgumentException(sprintf('Expected an object as argument but got %s', gettype($object)));
28+
if (!\is_object($object)) {
29+
throw new \InvalidArgumentException(sprintf('Expected an object as argument but got %s', \gettype($object)));
2830
}
2931

3032
$this->object = $object;

‎src/Symfony/Component/Messenger/Tests/DependencyInjection/MessengerPassTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Messenger/Tests/DependencyInjection/MessengerPassTest.php
+43Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,21 @@ public function testGetClassesAndMethodsAndPrioritiesFromTheSubscriber()
125125
$this->assertEquals(new Reference(PrioritizedHandler::class), $secondHandlerDefinition->getArgument(0)[1]);
126126
}
127127

128+
/**
129+
* @expectedException \Symfony\Component\DependencyInjection\Exception\RuntimeException
130+
* @expectedExceptionMessage Invalid handler service "Symfony\Component\Messenger\Tests\DependencyInjection\HandlerMappingWithNonExistentMethod": method "Symfony\Component\Messenger\Tests\DependencyInjection\HandlerMappingWithNonExistentMethod::dummyMethod()" does not exist.
131+
*/
132+
public function testThrowsExceptionIfTheHandlerMethodDoesNotExist()
133+
{
134+
$container = $this->getContainerBuilder();
135+
$container
136+
->register(HandlerMappingWithNonExistentMethod::class, HandlerMappingWithNonExistentMethod::class)
137+
->addTag('messenger.message_handler')
138+
;
139+
140+
(new MessengerPass())->process($container);
141+
}
142+
128143
public function testItRegistersReceivers()
129144
{
130145
$container = $this->getContainerBuilder();
@@ -368,6 +383,10 @@ public static function getHandledMessages(): array
368383
SecondMessage::class,
369384
);
370385
}
386+
387+
public function __invoke()
388+
{
389+
}
371390
}
372391

373392
class PrioritizedHandler implements MessageSubscriberInterface
@@ -378,6 +397,10 @@ public static function getHandledMessages(): array
378397
array(SecondMessage::class, 10),
379398
);
380399
}
400+
401+
public function __invoke()
402+
{
403+
}
381404
}
382405

383406
class HandlerMappingMethods implements MessageSubscriberInterface
@@ -389,6 +412,22 @@ public static function getHandledMessages(): array
389412
SecondMessage::class => array('secondMessage', 20),
390413
);
391414
}
415+
416+
public function dummyMethod()
417+
{}
418+
419+
public function secondMessage()
420+
{}
421+
}
422+
423+
class HandlerMappingWithNonExistentMethod implements MessageSubscriberInterface
424+
{
425+
public static function getHandledMessages(): array
426+
{
427+
return array(
428+
DummyMessage::class => 'dummyMethod',
429+
);
430+
}
392431
}
393432

394433
class HandleNoMessageHandler implements MessageSubscriberInterface
@@ -397,4 +436,8 @@ public static function getHandledMessages(): array
397436
{
398437
return array();
399438
}
439+
440+
public function __invoke()
441+
{
442+
}
400443
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.