Skip to content

Commit

Permalink
minor #35441 Improve displaying anonymous classes (nicolas-grekas)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
nicolas-grekas committed Jan 27, 2020
2 parents 524ee7a + 487bcc6 commit af295b5
Show file tree
Hide file tree
Showing 11 changed files with 16 additions and 16 deletions.
4 changes: 2 additions & 2 deletions src/Symfony/Component/Console/Application.php
Expand Up @@ -776,7 +776,7 @@ protected function doRenderThrowable(\Throwable $e, OutputInterface $output): vo
$message = trim($e->getMessage());
if ('' === $message || OutputInterface::VERBOSITY_VERBOSE <= $output->getVerbosity()) {
$class = \get_class($e);
$class = 'c' === $class[0] && 0 === strpos($class, "class@anonymous\0") ? get_parent_class($class).'@anonymous' : $class;
$class = 'c' === $class[0] && 0 === strpos($class, "class@anonymous\0") ? (get_parent_class($class) ?: key(class_implements($class))).'@anonymous' : $class;
$title = sprintf(' [%s%s] ', $class, 0 !== ($code = $e->getCode()) ? ' ('.$code.')' : '');
$len = Helper::strlen($title);
} else {
Expand All @@ -785,7 +785,7 @@ protected function doRenderThrowable(\Throwable $e, OutputInterface $output): vo

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

Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/ErrorHandler/DebugClassLoader.php
Expand Up @@ -406,7 +406,7 @@ public function checkAnnotations(\ReflectionClass $refl, string $class): array
}
$deprecations = [];

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

// Don't trigger deprecations for classes in the same vendor
if ($class !== $className) {
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/ErrorHandler/ErrorHandler.php
Expand Up @@ -762,7 +762,7 @@ private function cleanTrace(array $backtrace, int $type, string $file, int $line
private function parseAnonymousClass(string $message): string
{
return preg_replace_callback('/class@anonymous\x00.*?\.php(?:0x?|:[0-9]++\$)[0-9a-fA-F]++/', static function ($m) {
return class_exists($m[0], false) ? get_parent_class($m[0]).'@anonymous' : $m[0];
return class_exists($m[0], false) ? (get_parent_class($m[0]) ?: key(class_implements($m[0]))).'@anonymous' : $m[0];
}, $message);
}
}
Expand Up @@ -142,7 +142,7 @@ public function getClass(): string
*/
public function setClass($class): self
{
$this->class = 'c' === $class[0] && 0 === strpos($class, "class@anonymous\0") ? get_parent_class($class).'@anonymous' : $class;
$this->class = 'c' === $class[0] && 0 === strpos($class, "class@anonymous\0") ? (get_parent_class($class) ?: key(class_implements($class))).'@anonymous' : $class;

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

Expand Down
4 changes: 2 additions & 2 deletions src/Symfony/Component/HttpKernel/Kernel.php
Expand Up @@ -219,7 +219,7 @@ public function getBundle(string $name)
{
if (!isset($this->bundles[$name])) {
$class = \get_class($this);
$class = 'c' === $class[0] && 0 === strpos($class, "class@anonymous\0") ? get_parent_class($class).'@anonymous' : $class;
$class = 'c' === $class[0] && 0 === strpos($class, "class@anonymous\0") ? (get_parent_class($class) ?: key(class_implements($class))).'@anonymous' : $class;

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));
}
Expand Down Expand Up @@ -394,7 +394,7 @@ protected function build(ContainerBuilder $container)
protected function getContainerClass()
{
$class = \get_class($this);
$class = 'c' === $class[0] && 0 === strpos($class, "class@anonymous\0") ? get_parent_class($class).str_replace('.', '_', ContainerBuilder::hash($class)) : $class;
$class = 'c' === $class[0] && 0 === strpos($class, "class@anonymous\0") ? (get_parent_class($class) ?: key(class_implements($class))).str_replace('.', '_', ContainerBuilder::hash($class)) : $class;
$class = str_replace('\\', '_', $class).ucfirst($this->environment).($this->debug ? 'Debug' : '').'Container';
if (!preg_match('/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$/', $class)) {
throw new \InvalidArgumentException(sprintf('The environment "%s" contains invalid characters, it can only contain characters allowed in PHP class names.', $this->environment));
Expand Down
Expand Up @@ -79,7 +79,7 @@ public function next(): MiddlewareInterface
$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"', 'c' === $class[0] && 0 === strpos($class, "class@anonymous\0") ? (get_parent_class($class) ?: key(class_implements($class))).'@anonymous' : $class);
}
$this->currentEvent .= sprintf(' on "%s"', $this->busName);

Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/VarDumper/Caster/Caster.php
Expand Up @@ -77,7 +77,7 @@ public static function castObject(object $obj, string $class, bool $hasDebugInfo
$prefixedKeys[$i] = self::PREFIX_DYNAMIC.$k;
}
} elseif (isset($k[16]) && "\0" === $k[16] && 0 === strpos($k, "\0class@anonymous\0")) {
$prefixedKeys[$i] = "\0".get_parent_class($class).'@anonymous'.strrchr($k, "\0");
$prefixedKeys[$i] = "\0".(get_parent_class($class) ?: key(class_implements($class))).'@anonymous'.strrchr($k, "\0");
}
++$i;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/VarDumper/Caster/ClassStub.php
Expand Up @@ -57,7 +57,7 @@ public function __construct(string $identifier, $callable = null)

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

Expand Down
4 changes: 2 additions & 2 deletions src/Symfony/Component/VarDumper/Caster/ExceptionCaster.php
Expand Up @@ -74,7 +74,7 @@ public static function castThrowingCasterException(ThrowingCasterException $e, a
if (isset($a[$xPrefix.'previous'], $a[$trace]) && $a[$xPrefix.'previous'] instanceof \Exception) {
$b = (array) $a[$xPrefix.'previous'];
$class = \get_class($a[$xPrefix.'previous']);
$class = 'c' === $class[0] && 0 === strpos($class, "class@anonymous\0") ? get_parent_class($class).'@anonymous' : $class;
$class = 'c' === $class[0] && 0 === strpos($class, "class@anonymous\0") ? (get_parent_class($class) ?: key(class_implements($class))).'@anonymous' : $class;
self::traceUnshift($b[$xPrefix.'trace'], $class, $b[$prefix.'file'], $b[$prefix.'line']);
$a[$trace] = new TraceStub($b[$xPrefix.'trace'], false, 0, -\count($a[$trace]->value));
}
Expand Down Expand Up @@ -284,7 +284,7 @@ private static function filterExceptionArray(string $xClass, array $a, string $x

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

Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/VarDumper/Cloner/AbstractCloner.php
Expand Up @@ -286,7 +286,7 @@ protected function castObject(Stub $stub, bool $isNested)
$class = $stub->class;

if (isset($class[15]) && "\0" === $class[15] && 0 === strpos($class, "class@anonymous\x00")) {
$stub->class = get_parent_class($class).'@anonymous';
$stub->class = (get_parent_class($class) ?: key(class_implements($class))).'@anonymous';
}
if (isset($this->classInfo[$class])) {
list($i, $parents, $hasDebugInfo, $fileInfo) = $this->classInfo[$class];
Expand Down
4 changes: 2 additions & 2 deletions src/Symfony/Component/VarDumper/Tests/Caster/CasterTest.php
Expand Up @@ -164,11 +164,11 @@ public function testAnonymousClass()
, $c
);

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

$this->assertDumpMatchesFormat(
<<<'EOTXT'
@anonymous {
Countable@anonymous {
-foo: "foo"
}
EOTXT
Expand Down

0 comments on commit af295b5

Please sign in to comment.