Skip to content

Commit

Permalink
[VarDumper] finish PHP 7.4 support and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolas-grekas committed Jul 23, 2019
1 parent 48c3547 commit 8ce8225
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 5 deletions.
4 changes: 2 additions & 2 deletions src/Symfony/Component/VarDumper/Caster/Caster.php
Expand Up @@ -68,8 +68,8 @@ public static function castObject($obj, $class, $hasDebugInfo = false)
foreach ($a as $k => $v) {
if (isset($k[0]) ? "\0" !== $k[0] : \PHP_VERSION_ID >= 70200) {
if (!isset($publicProperties[$class])) {
foreach (get_class_vars($class) as $prop => $v) {
$publicProperties[$class][$prop] = true;
foreach ((new \ReflectionClass($class))->getProperties(\ReflectionProperty::IS_PUBLIC) as $prop) {
$publicProperties[$class][$prop->name] = true;
}
}
if (!isset($publicProperties[$class][$k])) {
Expand Down
12 changes: 9 additions & 3 deletions src/Symfony/Component/VarDumper/Cloner/Stub.php
Expand Up @@ -49,11 +49,17 @@ public function __sleep()
$properties = [];

if (!isset(self::$defaultProperties[$c = \get_class($this)])) {
self::$defaultProperties[$c] = get_class_vars($c);
$defaultProperties = get_class_vars($c);

foreach ((new \ReflectionClass($c))->getStaticProperties() as $k => $v) {
unset(self::$defaultProperties[$c][$k]);
foreach ((new \ReflectionClass($c))->getProperties(\ReflectionProperty::IS_PUBLIC) as $v) {
if ($v->isStatic()) {
unset($defaultProperties[$v->name]);
} elseif (!isset($defaultProperties[$v->name])) {
$defaultProperties[$v->name] = null;
}
}

self::$defaultProperties[$c] = $defaultProperties;
}

foreach (self::$defaultProperties[$c] as $k => $v) {
Expand Down
68 changes: 68 additions & 0 deletions src/Symfony/Component/VarDumper/Tests/Cloner/VarClonerTest.php
Expand Up @@ -13,6 +13,7 @@

use PHPUnit\Framework\TestCase;
use Symfony\Component\VarDumper\Cloner\VarCloner;
use Symfony\Component\VarDumper\Tests\Fixtures\Php74;

/**
* @author Nicolas Grekas <p@tchwork.com>
Expand Down Expand Up @@ -431,6 +432,73 @@ public function testCaster()
[useRefHandles:Symfony\Component\VarDumper\Cloner\Data:private] => -1
)
EOTXT;
$this->assertStringMatchesFormat($expected, print_r($clone, true));
}

/**
* @requires PHP 7.4
*/
public function testPhp74()
{
$data = new Php74();

$cloner = new VarCloner();
$clone = $cloner->cloneVar($data);

$expected = <<<'EOTXT'
Symfony\Component\VarDumper\Cloner\Data Object
(
[data:Symfony\Component\VarDumper\Cloner\Data:private] => Array
(
[0] => Array
(
[0] => Symfony\Component\VarDumper\Cloner\Stub Object
(
[type] => 4
[class] => Symfony\Component\VarDumper\Tests\Fixtures\Php74
[value] =>
[cut] => 0
[handle] => %i
[refCount] => 0
[position] => 1
[attr] => Array
(
)
)
)
[1] => Array
(
[p1] => 123
[p2] => Symfony\Component\VarDumper\Cloner\Stub Object
(
[type] => 4
[class] => stdClass
[value] =>
[cut] => 0
[handle] => %i
[refCount] => 0
[position] => 0
[attr] => Array
(
)
)
)
)
[position:Symfony\Component\VarDumper\Cloner\Data:private] => 0
[key:Symfony\Component\VarDumper\Cloner\Data:private] => 0
[maxDepth:Symfony\Component\VarDumper\Cloner\Data:private] => 20
[maxItemsPerDepth:Symfony\Component\VarDumper\Cloner\Data:private] => -1
[useRefHandles:Symfony\Component\VarDumper\Cloner\Data:private] => -1
)
EOTXT;
$this->assertStringMatchesFormat($expected, print_r($clone, true));
}
Expand Down
14 changes: 14 additions & 0 deletions src/Symfony/Component/VarDumper/Tests/Fixtures/Php74.php
@@ -0,0 +1,14 @@
<?php

namespace Symfony\Component\VarDumper\Tests\Fixtures;

class Php74
{
public $p1 = 123;
public \stdClass $p2;

public function __construct()
{
$this->p2 = new \stdClass();
}
}

0 comments on commit 8ce8225

Please sign in to comment.