Skip to content
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

Dumped object do not include uninitialized properties #448

Closed
janbarasek opened this issue Nov 9, 2020 · 6 comments
Closed

Dumped object do not include uninitialized properties #448

janbarasek opened this issue Nov 9, 2020 · 6 comments

Comments

@janbarasek
Copy link
Contributor

Version: 2.7.5

Bug Description

In case of PHP 7.4 entity with required scalar properties Tracy do not show required properties without value.

For example imagine simple object OrderInfoAddress:

<?php

declare(strict_types=1);

namespace Baraja\Cart;


final class OrderInfoAddress
{
	private string $street;

	private string $city;

	private string $zip;

	private string $companyName = '';

	private string $ic = '';

	private bool $buyAsCompany = false;

	private bool $invoiceAddressIsDifferent = false;
}

In case of simple dump like:

bdump(new OrderInfoAddress)

Tracy show initialized properties with value only:

Snímek obrazovky 2020-11-09 v 19 30 31

Steps To Reproduce

Use my test object.

Expected Behavior & Possible Solution

Tracy should show all properties, but instead, value shows information property is uninitialized.

This settings can be detected by \ReflectionProperty by simple logic: $property->isInitialized($instance).

Current behavior is confusing. In the case of inheritance of multiple classes, it is not possible to unambiguously determine whether the property was inherited or just has no value.

Thanks!

@dg
Copy link
Member

dg commented Nov 12, 2020

Look, I'm not sure if it should work as you write. Should this dump show property $foo?

class X 
{
  public $foo;
}

$x = new X;
unset($x->foo);
dump($x);

@janbarasek
Copy link
Contributor Author

@dg I don't think the property $foo should be displayed in this case.

However, it should be displayed when it is physically defined in a class so that the developer knows that its value may exist in some cases.

What do you think?

@mabar
Copy link
Contributor

mabar commented Nov 12, 2020

I prepared example with all variants - var_dump displays typed properties every time, not-typed properties only if they were not unset() and the dynamic properties.
Tracy could ideally show all properties and show the differences like uninitialized / unset / dynamic

http://sandbox.onlinephpfunctions.com/code/64b434282c03f8ce7a12987ebd6fc2e4b6a4bd83

@dg
Copy link
Member

dg commented Nov 13, 2020

@janbarasek but now you are contradictory. $foo is physically defined in a class

@janbarasek
Copy link
Contributor Author

janbarasek commented Nov 13, 2020

@dg Ok, back to square one.

Simple code:

class Entity
{
	public string $uninitialized;
	public $foo;
}

var_dump(new Entity);

Show:

object(Entity)#1 (1) {
   ["uninitialized"]=> uninitialized(string)
   ["foo"]=> NULL
}

It means that the native var_dump() function displays all properties, including uninitialized ones.

Your X class should be dumped as empty.

I think the dump() function should behave the same.

@mabar
Copy link
Contributor

mabar commented Nov 13, 2020

So I made a working implementation which includes class-only defined properties. Needs changes, because uninitialized / unset modifiers are rendered as values, but good enough for demonstration.

Here is the commit: mabar@88f3f45

Testing code:

class Test
{

	public int $typed = 123;
	public $untyped = 123;
	public int $typedNoValue;
	public $untypedNoValue;
	protected $prot = 'prot';
	private $priv = 'priv';

	public function doUnset(): void
	{
		unset($this->typed);
		unset($this->untyped);
		unset($this->typedNoValue);
		unset($this->untypedNoValue);
		unset($this->prot);
		unset($this->priv);
	}

}

$t = new Test();
bdump($t);

$t->doUnset();
$t->dynamicallySet = 'foo';

bdump($t);

This is before:
before

And this is after:
after

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants