Skip to content

Commit 6b456a7

Browse files
derrabusnicolas-grekas
authored andcommitted
Parse and render anonymous classes correctly on php 8
1 parent b6443c9 commit 6b456a7

File tree

3 files changed

+15
-11
lines changed

3 files changed

+15
-11
lines changed

Application.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -863,17 +863,16 @@ private function doActuallyRenderThrowable(\Throwable $e, OutputInterface $outpu
863863
do {
864864
$message = trim($e->getMessage());
865865
if ('' === $message || OutputInterface::VERBOSITY_VERBOSE <= $output->getVerbosity()) {
866-
$class = \get_class($e);
867-
$class = 'c' === $class[0] && 0 === strpos($class, "class@anonymous\0") ? get_parent_class($class).'@anonymous' : $class;
866+
$class = get_debug_type($e);
868867
$title = sprintf(' [%s%s] ', $class, 0 !== ($code = $e->getCode()) ? ' ('.$code.')' : '');
869868
$len = Helper::strlen($title);
870869
} else {
871870
$len = 0;
872871
}
873872

874-
if (false !== strpos($message, "class@anonymous\0")) {
875-
$message = preg_replace_callback('/class@anonymous\x00.*?\.php(?:0x?|:[0-9]++\$)[0-9a-fA-F]++/', function ($m) {
876-
return class_exists($m[0], false) ? get_parent_class($m[0]).'@anonymous' : $m[0];
873+
if (false !== strpos($message, "@anonymous\0")) {
874+
$message = preg_replace_callback('/[a-zA-Z_\x7f-\xff][\\\\a-zA-Z0-9_\x7f-\xff]*+@anonymous\x00.*?\.php(?:0x?|:[0-9]++\$)[0-9a-fA-F]++/', function ($m) {
875+
return class_exists($m[0], false) ? (get_parent_class($m[0]) ?: key(class_implements($m[0])) ?: 'class').'@anonymous' : $m[0];
877876
}, $message);
878877
}
879878

Tests/ApplicationTest.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -897,7 +897,8 @@ public function testRenderAnonymousException()
897897
$application = new Application();
898898
$application->setAutoExit(false);
899899
$application->register('foo')->setCode(function () {
900-
throw new class('') extends \InvalidArgumentException { };
900+
throw new class('') extends \InvalidArgumentException {
901+
};
901902
});
902903
$tester = new ApplicationTester($application);
903904

@@ -907,20 +908,22 @@ public function testRenderAnonymousException()
907908
$application = new Application();
908909
$application->setAutoExit(false);
909910
$application->register('foo')->setCode(function () {
910-
throw new \InvalidArgumentException(sprintf('Dummy type "%s" is invalid.', \get_class(new class() { })));
911+
throw new \InvalidArgumentException(sprintf('Dummy type "%s" is invalid.', \get_class(new class() {
912+
})));
911913
});
912914
$tester = new ApplicationTester($application);
913915

914916
$tester->run(['command' => 'foo'], ['decorated' => false]);
915-
$this->assertStringContainsString('Dummy type "@anonymous" is invalid.', $tester->getDisplay(true));
917+
$this->assertStringContainsString('Dummy type "class@anonymous" is invalid.', $tester->getDisplay(true));
916918
}
917919

918920
public function testRenderExceptionStackTraceContainsRootException()
919921
{
920922
$application = new Application();
921923
$application->setAutoExit(false);
922924
$application->register('foo')->setCode(function () {
923-
throw new class('') extends \InvalidArgumentException { };
925+
throw new class('') extends \InvalidArgumentException {
926+
};
924927
});
925928
$tester = new ApplicationTester($application);
926929

@@ -930,12 +933,13 @@ public function testRenderExceptionStackTraceContainsRootException()
930933
$application = new Application();
931934
$application->setAutoExit(false);
932935
$application->register('foo')->setCode(function () {
933-
throw new \InvalidArgumentException(sprintf('Dummy type "%s" is invalid.', \get_class(new class() { })));
936+
throw new \InvalidArgumentException(sprintf('Dummy type "%s" is invalid.', \get_class(new class() {
937+
})));
934938
});
935939
$tester = new ApplicationTester($application);
936940

937941
$tester->run(['command' => 'foo'], ['decorated' => false]);
938-
$this->assertStringContainsString('Dummy type "@anonymous" is invalid.', $tester->getDisplay(true));
942+
$this->assertStringContainsString('Dummy type "class@anonymous" is invalid.', $tester->getDisplay(true));
939943
}
940944

941945
public function testRun()

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
"php": ">=7.1.3",
2020
"symfony/polyfill-mbstring": "~1.0",
2121
"symfony/polyfill-php73": "^1.8",
22+
"symfony/polyfill-php80": "^1.15",
2223
"symfony/service-contracts": "^1.1|^2"
2324
},
2425
"require-dev": {

0 commit comments

Comments
 (0)