Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 37ee7db

Browse files
[VarDumper] finish PHP 7.4 support and add tests
1 parent b2027e9 commit 37ee7db

File tree

4 files changed

+93
-5
lines changed

4 files changed

+93
-5
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@ public static function castObject($obj, $class, $hasDebugInfo = false)
6868
foreach ($a as $k => $v) {
6969
if (isset($k[0]) ? "\0" !== $k[0] : \PHP_VERSION_ID >= 70200) {
7070
if (!isset($publicProperties[$class])) {
71-
foreach (get_class_vars($class) as $prop => $v) {
72-
$publicProperties[$class][$prop] = true;
71+
foreach ((new \ReflectionClass($class))->getProperties(\ReflectionProperty::IS_PUBLIC) as $prop) {
72+
$publicProperties[$class][$prop->name] = true;
7373
}
7474
}
7575
if (!isset($publicProperties[$class][$k])) {

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,17 @@ public function __sleep()
4949
$properties = [];
5050

5151
if (!isset(self::$defaultProperties[$c = \get_class($this)])) {
52-
self::$defaultProperties[$c] = get_class_vars($c);
52+
$props = get_class_vars($c);
5353

54-
foreach ((new \ReflectionClass($c))->getStaticProperties() as $k => $v) {
55-
unset(self::$defaultProperties[$c][$k]);
54+
foreach ((new \ReflectionClass($c))->getProperties(\ReflectionProperty::IS_PUBLIC) as $v) {
55+
if ($prop->isStatic()) {
56+
unset($props[$v->name]);
57+
} elseif (!isset($props[$v->name])) {
58+
$props[$v->name] = null;
59+
}
5660
}
61+
62+
self::$defaultProperties[$c] = $props;
5763
}
5864

5965
foreach (self::$defaultProperties[$c] as $k => $v) {

src/Symfony/Component/VarDumper/Tests/Cloner/VarClonerTest.php

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\Component\VarDumper\Cloner\VarCloner;
16+
use Symfony\Component\VarDumper\Tests\Fixtures\Php74;
1617

1718
/**
1819
* @author Nicolas Grekas <[email protected]>
@@ -431,6 +432,73 @@ public function testCaster()
431432
[useRefHandles:Symfony\Component\VarDumper\Cloner\Data:private] => -1
432433
)
433434
435+
EOTXT;
436+
$this->assertStringMatchesFormat($expected, print_r($clone, true));
437+
}
438+
439+
/**
440+
* @requires PHP 7.4
441+
*/
442+
public function testPhp74()
443+
{
444+
$data = new Php74();
445+
446+
$cloner = new VarCloner();
447+
$clone = $cloner->cloneVar($data);
448+
449+
$expected = <<<'EOTXT'
450+
Symfony\Component\VarDumper\Cloner\Data Object
451+
(
452+
[data:Symfony\Component\VarDumper\Cloner\Data:private] => Array
453+
(
454+
[0] => Array
455+
(
456+
[0] => Symfony\Component\VarDumper\Cloner\Stub Object
457+
(
458+
[type] => 4
459+
[class] => Symfony\Component\VarDumper\Tests\Fixtures\Php74
460+
[value] =>
461+
[cut] => 0
462+
[handle] => %i
463+
[refCount] => 0
464+
[position] => 1
465+
[attr] => Array
466+
(
467+
)
468+
469+
)
470+
471+
)
472+
473+
[1] => Array
474+
(
475+
[p1] => 123
476+
[p2] => Symfony\Component\VarDumper\Cloner\Stub Object
477+
(
478+
[type] => 4
479+
[class] => stdClass
480+
[value] =>
481+
[cut] => 0
482+
[handle] => %i
483+
[refCount] => 0
484+
[position] => 0
485+
[attr] => Array
486+
(
487+
)
488+
489+
)
490+
491+
)
492+
493+
)
494+
495+
[position:Symfony\Component\VarDumper\Cloner\Data:private] => 0
496+
[key:Symfony\Component\VarDumper\Cloner\Data:private] => 0
497+
[maxDepth:Symfony\Component\VarDumper\Cloner\Data:private] => 20
498+
[maxItemsPerDepth:Symfony\Component\VarDumper\Cloner\Data:private] => -1
499+
[useRefHandles:Symfony\Component\VarDumper\Cloner\Data:private] => -1
500+
)
501+
434502
EOTXT;
435503
$this->assertStringMatchesFormat($expected, print_r($clone, true));
436504
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace Symfony\Component\VarDumper\Tests\Fixtures;
4+
5+
class Php74
6+
{
7+
public $p1 = 123;
8+
public \stdClass $p2;
9+
10+
public function __construct()
11+
{
12+
$this->p2 = new \stdClass();
13+
}
14+
}

0 commit comments

Comments
 (0)