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
-------
487bcc6200
Improve displaying anonymous classes
This commit is contained in:
commit
af295b5b8d
@ -776,7 +776,7 @@ class Application implements ResetInterface
|
||||
$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 {
|
||||
@ -785,7 +785,7 @@ class Application implements ResetInterface
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
|
@ -406,7 +406,7 @@ class DebugClassLoader
|
||||
}
|
||||
$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) {
|
||||
|
@ -762,7 +762,7 @@ class ErrorHandler
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
@ -142,7 +142,7 @@ class FlattenException
|
||||
*/
|
||||
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;
|
||||
}
|
||||
@ -201,7 +201,7 @@ class FlattenException
|
||||
{
|
||||
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);
|
||||
}
|
||||
|
||||
|
@ -219,7 +219,7 @@ abstract class Kernel implements KernelInterface, RebootableInterface, Terminabl
|
||||
{
|
||||
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));
|
||||
}
|
||||
@ -394,7 +394,7 @@ abstract class Kernel implements KernelInterface, RebootableInterface, Terminabl
|
||||
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));
|
||||
|
@ -79,7 +79,7 @@ class TraceableStack implements StackInterface
|
||||
$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);
|
||||
|
||||
|
@ -77,7 +77,7 @@ class Caster
|
||||
$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;
|
||||
}
|
||||
|
@ -57,7 +57,7 @@ class ClassStub extends ConstStub
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
|
@ -74,7 +74,7 @@ class ExceptionCaster
|
||||
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));
|
||||
}
|
||||
@ -284,7 +284,7 @@ class ExceptionCaster
|
||||
|
||||
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']);
|
||||
}
|
||||
|
||||
|
@ -286,7 +286,7 @@ abstract class AbstractCloner implements ClonerInterface
|
||||
$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];
|
||||
|
@ -164,11 +164,11 @@ EOTXT
|
||||
, $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
|
||||
|
Reference in New Issue
Block a user