8000 minor #35441 Improve displaying anonymous classes (nicolas-grekas) · symfony/symfony@af295b5 · GitHub
[go: up one dir, main page]

Skip to content

Commit af295b5

Browse files
minor #35441 Improve displaying anonymous classes (nicolas-grekas)
This PR was merged into the 5.1-dev branch. Discussion ---------- Improve displaying anonymous classes | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | no | Deprecations? | no | Tickets | - | License | MIT | Doc PR | - When an anonymous class extends a base `Foo` class, we display them as `Foo@anonymous`. When one doesn't extend any base class, we display `@anonymous`. This PR improves the situation by displaying `FooInterface@anonymous` when an anonymous class doesn't extend any base class but implements `FooInterface`. Commits ------- 487bcc6 Improve displaying anonymous classes
2 parents 524ee7a + 487bcc6 commit af295b5

File tree

11 files changed

+16
-16
lines changed

11 files changed

+16
-16
lines changed

src/Symfony/Component/Console/Application.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -776,7 +776,7 @@ protected function doRenderThrowable(\Throwable $e, OutputInterface $output): vo
776776
$message = trim($e->getMessage());
777777
if ('' === $message || OutputInterface::VERBOSITY_VERBOSE <= $output->getVerbosity()) {
778778
$class = \get_class($e);
779-
$class = 'c' === $class[0] && 0 === strpos($class, "class@anonymous\0") ? get_parent_class($class).'@anonymous' : $class;
779+
$class = 'c' === $class[0] && 0 === strpos($class, "class@anonymous\0") ? (get_parent_class($class) ?: key(class_implements($class))).'@anonymous' : $class;
780780
$title = sprintf(' [%s%s] ', $class, 0 !== ($code = $e->getCode()) ? ' ('.$code.')' : '');
781781
$len = Helper::strlen($title);
782782
} else {
@@ -785,7 +785,7 @@ protected function doRenderThrowable(\Throwable $e, OutputInterface $output): vo
785785

786786
if (false !== strpos($message, "class@anonymous\0")) {
787787
$message = preg_replace_callback('/class@anonymous\x00.*?\.php(?:0x?|:[0-9]++\$)[0-9a-fA-F]++/', function ($m) {
788-
return class_exists($m[0], false) ? get_parent_class($m[0]).'@anonymous' : $m[0];
788+
return class_exists($m[0], false) ? (get_parent_class($m[0]) ?: key(class_implements($m[0]))).'@anonymous' : $m[0];
789789
}, $message);
790790
}
791791

src/Symfony/Component/ErrorHandler/DebugClassLoader.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,7 @@ public function checkAnnotations(\ReflectionClass $refl, string $class): array
406406
}
407407
$deprecations = [];
408408

409-
$className = isset($class[15]) && "\0" === $class[15] && 0 === strpos($class, "class@anonymous\x00") ? get_parent_class($class).'@anonymous' : $class;
409+
$className = isset($class[15]) && "\0" === $class[15] && 0 === strpos($class, "class@anonymous\x00") ? (get_parent_class($class) ?: key(class_implements($class))).'@anonymous' : $class;
410410

411411
// Don't trigger deprecations for classes in the same vendor
412412
if ($class !== $className) {

src/Symfony/Component/ErrorHandler/ErrorHandler.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -762,7 +762,7 @@ private function cleanTrace(array $backtrace, int $type, string $file, int $line
762762
private function parseAnonymousClass(string $message): string
763763
{
764764
return preg_replace_callback('/class@anonymous\x00.*?\.php(?:0x?|:[0-9]++\$)[0-9a-fA-F]++/', static function ($m) {
765-
return class_exists($m[0], false) ? get_parent_class($m[0]).'@anonymous' : $m[0];
765+
return class_exists($m[0], false) ? (get_parent_class($m[0]) ?: key(class_implements($m[0]))).'@anonymous' : $m[0];
766766
}, $message);
767767
}
768768
}

src/Symfony/Component/ErrorHandler/Exception/FlattenException.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ public function getClass(): string
142142
*/
143143
public function setClass($class): self
144144
{
145-
$this->class = 'c' === $class[0] && 0 === strpos($class, "class@anonymous\0") ? get_parent_class($class).'@anonymous' : $class;
145+
$this->class = 'c' === $class[0] && 0 === strpos($class, "class@anonymous\0") ? (get_parent_class($class) ?: key(class_implements($class))).'@anonymous' : $class;
146146

147147
return $this;
148148
}
@@ -201,7 +201,7 @@ public function setMessage($message): self
201201
{
202202
if (false !== strpos($message, "class@anonymous\0")) {
203203
$message = preg_replace_callback('/class@anonymous\x00.*?\.php(?:0x?|:[0-9]++\$)[0-9a-fA-F]++/', function ($m) {
204-
return class_exists($m[0], false) ? get_parent_class($m[0]).'@anonymous' : $m[0];
204+
return class_exists($m[0], false) ? (get_parent_class($m[0]) ?: key(class_implements($m[0]))).'@anonymous' : $m[0];
205205
}, $message);
206206
}
207207

src/Symfony/Component/HttpKernel/Kernel.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ public function getBundle(string $name)
219219
{
220220
if (!isset($this->bundles[$name])) {
221221
$class = \get_class($this);
222-
$class = 'c' === $class[0] && 0 === strpos($class, "class@anonymous\0") ? get_parent_class($class).'@anonymous' : $class;
222+
$class = 'c' === $class[0] && 0 === strpos($class, "class@anonymous\0") ? (get_parent_class($class) ?: key(class_implements($class))).'@anonymous' : $class;
223223

224224
throw new \InvalidArgumentException(sprintf('Bundle "%s" does not exist or it is not enabled. Maybe you forgot to add it in the registerBundles() method of your %s.php file?', $name, $class));
225225
}
@@ -394,7 +394,7 @@ protected function build(ContainerBuilder $container)
394394
protected function getContainerClass()
395395
{
396396
$class = \get_class($this);
397-
$class = 'c' === $class[0] && 0 === strpos($class, "class@anonymous\0") ? get_parent_class($class).str_replace('.', '_', ContainerBuilder::hash($class)) : $class;
397+
$class = 'c' === $class[0] && 0 === strpos($class, "class@anonymous\0") ? (get_parent_class($class) ?: key(class_implements($class))).str_replace('.', '_', ContainerBuilder::hash($class)) : $class;
398398
$class = str_replace('\\', '_', $class).ucfirst($this->environment).($this->debug ? 'Debug' : '').'Container';
399399
if (!preg_match('/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$/', $class)) {
400400
throw new \InvalidArgumentException(sprintf('The environment "%s" contains invalid characters, it can only contain characters allowed in PHP class names.', $this->environment));

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public function next(): MiddlewareInterface
7979
$this->currentEvent = 'Tail';
8080
} else {
8181
$class = \get_class($nextMiddleware);
82-
$this->currentEvent = sprintf('"%s"', 'c' === $class[0] && 0 === strpos($class, "class@anonymous\0") ? get_parent_class($class).'@anonymous' : $class);
82+
$this->currentEvent = sprintf('"%s"', 'c' === $class[0] && 0 === strpos($class, "class@anonymous\0") ? (get_parent_class($class) ?: key(class_implements($class))).'@anonymous' : $class);
8383
}
8484
$this->currentEvent .= sprintf(' on "%s"', $this->busName);
8585

src/Symfony/Component/VarDumper/Caster/Caster.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public static function castObject(object $obj, string $class, bool $hasDebugInfo
7777
$prefixedKeys[$i] = self::PREFIX_DYNAMIC.$k;
7878
}
7979
} elseif (isset($k[16]) && "\0" === $k[16] && 0 === strpos($k, "\0class@anonymous\0")) {
80-
$prefixedKeys[$i] = "\0".get_parent_class($class).'@anonymous'.strrchr($k, "\0");
80+
$prefixedKeys[$i] = "\0".(get_parent_class($class) ?: key(class_implements($class))).'@anonymous'.strrchr($k, "\0");
8181
}
8282
++$i;
8383
}

src/Symfony/Component/VarDumper/Caster/ClassStub.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public function __construct(string $identifier, $callable = null)
5757

5858
if (false !== strpos($identifier, "class@anonymous\0")) {
5959
$this->value = $identifier = preg_replace_callback('/class@anonymous\x00.*?\.php(?:0x?|:[0-9]++\$)[0-9a-fA-F]++/', function ($m) {
60-
return class_exists($m[0], false) ? get_parent_class($m[0]).'@anonymous' : $m[0];
60+
return class_exists($m[0], false) ? (get_parent_class($m[0]) ?: key(class_implements($m[0]))).'@anonymous' : $m[0];
6161
}, $identifier);
6262
}
6363

src/Symfony/Component/VarDumper/Caster/ExceptionCaster.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public static function castThrowingCasterException(ThrowingCasterException $e, a
7474
if (isset($a[$xPrefix.'previous'], $a[$trace]) && $a[$xPrefix.'previous'] instanceof \Exception) {
7575
$b = (array) $a[$xPrefix.'previous'];
7676
$class = \get_class($a[$xPrefix.'previous']);
77-
$class = 'c' === $class[0] && 0 === strpos($class, "class@anonymous\0") ? get_parent_class($class).'@anonymous' : $class;
77+
$class = 'c' === $class[0] && 0 === strpos($class, "class@anonymous\0") ? (get_parent_class($class) ?: key(class_implements($class))).'@anonymous' : $class;
7878
self::traceUnshift($b[$xPrefix.'trace'], $class, $b[$prefix.'file'], $b[$prefix.'line']);
7979
$a[$trace] = new TraceStub($b[$xPrefix.'trace'], false, 0, -\count($a[$trace]->value));
8080
}
@@ -284,7 +284,7 @@ private static function filterExceptionArray(string $xClass, array $a, string $x
284284

285285
if (isset($a[Caster::PREFIX_PROTECTED.'message']) && false !== strpos($a[Caster::PREFIX_PROTECTED.'message'], "class@anonymous\0")) {
286286
$a[Caster::PREFIX_PROTECTED.'message'] = preg_replace_callback('/class@anonymous\x00.*?\.php(?:0x?|:[0-9]++\$)[0-9a-fA-F]++/', function ($m) {
287-
return class_exists($m[0], false) ? get_parent_class($m[0]).'@anonymous' : $m[0];
287+
return class_exists($m[0], false) ? (get_parent_class($m[0]) ?: key(class_implements($m[0]))).'@anonymous' : $m[0];
288288
}, $a[Caster::PREFIX_PROTECTED.'message']);
289289
}
290290

src/Symfony/Component/VarDumper/Cloner/AbstractCloner.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ protected function castObject(Stub $stub, bool $isNested)
286286
$class = $stub->class;
287287

288288
if (isset($class[15]) && "\0" === $class[15] && 0 === strpos($class, "class@anonymous\x00")) {
289-
$stub->class = get_parent_class($class).'@anonymous';
289+
$stub->class = (get_parent_class($class) ?: key(class_implements($class))).'@anonymous';
290290
}
291291
if (isset($this->classInfo[$class])) {
292292
list($i, $parents, $hasDebugInfo, $fileInfo) = $this->classInfo[$class];

src/Symfony/Component/VarDumper/Tests/Caster/CasterTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,11 +164,11 @@ public function testAnonymousClass()
164164
, $c
165165
);
166166

167-
$c = eval('return new class { private $foo = "foo"; };');
167+
$c = eval('return new class implements \Countable { private $foo = "foo"; public function count() { return 0; } };');
168168

169169
$this->assertDumpMatchesFormat(
170170
<<<'EOTXT'
171-
@anonymous {
171+
Countable@anonymous {
172172
-foo: "foo"
173173
}
174174
EOTXT

0 commit comments

Comments
 (0)
0