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

Skip to content

Commit d901c6d

Browse files
committed
feature #28909 [Messenger] made dispatch() and handle() return void (nicolas-grekas)
This PR was merged into the 4.2-dev branch. Discussion ---------- [Messenger] made dispatch() and handle() return void | Q | A | ------------- | --- | Branch? | 4.2 | Bug fix? | no | New feature? | no | BC breaks? | yes | Deprecations? | no | Tests pass? | yes | Fixed tickets | - | License | MIT | Doc PR | - Middlewares and dispatchers should not return any value. Forwarding back the results from handlers breaks the scope of the component. The canonical example of some bad design this can lead to is `ChainHandler`: how can the caller know that there is one in the chain and that it should expect an array as a return value? More generally, how can a caller know what to expect back from a call to dispatch()? I think we should not allow such broken designs. Instead, we should favor east-oriented design: if one needs a command bus, one would have to dispatch a proper command object - and if a result is expected back, it should be done via a setter on the dispatched command - or better, a callback set on the command object. This way we play *by the rules* of the type-system, not against. Commits ------- f942ffc [Messenger] made dispatch() and handle() return void
2 parents 316e95c + f942ffc commit d901c6d

28 files changed

+71
-173
lines changed

src/Symfony/Bridge/Doctrine/Messenger/DoctrineTransactionMiddleware.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public function __construct(ManagerRegistry $managerRegistry, ?string $entityMan
3131
$this->entityManagerName = $entityManagerName;
3232
}
3333

34-
public function handle($message, callable $next)
34+
public function handle($message, callable $next): void
3535
{
3636
$entityManager = $this->managerRegistry->getManager($this->entityManagerName);
3737

@@ -41,15 +41,13 @@ public function handle($message, callable $next)
4141

4242
$entityManager->getConnection()->beginTransaction();
4343
try {
44-
$result = $next($message);
44+
$next($message);
4545
$entityManager->flush();
4646
$entityManager->getConnection()->commit();
4747
} catch (\Throwable $exception) {
4848
$entityManager->getConnection()->rollBack();
4949

5050
throw $exception;
5151
}
52-
53-
return $result;
5452
}
5553
}

src/Symfony/Bundle/WebProfilerBundle/Resources/views/Collector/messenger.html.twig

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -168,16 +168,6 @@
168168
{% endfor %}
169169
</td>
170170
</tr>
171-
<tr>
172-
<td class="text-bold">Result</td>
173-
<td>
174-
{% if dispatchCall.result is defined %}
175-
{{ profiler_dump(dispatchCall.result.seek('value'), maxDepth=2) }}
176-
{% elseif dispatchCall.exception is defined %}
177-
<span class="text-danger">No result as an exception occurred</span>
178-
{% endif %}
179-
</td>
180-
</tr>
181171
{% if dispatchCall.exception is defined %}
182172
<tr>
183173
<td class="text-bold">Exception</td>

src/Symfony/Component/Messenger/Asynchronous/Middleware/SendMessageMiddleware.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,13 @@ public function __construct(SenderLocatorInterface $senderLocator, array $messag
3838
*
3939
* {@inheritdoc}
4040
*/
41-
public function handle($envelope, callable $next)
41+
public function handle($envelope, callable $next): void
4242
{
4343
if ($envelope->get(ReceivedStamp::class)) {
4444
// It's a received message. Do not send it back:
45-
return $next($envelope);
45+
$next($envelope);
46+
47+
return;
4648
}
4749

4850
$sender = $this->senderLocator->getSenderForMessage($envelope->getMessage());
@@ -55,7 +57,7 @@ public function handle($envelope, callable $next)
5557
}
5658
}
5759

58-
return $next($envelope);
60+
$next($envelope);
5961
}
6062

6163
private function mustSendAndHandle($message): bool

src/Symfony/Component/Messenger/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ CHANGELOG
66

77
* The component is not experimental anymore
88
* All the changes below are BC BREAKS
9+
* `MessageBusInterface::dispatch()` and `MiddlewareInterface::handle()` now return `void`
910
* The signature of `Amqp*` classes changed to take a `Connection` as a first argument and an optional
1011
`Serializer` as a second argument.
1112
* `SenderLocator` has been renamed to `ContainerSenderLocator`

src/Symfony/Component/Messenger/DataCollector/MessengerDataCollector.php

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -98,14 +98,6 @@ private function collectMessage(string $busName, array $tracedMessage)
9898
'caller' => $tracedMessage['caller'],
9999
);
100100

101-
if (array_key_exists('result', $tracedMessage)) {
102-
$result = $tracedMessage['result'];
103-
$debugRepresentation['result'] = array(
104-
'type' => \is_object($result) ? \get_class($result) : \gettype($result),
105-
'value' => $result,
106-
);
107-
}
108-
109101
if (isset($tracedMessage['exception'])) {
110102
$exception = $tracedMessage['exception'];
111103

src/Symfony/Component/Messenger/Handler/ChainHandler.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,8 @@ public function __construct(array $handlers)
3939

4040
public function __invoke($message)
4141
{
42-
$results = array();
43-
4442
foreach ($this->handlers as $handler) {
45-
$results[] = $handler($message);
43+
$handler($message);
4644
}
47-
48-
return $results;
4945
}
5046
}

src/Symfony/Component/Messenger/MessageBus.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,13 @@ public function __construct(iterable $middlewareHandlers = array())
3838
/**
3939
* {@inheritdoc}
4040
*/
41-
public function dispatch($message)
41+
public function dispatch($message): void
4242
{
4343
if (!\is_object($message)) {
4444
throw new InvalidArgumentException(sprintf('Invalid type for message argument. Expected object, but got "%s".', \gettype($message)));
4545
}
4646

47-
return \call_user_func($this->callableForNextMiddleware(0, Envelope::wrap($message)), $message);
47+
\call_user_func($this->callableForNextMiddleware(0, Envelope::wrap($message)), $message);
4848
}
4949

5050
private function callableForNextMiddleware(int $index, Envelope $currentEnvelope): callable
@@ -71,7 +71,7 @@ private function callableForNextMiddleware(int $index, Envelope $currentEnvelope
7171
$message = $message->getMessage();
7272
}
7373

74-
return $middleware->handle($message, $this->callableForNextMiddleware($index + 1, $currentEnvelope));
74+
$middleware->handle($message, $this->callableForNextMiddleware($index + 1, $currentEnvelope));
7575
};
7676
}
7777
}

src/Symfony/Component/Messenger/MessageBusInterface.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,7 @@ interface MessageBusInterface
1919
/**
2020
* Dispatches the given message.
2121
*
22-
* The bus can return a value coming from handlers, but is not required to do so.
23-
*
2422
* @param object|Envelope $message The message or the message pre-wrapped in an envelope
25-
*
26-
* @return mixed
2723
*/
28-
public function dispatch($message);
24+
public function dispatch($message): void;
2925
}

src/Symfony/Component/Messenger/Middleware/AllowNoHandlerMiddleware.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@
1818
*/
1919
class AllowNoHandlerMiddleware implements MiddlewareInterface
2020
{
21-
public function handle($message, callable $next)
21+
public function handle($message, callable $next): void
2222
{
2323
try {
24-
return $next($message);
24+
$next($message);
2525
} catch (NoHandlerForMessageException $e) {
2626
// We allow not having a handler for this message.
2727
}

src/Symfony/Component/Messenger/Middleware/Enhancers/ActivationMiddlewareDecorator.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,12 @@ public function __construct(MiddlewareInterface $inner, $activated)
3737
/**
3838
* @param Envelope $envelope
3939
*/
40-
public function handle($envelope, callable $next)
40+
public function handle($envelope, callable $next): void
4141
{
4242
if (\is_callable($this->activated) ? ($this->activated)($envelope) : $this->activated) {
43-
return $this->inner->handle($envelope->getMessageFor($this->inner), $next);
43+
$this->inner->handle($envelope->getMessageFor($this->inner), $next);
44+
} else {
45+
$next($envelope);
4446
}
45-
46-
return $next($envelope);
4747
}
4848
}

src/Symfony/Component/Messenger/Middleware/Enhancers/TraceableMiddleware.php

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public function __construct(MiddlewareInterface $inner, Stopwatch $stopwatch, st
3939
/**
4040
* @param Envelope $envelope
4141
*/
42-
public function handle($envelope, callable $next)
42+
public function handle($envelope, callable $next): void
4343
{
4444
$class = \get_class($this->inner);
4545
$eventName = 'c' === $class[0] && 0 === strpos($class, "class@anonymous\0") ? get_parent_class($class).'@anonymous' : $class;
@@ -51,19 +51,15 @@ public function handle($envelope, callable $next)
5151
$this->stopwatch->start($eventName, $this->eventCategory);
5252

5353
try {
54-
$result = $this->inner->handle($envelope->getMessageFor($this->inner), function ($message) use ($next, $eventName) {
54+
$this->inner->handle($envelope->getMessageFor($this->inner), function ($message) use ($next, $eventName) {
5555
$this->stopwatch->stop($eventName);
56-
$result = $next($message);
56+
$next($message);
5757
$this->stopwatch->start($eventName, $this->eventCategory);
58-
59-
return $result;
6058
});
6159
} finally {
6260
if ($this->stopwatch->isStarted($eventName)) {
6361
$this->stopwatch->stop($eventName);
6462
}
6563
}
66-
67-
return $result;
6864
}
6965
}

src/Symfony/Component/Messenger/Middleware/HandleMessageMiddleware.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,11 @@ public function __construct(HandlerLocatorInterface $messageHandlerResolver)
2828
/**
2929
* {@inheritdoc}
3030
*/
31-
public function handle($message, callable $next)
31+
public function handle($message, callable $next): void
3232
{
3333
$handler = $this->messageHandlerResolver->resolve($message);
34-
$result = $handler($message);
34+
$handler($message);
3535

3636
$next($message);
37-
38-
return $result;
3937
}
4038
}

src/Symfony/Component/Messenger/Middleware/LoggingMiddleware.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,12 @@ public function __construct(LoggerInterface $logger)
2828
/**
2929
* {@inheritdoc}
3030
*/
31-
public function handle($message, callable $next)
31+
public function handle($message, callable $next): void
3232
{
3333
$this->logger->debug('Starting handling message {class}', $this->createContext($message));
3434

3535
try {
36-
$result = $next($message);
36+
$next($message);
3737
} catch (\Throwable $e) {
3838
$this->logger->warning('An exception occurred while handling message {class}', array_merge(
3939
$this->createContext($message),
@@ -44,8 +44,6 @@ public function handle($message, callable $next)
4444
}
4545

4646
$this->logger->debug('Finished handling message {class}', $this->createContext($message));
47-
48-
return $result;
4947
}
5048

5149
private function createContext($message): array

src/Symfony/Component/Messenger/Middleware/MiddlewareInterface.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ interface MiddlewareInterface
1818
{
1919
/**
2020
* @param object $message
21-
*
22-
* @return mixed
2321
*/
24-
public function handle($message, callable $next);
22+
public function handle($message, callable $next): void;
2523
}

src/Symfony/Component/Messenger/Middleware/ValidationMiddleware.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public function __construct(ValidatorInterface $validator)
3232
/**
3333
* @param Envelope $envelope
3434
*/
35-
public function handle($envelope, callable $next)
35+
public function handle($envelope, callable $next): void
3636
{
3737
$message = $envelope->getMessage();
3838
$groups = null;
@@ -46,6 +46,6 @@ public function handle($envelope, callable $next)
4646
throw new ValidationFailedException($message, $violations);
4747
}
4848

49-
return $next($envelope);
49+
$next($envelope);
5050
}
5151
}

src/Symfony/Component/Messenger/Tests/DataCollector/MessengerDataCollectorTest.php

Lines changed: 6 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,12 @@ protected function setUp()
3333
$this->dumper->setColors(false);
3434
}
3535

36-
/**
37-
* @dataProvider getHandleTestData
38-
*/
39-
public function testHandle($returnedValue, $expected)
36+
public function testHandle()
4037
{
4138
$message = new DummyMessage('dummy message');
4239

4340
$bus = $this->getMockBuilder(MessageBusInterface::class)->getMock();
44-
$bus->method('dispatch')->with($message)->willReturn($returnedValue);
41+
$bus->method('dispatch')->with($message);
4542
$bus = new TraceableMessageBus($bus);
4643

4744
$collector = new MessengerDataCollector();
@@ -54,13 +51,9 @@ public function testHandle($returnedValue, $expected)
5451
$messages = $collector->getMessages();
5552
$this->assertCount(1, $messages);
5653

57-
$this->assertStringMatchesFormat($expected, $this->getDataAsString($messages[0]));
58-
}
59-
60-
public function getHandleTestData()
61-
{
6254
$file = __FILE__;
63-
$messageDump = <<<DUMP
55+
$expected = <<<DUMP
56+
array:4 [
6457
"bus" => "default"
6558
"stamps" => null
6659
"message" => array:2 [
@@ -74,48 +67,10 @@ public function getHandleTestData()
7467
"file" => "$file"
7568
"line" => %d
7669
]
77-
DUMP;
78-
79-
yield 'no returned value' => array(
80-
null,
81-
<<<DUMP
82-
array:5 [
83-
$messageDump
84-
"result" => array:2 [
85-
"type" => "NULL"
86-
"value" => null
87-
]
88-
]
89-
DUMP
90-
);
91-
92-
yield 'scalar returned value' => array(
93-
'returned value',
94-
<<<DUMP
95-
array:5 [
96-
$messageDump
97-
"result" => array:2 [
98-
"type" => "string"
99-
"value" => "returned value"
100-
]
10170
]
102-
DUMP
103-
);
71+
DUMP;
10472

105-
yield 'array returned value' => array(
106-
array('returned value'),
107-
<<<DUMP
108-
array:5 [
109-
$messageDump
110-
"result" => array:2 [
111-
"type" => "array"
112-
"value" => array:1 [
113-
0 => "returned value"
114-
]
115-
]
116-
]
117-
DUMP
118-
);
73+
$this->assertStringMatchesFormat($expected, $this->getDataAsString($messages[0]));
11974
}
12075

12176
public function testHandleWithException()

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -852,8 +852,8 @@ public function dummyMethodForSomeBus()
852852

853853
class UselessMiddleware implements MiddlewareInterface
854854
{
855-
public function handle($message, callable $next)
855+
public function handle($message, callable $next): void
856856
{
857-
return $next($message);
857+
$next($message);
858858
}
859859
}

src/Symfony/Component/Messenger/Tests/Handler/ChainHandlerTest.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
class ChainHandlerTest extends TestCase
1919
{
20-
public function testItCallsTheHandlersAndReturnsAllResults()
20+
public function testItCallsTheHandlers()
2121
{
2222
$message = new DummyMessage('Hey');
2323

@@ -26,19 +26,15 @@ public function testItCallsTheHandlersAndReturnsAllResults()
2626
->expects($this->once())
2727
->method('__invoke')
2828
->with($message)
29-
->willReturn('Hello')
3029
;
3130
$handler2 = $this->createPartialMock(\stdClass::class, array('__invoke'));
3231
$handler2
3332
->expects($this->once())
3433
->method('__invoke')
3534
->with($message)
36-
->willReturn('World')
3735
;
3836

39-
$results = (new ChainHandler(array($handler1, $handler2)))($message);
40-
41-
$this->assertSame(array('Hello', 'World'), $results);
37+
(new ChainHandler(array($handler1, $handler2)))($message);
4238
}
4339

4440
/**

0 commit comments

Comments
 (0)