diff --git a/src/Symfony/Component/VarDumper/Caster/Caster.php b/src/Symfony/Component/VarDumper/Caster/Caster.php index ae8b40fa0eacd..c1f38eb4a81e4 100644 --- a/src/Symfony/Component/VarDumper/Caster/Caster.php +++ b/src/Symfony/Component/VarDumper/Caster/Caster.php @@ -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])) { diff --git a/src/Symfony/Component/VarDumper/Cloner/Stub.php b/src/Symfony/Component/VarDumper/Cloner/Stub.php index 27dd3ef32c4df..19f111f47845e 100644 --- a/src/Symfony/Component/VarDumper/Cloner/Stub.php +++ b/src/Symfony/Component/VarDumper/Cloner/Stub.php @@ -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) { diff --git a/src/Symfony/Component/VarDumper/Tests/Cloner/VarClonerTest.php b/src/Symfony/Component/VarDumper/Tests/Cloner/VarClonerTest.php index 3b180af49810d..f6784be498623 100644 --- a/src/Symfony/Component/VarDumper/Tests/Cloner/VarClonerTest.php +++ b/src/Symfony/Component/VarDumper/Tests/Cloner/VarClonerTest.php @@ -13,6 +13,7 @@ use PHPUnit\Framework\TestCase; use Symfony\Component\VarDumper\Cloner\VarCloner; +use Symfony\Component\VarDumper\Tests\Fixtures\Php74; /** * @author Nicolas Grekas @@ -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)); } diff --git a/src/Symfony/Component/VarDumper/Tests/Fixtures/Php74.php b/src/Symfony/Component/VarDumper/Tests/Fixtures/Php74.php new file mode 100644 index 0000000000000..724fbeb7bdb6e --- /dev/null +++ b/src/Symfony/Component/VarDumper/Tests/Fixtures/Php74.php @@ -0,0 +1,14 @@ +p2 = new \stdClass(); + } +}