Skip to content

[VarDumper] fix dumping typed references from properties #43171

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

Merged
merged 1 commit into from
Sep 24, 2021
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 32 additions & 15 deletions src/Symfony/Component/VarDumper/Cloner/VarCloner.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,29 +82,39 @@ protected function doClone($var)
// $v is the original value or a stub object in case of hard references

if (\PHP_VERSION_ID >= 70400) {
$zvalIsRef = null !== \ReflectionReference::fromArrayElement($vals, $k);
$zvalRef = ($r = \ReflectionReference::fromArrayElement($vals, $k)) ? $r->getId() : null;
} else {
$refs[$k] = $cookie;
$zvalIsRef = $vals[$k] === $cookie;
$zvalRef = $vals[$k] === $cookie;
}

if ($zvalIsRef) {
if ($zvalRef) {
$vals[$k] = &$stub; // Break hard references to make $queue completely
unset($stub); // independent from the original structure
if ($v instanceof Stub && isset($hardRefs[spl_object_id($v)])) {
$vals[$k] = $refs[$k] = $v;
if (\PHP_VERSION_ID >= 70400 ? null !== $vals[$k] = $hardRefs[$zvalRef] ?? null : $v instanceof Stub && isset($hardRefs[spl_object_id($v)])) {
if (\PHP_VERSION_ID >= 70400) {
$v = $vals[$k];
} else {
$refs[$k] = $vals[$k] = $v;
}
if ($v->value instanceof Stub && (Stub::TYPE_OBJECT === $v->value->type || Stub::TYPE_RESOURCE === $v->value->type)) {
++$v->value->refCount;
}
++$v->refCount;
continue;
}
$refs[$k] = $vals[$k] = new Stub();
$refs[$k]->value = $v;
$h = spl_object_id($refs[$k]);
$hardRefs[$h] = &$refs[$k];
$values[$h] = $v;
$vals[$k] = new Stub();
$vals[$k]->value = $v;
$vals[$k]->handle = ++$refsCounter;

if (\PHP_VERSION_ID >= 70400) {
$hardRefs[$zvalRef] = $vals[$k];
} else {
$refs[$k] = $vals[$k];
$h = spl_object_id($refs[$k]);
$hardRefs[$h] = &$refs[$k];
$values[$h] = $v;
}
}
// Create $stub when the original value $v can not be used directly
// If $v is a nested structure, put that structure in array $a
Expand Down Expand Up @@ -163,12 +173,17 @@ protected function doClone($var)
unset($v[$gid]);
$a = [];
foreach ($v as $gk => &$gv) {
if ($v === $gv) {
if ($v === $gv && (\PHP_VERSION_ID < 70400 || !isset($hardRefs[\ReflectionReference::fromArrayElement($v, $gk)->getId()]))) {
unset($v);
$v = new Stub();
$v->value = [$v->cut = \count($gv), Stub::TYPE_ARRAY => 0];
$v->handle = -1;
$gv = &$hardRefs[spl_object_id($v)];
if (\PHP_VERSION_ID >= 70400) {
$gv = &$a[$gk];
$hardRefs[\ReflectionReference::fromArrayElement($a, $gk)->getId()] = &$gv;
} else {
$gv = &$hardRefs[spl_object_id($v)];
}
$gv = $v;
}

Expand Down Expand Up @@ -270,10 +285,12 @@ protected function doClone($var)
}
}

if ($zvalIsRef) {
$refs[$k]->value = $stub;
} else {
if (!$zvalRef) {
$vals[$k] = $stub;
} elseif (\PHP_VERSION_ID >= 70400) {
$hardRefs[$zvalRef]->value = $stub;
} else {
$refs[$k]->value = $stub;
}
}

Expand Down
53 changes: 48 additions & 5 deletions src/Symfony/Component/VarDumper/Tests/Cloner/VarClonerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -499,12 +499,55 @@ public function testPhp74()
[p1] => 123
[p2] => Symfony\Component\VarDumper\Cloner\Stub Object
(
[type] => 4
[class] => stdClass
[value] =>
[type] => 1
[class] =>
[value] => Symfony\Component\VarDumper\Cloner\Stub Object
(
[type] => 4
[class] => stdClass
[value] =>
[cut] => 0
[handle] => %i
[refCount] => 1
[position] => 0
[attr] => Array
(
)

)

[cut] => 0
[handle] => %i
[refCount] => 0
[handle] => 1
[refCount] => 1
[position] => 0
[attr] => Array
(
)

)

[p3] => Symfony\Component\VarDumper\Cloner\Stub Object
(
[type] => 1
[class] =>
[value] => Symfony\Component\VarDumper\Cloner\Stub Object
(
[type] => 4
[class] => stdClass
[value] =>
[cut] => 0
[handle] => %i
[refCount] => 1
[position] => 0
[attr] => Array
(
)

)

[cut] => 0
[handle] => 1
[refCount] => 1
[position] => 0
[attr] => Array
(
Expand Down
2 changes: 2 additions & 0 deletions src/Symfony/Component/VarDumper/Tests/Fixtures/Php74.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ class Php74
{
public $p1 = 123;
public \stdClass $p2;
public \stdClass $p3;

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