Skip to content

Commit

Permalink
bug #36914 Parse and render anonymous classes correctly on php 8 (der…
Browse files Browse the repository at this point in the history
…rabus)

This PR was merged into the 4.4 branch.

Discussion
----------

Parse and render anonymous classes correctly on php 8

| Q             | A
| ------------- | ---
| Branch?       | 4.4
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       | #36872
| License       | MIT
| Doc PR        | N/A

The format of the value that `get_class()` returns for anonymous classes has changed in php 8. This PR attempts to detect both formats, with the help of the PHP80 polyfill where possible.

Commits
-------

9d702fd94b Parse and render anonymous classes correctly on php 8
  • Loading branch information
nicolas-grekas committed May 24, 2020
2 parents b8ec4c5 + d7aeaeb commit b8c48d6
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 11 deletions.
3 changes: 1 addition & 2 deletions Middleware/TraceableMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,7 @@ public function next(): MiddlewareInterface
if ($this->stack === $nextMiddleware = $this->stack->next()) {
$this->currentEvent = 'Tail';
} else {
$class = \get_class($nextMiddleware);
$this->currentEvent = sprintf('"%s"', 'c' === $class[0] && 0 === strpos($class, "class@anonymous\0") ? get_parent_class($class).'@anonymous' : $class);
$this->currentEvent = sprintf('"%s"', get_debug_type($nextMiddleware));
}
$this->currentEvent .= sprintf(' on "%s"', $this->busName);

Expand Down
19 changes: 11 additions & 8 deletions Tests/Middleware/TraceableMiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,16 @@ public function testHandle()
$busId = 'command_bus';
$envelope = new Envelope(new DummyMessage('Hello'));

$middleware = $this->getMockBuilder(MiddlewareInterface::class)->getMock();
$middleware->expects($this->once())
->method('handle')
->with($envelope, $this->anything())
->willReturnCallback(function ($envelope, StackInterface $stack) {
$middleware = new class() implements MiddlewareInterface {
public $calls = 0;

public function handle(Envelope $envelope, StackInterface $stack): Envelope
{
++$this->calls;

return $stack->next()->handle($envelope, $stack);
})
;
}
};

$stopwatch = $this->createMock(Stopwatch::class);
$stopwatch->expects($this->once())->method('isStarted')->willReturn(true);
Expand All @@ -51,14 +53,15 @@ public function testHandle()
$stopwatch->expects($this->exactly(2))
->method('stop')
->withConsecutive(
[$this->matches('"%sMiddlewareInterface%s" on "command_bus"')],
['"Symfony\Component\Messenger\Middleware\MiddlewareInterface@anonymous" on "command_bus"'],
['Tail on "command_bus"']
)
;

$traced = new TraceableMiddleware($stopwatch, $busId);

$traced->handle($envelope, new StackMiddleware(new \ArrayIterator([null, $middleware])));
$this->assertSame(1, $middleware->calls);
}

public function testHandleWithException()
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
],
"require": {
"php": ">=7.1.3",
"psr/log": "~1.0"
"psr/log": "~1.0",
"symfony/polyfill-php80": "^1.15"
},
"require-dev": {
"doctrine/dbal": "^2.6|^3.0",
Expand Down

0 comments on commit b8c48d6

Please sign in to comment.