Skip to content

Commit 7ec8dc6

Browse files
[VarDumper] fix PHP 8 support
1 parent d12b3b6 commit 7ec8dc6

File tree

8 files changed

+42
-29
lines changed

8 files changed

+42
-29
lines changed

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

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ class Caster
4646
*
4747
* @return array The array-cast of the object, with prefixed dynamic properties
4848
*/
49-
public static function castObject($obj, $class, $hasDebugInfo = false)
49+
public static function castObject($obj, $class, $hasDebugInfo = false, $debugClass = null)
5050
{
5151
if ($class instanceof \ReflectionClass) {
5252
@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);
@@ -71,6 +71,17 @@ public static function castObject($obj, $class, $hasDebugInfo = false)
7171

7272
if ($a) {
7373
static $publicProperties = [];
74+
if (null === $debugClass) {
75+
if (\PHP_VERSION_ID >= 80000) {
76+
$debugClass = get_debug_type($obj);
77+
} else {
78+
$debugClass = $class;
79+
80+
if (isset($debugClass[15]) && "\0" === $debugClass[15]) {
81+
$debugClass = (get_parent_class($debugClass) ?: key(class_implements($debugClass)) ?: 'class').'@anonymous';
82+
}
83+
}
84+
}
7485

7586
$i = 0;
7687
$prefixedKeys = [];
@@ -84,8 +95,8 @@ public static function castObject($obj, $class, $hasDebugInfo = false)
8495
if (!isset($publicProperties[$class][$k])) {
8596
$prefixedKeys[$i] = self::PREFIX_DYNAMIC.$k;
8697
}
87-
} elseif (isset($k[16]) && "\0" === $k[16] && 0 === strpos($k, "\0class@anonymous\0")) {
88-
$prefixedKeys[$i] = "\0".get_parent_class($class).'@anonymous'.strrchr($k, "\0");
98+
} elseif ($debugClass !== $class && 1 === strpos($k, $class)) {
99+
$prefixedKeys[$i] = "\0".$debugClass.strrchr($k, "\0");
89100
}
90101
++$i;
91102
}
@@ -101,6 +112,9 @@ public static function castObject($obj, $class, $hasDebugInfo = false)
101112
if ($hasDebugInfo && \is_array($debugInfo)) {
102113
foreach ($debugInfo as $k => $v) {
103114
if (!isset($k[0]) || "\0" !== $k[0]) {
115+
if (\array_key_exists(self::PREFIX_DYNAMIC.$k, $a)) {
116+
continue;
117+
}
104118
$k = self::PREFIX_VIRTUAL.$k;
105119
}
106120

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

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -160,15 +160,6 @@ public static function castFileObject(\SplFileObject $c, array $a, Stub $stub, $
160160
return $a;
161161
}
162162

163-
public static function castFixedArray(\SplFixedArray $c, array $a, Stub $stub, $isNested)
164-
{
165-
$a += [
166-
Caster::PREFIX_VIRTUAL.'storage' => $c->toArray(),
167-
];
168-
169-
return $a;
170-
}
171-
172163
public static function castObjectStorage(\SplObjectStorage $c, array $a, Stub $stub, $isNested)
173164
{
174165
$storage = [];
@@ -200,22 +191,23 @@ public static function castOuterIterator(\OuterIterator $c, array $a, Stub $stub
200191
private static function castSplArray($c, array $a, Stub $stub, $isNested)
201192
{
202193
$prefix = Caster::PREFIX_VIRTUAL;
203-
$class = $stub->class;
204194
$flags = $c->getFlags();
205195

206196
if (!($flags & \ArrayObject::STD_PROP_LIST)) {
207197
$c->setFlags(\ArrayObject::STD_PROP_LIST);
208-
$a = Caster::castObject($c, $class);
198+
$a = Caster::castObject($c, \get_class($c), method_exists($c, '__debugInfo'), $stub->class);
209199
$c->setFlags($flags);
210200
}
201+
if (\PHP_VERSION_ID < 80000) {
202+
$a[$prefix.'storage'] = $c->getArrayCopy();
203+
}
211204
$a += [
212205
$prefix.'flag::STD_PROP_LIST' => (bool) ($flags & \ArrayObject::STD_PROP_LIST),
213206
$prefix.'flag::ARRAY_AS_PROPS' => (bool) ($flags & \ArrayObject::ARRAY_AS_PROPS),
214207
];
215208
if ($c instanceof \ArrayObject) {
216209
$a[$prefix.'iteratorClass'] = new ClassStub($c->getIteratorClass());
217210
}
218-
$a[$prefix.'storage'] = $c->getArrayCopy();
219211

220212
return $a;
221213
}

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,6 @@ abstract class AbstractCloner implements ClonerInterface
102102
'SplDoublyLinkedList' => ['Symfony\Component\VarDumper\Caster\SplCaster', 'castDoublyLinkedList'],
103103
'SplFileInfo' => ['Symfony\Component\VarDumper\Caster\SplCaster', 'castFileInfo'],
104104
'SplFileObject' => ['Symfony\Component\VarDumper\Caster\SplCaster', 'castFileObject'],
105-
'SplFixedArray' => ['Symfony\Component\VarDumper\Caster\SplCaster', 'castFixedArray'],
106105
'SplHeap' => ['Symfony\Component\VarDumper\Caster\SplCaster', 'castHeap'],
107106
'SplObjectStorage' => ['Symfony\Component\VarDumper\Caster\SplCaster', 'castObjectStorage'],
108107
'SplPriorityQueue' => ['Symfony\Component\VarDumper\Caster\SplCaster', 'castHeap'],
@@ -266,8 +265,8 @@ protected function castObject(Stub $stub, $isNested)
266265
$obj = $stub->value;
267266
$class = $stub->class;
268267

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

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

294293
try {
295294
while ($i--) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ public function testAnonymousClass()
171171

172172
$this->assertDumpMatchesFormat(
173173
<<<'EOTXT'
174-
@anonymous {
174+
class@anonymous {
175175
-foo: "foo"
176176
}
177177
EOTXT

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ public function testCastPdo()
3030
{
3131
$pdo = new \PDO('sqlite::memory:');
3232
$pdo->setAttribute(\PDO::ATTR_STATEMENT_CLASS, ['PDOStatement', [$pdo]]);
33+
$pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
3334

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

@@ -45,7 +46,7 @@ public function testCastPdo()
4546
"\x00~\x00inTransaction" => false
4647
"\x00~\x00attributes" => array:9 [
4748
"CASE" => NATURAL
48-
"ERRMODE" => SILENT
49+
"ERRMODE" => EXCEPTION
4950
"PERSISTENT" => false
5051
"DRIVER_NAME" => "sqlite"
5152
"ORACLE_NULLS" => NATURAL

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ public function testReflectionCaster()
4949
%A]
5050
methods: array:%d [
5151
%A
52-
"export" => ReflectionMethod {
53-
+name: "export"
52+
"__construct" => ReflectionMethod {
53+
+name: "__construct"
5454
+class: "ReflectionClass"
5555
%A parameters: {
5656
$%s: ReflectionParameter {

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

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -175,14 +175,17 @@ public function testCastArrayObject()
175175
$expected = <<<EOTXT
176176
ArrayObject {
177177
+"foo": 234
178+
-storage: array:1 [
179+
0 => 123
180+
]
178181
flag::STD_PROP_LIST: false
179182
flag::ARRAY_AS_PROPS: false
180183
iteratorClass: "ArrayIterator"
181-
storage: array:1 [
182-
0 => 123
183-
]
184184
}
185185
EOTXT;
186+
if (\PHP_VERSION_ID < 80000) {
187+
$expected = str_replace('-storage:', 'storage:', $expected);
188+
}
186189
$this->assertDumpEquals($expected, $var);
187190
}
188191

@@ -196,13 +199,16 @@ public function testArrayIterator()
196199
$expected = <<<EOTXT
197200
Symfony\Component\VarDumper\Tests\Caster\MyArrayIterator {
198201
-foo: 123
199-
flag::STD_PROP_LIST: false
200-
flag::ARRAY_AS_PROPS: false
201-
storage: array:1 [
202+
-storage: array:1 [
202203
0 => 234
203204
]
205+
flag::STD_PROP_LIST: false
206+
flag::ARRAY_AS_PROPS: false
204207
}
205208
EOTXT;
209+
if (\PHP_VERSION_ID < 80000) {
210+
$expected = str_replace('-storage:', 'storage:', $expected);
211+
}
206212
$this->assertDumpEquals($expected, $var);
207213
}
208214

src/Symfony/Component/VarDumper/Tests/Dumper/CliDumperTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,7 @@ public function provideDumpWithCommaFlagTests()
203203

204204
/**
205205
* @requires extension xml
206+
* @requires PHP < 8.0
206207
*/
207208
public function testXmlResource()
208209
{

0 commit comments

Comments
 (0)