Skip to content

Commit 8ca04d8

Browse files
Improve support for anonymous classes
1 parent 1ed8ba3 commit 8ca04d8

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

Application.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -753,12 +753,20 @@ protected function doRenderException(\Exception $e, OutputInterface $output)
753753
do {
754754
$message = trim($e->getMessage());
755755
if ('' === $message || OutputInterface::VERBOSITY_VERBOSE <= $output->getVerbosity()) {
756-
$title = sprintf(' [%s%s] ', \get_class($e), 0 !== ($code = $e->getCode()) ? ' ('.$code.')' : '');
756+
$class = \get_class($e);
757+
$class = 'c' === $class[0] && 0 === strpos($class, "class@anonymous\0") ? get_parent_class($class).'@anonymous' : $class;
758+
$title = sprintf(' [%s%s] ', $class, 0 !== ($code = $e->getCode()) ? ' ('.$code.')' : '');
757759
$len = Helper::strlen($title);
758760
} else {
759761
$len = 0;
760762
}
761763

764+
if (false !== strpos($message, "class@anonymous\0")) {
765+
$message = preg_replace_callback('/class@anonymous\x00.*?\.php0x?[0-9a-fA-F]++/', function ($m) {
766+
return \class_exists($m[0], false) ? get_parent_class($m[0]).'@anonymous' : $m[0];
767+
}, $message);
768+
}
769+
762770
$width = $this->terminal->getWidth() ? $this->terminal->getWidth() - 1 : PHP_INT_MAX;
763771
$lines = array();
764772
foreach ('' !== $message ? preg_split('/\r?\n/', $message) : array() as $line) {

Tests/ApplicationTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -824,6 +824,31 @@ public function testRenderExceptionLineBreaks()
824824
$this->assertStringMatchesFormatFile(self::$fixturesPath.'/application_renderexception_linebreaks.txt', $tester->getDisplay(true), '->renderException() keep multiple line breaks');
825825
}
826826

827+
public function testRenderAnonymousException()
828+
{
829+
$application = new Application();
830+
$application->setAutoExit(false);
831+
$application->register('foo')->setCode(function () {
832+
throw new class('') extends \InvalidArgumentException {
833+
};
834+
});
835+
$tester = new ApplicationTester($application);
836+
837+
$tester->run(array('command' => 'foo'), array('decorated' => false));
838+
$this->assertContains('[InvalidArgumentException@anonymous]', $tester->getDisplay(true));
839+
840+
$application = new Application();
841+
$application->setAutoExit(false);
842+
$application->register('foo')->setCode(function () {
843+
throw new \InvalidArgumentException(sprintf('Dummy type "%s" is invalid.', \get_class(new class() {
844+
})));
845+
});
846+
$tester = new ApplicationTester($application);
847+
848+
$tester->run(array('command' => 'foo'), array('decorated' => false));
849+
$this->assertContains('Dummy type "@anonymous" is invalid.', $tester->getDisplay(true));
850+
}
851+
827852
public function testRun()
828853
{
829854
$application = new Application();

0 commit comments

Comments
 (0)