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

Conversation

guillaumeVDP
Copy link
Contributor

@guillaumeVDP guillaumeVDP commented Apr 20, 2023

Q A
Branch? 6.3
Bug fix? no
New feature? yes
Deprecations? no
Tickets Fix ##50054
License MIT
Doc PR -

First commit for the feature. Please review this pull request and help me doing this in a good way 🙏

I am not sure if it's good to store uninitialized properties to the attr of the stub. Any better ideas ?

Missing:

  • tests
  • updating CHANGELOG.md

@carsonbot
Copy link

Hey!

Thanks for your PR. You are targeting branch "6.3" but it seems your PR description refers to branch "6.3 for features".
Could you update the PR description or change target branch? This helps core maintainers a lot.

Cheers!

Carsonbot

@guillaumeVDP guillaumeVDP marked this pull request as draft April 20, 2023 15:28
@guillaumeVDP guillaumeVDP marked this pull request as ready for review April 20, 2023 15:36
Copy link
Member

@lyrixx lyrixx left a comment

Choose a reason for hiding this comment

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

wahoo, super good start!

$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?

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

Choose a reason for hiding this comment

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

this is broken for any union or intersection types. Not all ReflectionType are ReflectionNamedType.

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

Choose a reason for hiding this comment

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

why using a list of single-item arrays with an unknown key for this $stub->attr['uninitialized'] ? why not using $stub->attr['uninitialized'][$key] = $type here ?

@@ -367,6 +367,14 @@ private function dumpItem(DumperInterface $dumper, Cursor $cursor, array &$refs,
$cut += \count($children);
}
$cursor->skipChildren = false;
// Add uninitialized properties
foreach ($item->attr["uninitialized"] as $uninitializedProperty) {
Copy link
Member

Choose a reason for hiding this comment

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

shouldn't this be done inside dumpChildren instead ? Otherwise, things will be weird when the cursor is meant to dump without children.

@@ -128,6 +128,15 @@ 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

@guillaumeVDP guillaumeVDP force-pushed the vardumper-show-uninitialized-properties branch from 7d978a0 to a4684c3 Compare April 21, 2023 08:58
@nicolas-grekas nicolas-grekas modified the milestones: 6.3, 6.4 May 23, 2023
@nicolas-grekas
Copy link
Member

nicolas-grekas commented Jul 27, 2023

I went with another approach in #51130
Thanks for pushing this forward!

fabpot added a commit that referenced this pull request Jul 30, 2023
…kas)

This PR was merged into the 6.4 branch.

Discussion
----------

[VarDumper] Dump uninitialized properties

| Q             | A
| ------------- | ---
| Branch?       | 6.4
| Bug fix?      | no
| New feature?  | yes
| Deprecations? | no
| Tickets       | Fix #50054
| License       | MIT
| Doc PR        | -

Replaces #50076

Given this:

```php
class Foo {
    private int|float $foo;
    public $baz;
}

$f = new Foo;
unset($f->baz);

dump($f);
```

This displays:

![image](https://github.com/symfony/symfony/assets/243674/00af5f8a-22ea-45ca-9ece-fec201517096)

Commits
-------

54468db [VarDumper] Dump uninitialized properties
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants