Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[VarDumper] fix PHP 8 support #36920

Merged
merged 1 commit into from May 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 17 additions & 3 deletions src/Symfony/Component/VarDumper/Caster/Caster.php
Expand Up @@ -46,7 +46,7 @@ class Caster
*
* @return array The array-cast of the object, with prefixed dynamic properties
*/
public static function castObject($obj, $class, $hasDebugInfo = false)
public static function castObject($obj, $class, $hasDebugInfo = false, $debugClass = null)
{
if ($class instanceof \ReflectionClass) {
@trigger_error(sprintf('Passing a ReflectionClass to "%s()" is deprecated since Symfony 3.3 and will be unsupported in 4.0. Pass the class name as string instead.', __METHOD__), E_USER_DEPRECATED);
Expand All @@ -71,6 +71,17 @@ public static function castObject($obj, $class, $hasDebugInfo = false)

if ($a) {
static $publicProperties = [];
if (null === $debugClass) {
if (\PHP_VERSION_ID >= 80000) {
nicolas-grekas marked this conversation as resolved.
Show resolved Hide resolved
$debugClass = get_debug_type($obj);
} else {
$debugClass = $class;

if (isset($debugClass[15]) && "\0" === $debugClass[15]) {
$debugClass = (get_parent_class($debugClass) ?: key(class_implements($debugClass)) ?: 'class').'@anonymous';
}
}
}

$i = 0;
$prefixedKeys = [];
Expand All @@ -84,8 +95,8 @@ public static function castObject($obj, $class, $hasDebugInfo = false)
if (!isset($publicProperties[$class][$k])) {
$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");
} elseif ($debugClass !== $class && 1 === strpos($k, $class)) {
$prefixedKeys[$i] = "\0".$debugClass.strrchr($k, "\0");
}
++$i;
}
Expand All @@ -101,6 +112,9 @@ public static function castObject($obj, $class, $hasDebugInfo = false)
if ($hasDebugInfo && \is_array($debugInfo)) {
foreach ($debugInfo as $k => $v) {
if (!isset($k[0]) || "\0" !== $k[0]) {
if (\array_key_exists(self::PREFIX_DYNAMIC.$k, $a)) {
continue;
}
$k = self::PREFIX_VIRTUAL.$k;
}

Expand Down
16 changes: 4 additions & 12 deletions src/Symfony/Component/VarDumper/Caster/SplCaster.php
Expand Up @@ -160,15 +160,6 @@ public static function castFileObject(\SplFileObject $c, array $a, Stub $stub, $
return $a;
}

public static function castFixedArray(\SplFixedArray $c, array $a, Stub $stub, $isNested)
{
$a += [
Caster::PREFIX_VIRTUAL.'storage' => $c->toArray(),
];

return $a;
}

public static function castObjectStorage(\SplObjectStorage $c, array $a, Stub $stub, $isNested)
{
$storage = [];
Expand Down Expand Up @@ -200,22 +191,23 @@ public static function castOuterIterator(\OuterIterator $c, array $a, Stub $stub
private static function castSplArray($c, array $a, Stub $stub, $isNested)
{
$prefix = Caster::PREFIX_VIRTUAL;
$class = $stub->class;
$flags = $c->getFlags();

if (!($flags & \ArrayObject::STD_PROP_LIST)) {
$c->setFlags(\ArrayObject::STD_PROP_LIST);
$a = Caster::castObject($c, $class);
$a = Caster::castObject($c, \get_class($c), method_exists($c, '__debugInfo'), $stub->class);
$c->setFlags($flags);
}
if (\PHP_VERSION_ID < 70400) {
$a[$prefix.'storage'] = $c->getArrayCopy();
}
$a += [
$prefix.'flag::STD_PROP_LIST' => (bool) ($flags & \ArrayObject::STD_PROP_LIST),
$prefix.'flag::ARRAY_AS_PROPS' => (bool) ($flags & \ArrayObject::ARRAY_AS_PROPS),
];
if ($c instanceof \ArrayObject) {
$a[$prefix.'iteratorClass'] = new ClassStub($c->getIteratorClass());
}
$a[$prefix.'storage'] = $c->getArrayCopy();

return $a;
}
Expand Down
7 changes: 3 additions & 4 deletions src/Symfony/Component/VarDumper/Cloner/AbstractCloner.php
Expand Up @@ -102,7 +102,6 @@ abstract class AbstractCloner implements ClonerInterface
'SplDoublyLinkedList' => ['Symfony\Component\VarDumper\Caster\SplCaster', 'castDoublyLinkedList'],
'SplFileInfo' => ['Symfony\Component\VarDumper\Caster\SplCaster', 'castFileInfo'],
'SplFileObject' => ['Symfony\Component\VarDumper\Caster\SplCaster', 'castFileObject'],
'SplFixedArray' => ['Symfony\Component\VarDumper\Caster\SplCaster', 'castFixedArray'],
'SplHeap' => ['Symfony\Component\VarDumper\Caster\SplCaster', 'castHeap'],
'SplObjectStorage' => ['Symfony\Component\VarDumper\Caster\SplCaster', 'castObjectStorage'],
'SplPriorityQueue' => ['Symfony\Component\VarDumper\Caster\SplCaster', 'castHeap'],
Expand Down Expand Up @@ -266,8 +265,8 @@ protected function castObject(Stub $stub, $isNested)
$obj = $stub->value;
$class = $stub->class;

if (isset($class[15]) && "\0" === $class[15] && 0 === strpos($class, "class@anonymous\x00")) {
$stub->class = get_parent_class($class).'@anonymous';
if ((\PHP_VERSION_ID >= 80000 || (isset($class[15]) && "\0" === $class[15])) && false !== strpos($class, "@anonymous\0")) {
$stub->class = \PHP_VERSION_ID < 80000 ? (get_parent_class($class) ?: key(class_implements($class)) ?: 'class').'@anonymous' : get_debug_type($obj);
}
if (isset($this->classInfo[$class])) {
list($i, $parents, $hasDebugInfo) = $this->classInfo[$class];
Expand All @@ -289,7 +288,7 @@ protected function castObject(Stub $stub, $isNested)
$this->classInfo[$class] = [$i, $parents, $hasDebugInfo];
}

$a = Caster::castObject($obj, $class, $hasDebugInfo);
$a = Caster::castObject($obj, $class, $hasDebugInfo, $stub->class);

try {
while ($i--) {
Expand Down
Expand Up @@ -171,7 +171,7 @@ public function testAnonymousClass()

$this->assertDumpMatchesFormat(
<<<'EOTXT'
@anonymous {
class@anonymous {
-foo: "foo"
}
EOTXT
Expand Down
Expand Up @@ -30,6 +30,7 @@ public function testCastPdo()
{
$pdo = new \PDO('sqlite::memory:');
$pdo->setAttribute(\PDO::ATTR_STATEMENT_CLASS, ['PDOStatement', [$pdo]]);
$pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);

$cast = PdoCaster::castPdo($pdo, [], new Stub(), false);

Expand All @@ -45,7 +46,7 @@ public function testCastPdo()
"\x00~\x00inTransaction" => false
"\x00~\x00attributes" => array:9 [
"CASE" => NATURAL
"ERRMODE" => SILENT
"ERRMODE" => EXCEPTION
"PERSISTENT" => false
"DRIVER_NAME" => "sqlite"
"ORACLE_NULLS" => NATURAL
Expand Down
Expand Up @@ -49,8 +49,8 @@ public function testReflectionCaster()
%A]
methods: array:%d [
%A
"export" => ReflectionMethod {
+name: "export"
"__construct" => ReflectionMethod {
+name: "__construct"
+class: "ReflectionClass"
%A parameters: {
$%s: ReflectionParameter {
Expand Down
18 changes: 12 additions & 6 deletions src/Symfony/Component/VarDumper/Tests/Caster/SplCasterTest.php
Expand Up @@ -175,14 +175,17 @@ public function testCastArrayObject()
$expected = <<<EOTXT
ArrayObject {
+"foo": 234
-storage: array:1 [
0 => 123
]
flag::STD_PROP_LIST: false
flag::ARRAY_AS_PROPS: false
iteratorClass: "ArrayIterator"
storage: array:1 [
0 => 123
]
}
EOTXT;
if (\PHP_VERSION_ID < 70400) {
$expected = str_replace('-storage:', 'storage:', $expected);
}
$this->assertDumpEquals($expected, $var);
}

Expand All @@ -196,13 +199,16 @@ public function testArrayIterator()
$expected = <<<EOTXT
Symfony\Component\VarDumper\Tests\Caster\MyArrayIterator {
-foo: 123
flag::STD_PROP_LIST: false
flag::ARRAY_AS_PROPS: false
storage: array:1 [
-storage: array:1 [
0 => 234
]
flag::STD_PROP_LIST: false
flag::ARRAY_AS_PROPS: false
}
EOTXT;
if (\PHP_VERSION_ID < 70400) {
$expected = str_replace('-storage:', 'storage:', $expected);
}
$this->assertDumpEquals($expected, $var);
}

Expand Down
Expand Up @@ -203,6 +203,7 @@ public function provideDumpWithCommaFlagTests()

/**
* @requires extension xml
* @requires PHP < 8.0
*/
public function testXmlResource()
{
Expand Down