8000 bug #43781 [Messenger] Fix `TraceableMessageBus` implementation so it… · symfony/symfony@36f8fe5 · GitHub
[go: up one dir, main page]

Skip to content

Commit 36f8fe5

Browse files
bug #43781 [Messenger] Fix TraceableMessageBus implementation so it can compute caller even when used within a callback (Ocramius)
This PR was squashed before being merged into the 4.4 branch. Discussion ---------- [Messenger] Fix `TraceableMessageBus` implementation so it can compute caller even when used within a callback | Q | A | ------------- | --- | Branch? | 4.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | | License | MIT | Doc PR | Quoting commits: > > This test shows that `array_map([new TraceableMessageBus(...), 'dispatch'], $messages)` does not work at all, > because `debug_backtrace()` is used in internals to compute the call-site, and it is not considering the fact > that the caller may be: > > * an internal PHP function > * a callback (this example test scenario) > * generated code (`eval()` result) > > The output of such a failure: > > ``` > 1) Symfony\Component\Messenger\Tests\TraceableMessageBusTest::testItTracesExceptionsWhenMessageBusIsFiredFromArrayCallback > Undefined array key "line" > > /app/src/Symfony/Component/Messenger/TraceableMessageBus.php:66 > /app/src/Symfony/Component/Messenger/TraceableMessageBus.php:36 > /app/src/Symfony/Component/Messenger/Tests/TraceableMessageBusTest.php:175 > ``` > > To be on the safe side, also removed `compact()` usage, which was making everything > much muddier and harder to comprehend. Commits ------- 1423229 [Messenger] Fix `TraceableMessageBus` implementation so it can compute caller even when used within a callback
2 parents 822e9e5 + 1423229 commit 36f8fe5

File tree

2 files changed

+26
-5
lines changed

2 files changed

+26
-5
lines changed

src/Symfony/Component/Messenger/Tests/TraceableMessageBusTest.php

+18
Original file line numberDiff line numberDiff line change
@@ -156,4 +156,22 @@ public function testItTracesExceptions()
156156
],
157157
], $actualTracedMessage);
158158
}
159+
160+
public function testItTracesExceptionsWhenMessageBusIsFiredFromArrayCallback()
161+
{
162+
$message = new DummyMessage('Hello');
163+
$exception = new \RuntimeException();
164+
165+
$bus = $this->createMock(MessageBusInterface::class);
166+
$bus->expects($this->once())
167+
->method('dispatch')
168+
->with($message)
169+
->willThrowException($exception);
170+
171+
$traceableBus = new TraceableMessageBus($bus);
172+
173+
$this->expectExceptionObject($exception);
174+
175+
array_map([$traceableBus, 'dispatch'], [$message]);
176+
}
159177
}

src/Symfony/Component/Messenger/TraceableMessageBus.php

+8-5
Original file line numberDiff line numberDiff line 8000 change
@@ -62,8 +62,8 @@ private function getCaller(): array
6262
{
6363
$trace = debug_backtrace(\DEBUG_BACKTRACE_IGNORE_ARGS, 8);
6464

65-
$file = $trace[1]['file'];
66-
$line = $trace[1]['line'];
65+
$file = $trace[1]['file'] ?? null;
66+
$line = $trace[1]['line'] ?? null;
6767

6868
$handleTraitFile = (new \ReflectionClass(HandleTrait::class))->getFileName();
6969
$found = false;
@@ -97,9 +97,12 @@ private function getCaller(): array
9797
}
9898
}
9999

100-
$name = str_replace('\\', '/', $file);
101-
$name = substr($name, strrpos($name, '/') + 1);
100+
$name = str_replace('\\', '/', (string) $file);
102101

103-
return compact('name', 'file', 'line');
102+
return [
103+
'name' => substr($name, strrpos($name, '/') + 1),
104+
'file' => $file,
105+
'line' => $line,
106+
];
104107
}
105108
}

0 commit comments

Comments
 (0)
0