Skip to content

[VarDumper] show uninitialized properties #50076

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

Closed
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
11 changes: 9 additions & 2 deletions src/Symfony/Component/VarDumper/Cloner/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ private function dumpItem(DumperInterface $dumper, Cursor $cursor, array &$refs,
$withChildren = false;
$cut = -1;
} else {
$cut = $this->dumpChildren($dumper, $cursor, $refs, $children, $cut, $item->type, null !== $item->class);
$cut = $this->dumpChildren($dumper, $cursor, $refs, $children, $cut, $item->type, $item->attr, null !== $item->class);
}
} elseif ($children && 0 <= $cut) {
$cut += \count($children);
Expand Down Expand Up @@ -392,7 +392,7 @@ private function dumpItem(DumperInterface $dumper, Cursor $cursor, array &$refs,
*
* @return int The final number of removed items
*/
private function dumpChildren(DumperInterface $dumper, Cursor $parentCursor, array &$refs, array $children, int $hashCut, int $hashType, bool $dumpKeys): int
private function dumpChildren(DumperInterface $dumper, Cursor $parentCursor, array &$refs, array $children, int $hashCut, int $hashType, array $hashAttributes, bool $dumpKeys): int
{
$cursor = clone $parentCursor;
++$cursor->depth;
Expand All @@ -411,6 +411,13 @@ private function dumpChildren(DumperInterface $dumper, Cursor $parentCursor, arr
}
}

// Add uninitialized properties
foreach ($hashAttributes["uninitialized"] as $propName => $propType) {
$string = "⚠ uninitialized($propType)";
$cursor->hashKey = $dumpKeys ? $propName : null;
$this->dumpItem($dumper, $cursor, $refs, $string);
}

return $hashCut;
}

Expand Down
20 changes: 20 additions & 0 deletions src/Symfony/Component/VarDumper/Cloner/VarCloner.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,26 @@ protected function doClone(mixed $var): array
$stub->value = $v;
$stub->handle = $h;
$a = $this->castObject($stub, 0 < $i);
if ($stub->value !== '') {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this work for nested objects or should we implement that logic in castObject ?

And I'm wondering whether this should be implemented in Caster::castObject by adding those properties as a special UninitializedStub, so that this is done before any custom caster runs. This way, if a caster decides to cut a property, this would also cut it if it is uninitialized.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with this. I would add this to AbstractCloner::castObject, where there is already $this->classInfo that we can use to store those UninitializedStub.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok I'll try implementing this solution. Thanks

$uninitializedProps = array_diff_key(get_class_vars($stub->value::class), $a);
$stubClass = $stub->value::class;
$stub->attr['uninitialized'] = [];
foreach ($uninitializedProps as $key => $value) {
$rp = new \ReflectionProperty($stubClass, $key);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a local cache for this?

$type = $rp->getType();
if (is_a($type, \ReflectionUnionType::class) || is_a($type, \ReflectionIntersectionType::class)) {
$names = [];
foreach ($type->getTypes() as $t) {
$names[] = $t->getName();
}
$separator = is_a($type, \ReflectionUnionType::class) ? '|' : '&';
$name = implode($separator, $names);
} else {
$name = $type->getName();
}
$stub->attr['uninitialized'][$key] = $name;
}
}
if ($v !== $stub->value) {
if (Stub::TYPE_OBJECT !== $stub->type || null === $stub->value) {
break;
Expand Down