Dumping variables in Nette\Diagnostics\Debugger (Tracy)

Notice: This thread is very old.
enumag
Member | 2118
+
0
-

The main reason to write this RFC was to fix ambiguous dumps panel in debug bar and add links to location where the dump occurred. While I was implementing it I've also added bunch of other features and fixes.

1) Ambiguous output in dumps panel

Let's say you have this call somewhere in your code:

\Nette\Diagnostics\Debugger::barDump($var);

And you get a result like this one:

Now you probably think that the value of $var was this:

$var = 'foo';

The thing is, it didn't have to. You would have recieved the very same result with this:

$var = array('' => 'foo');

The reason for this behaviour is this line in Debugger::barDump() method. The secondary effect of this line is that an empty array will NOT be shown in the panel at all.

I don't know why are arrays dumped differently than other variables but for the above reasons I consider it wrong. The normal dump is just fine for arrays in my opinion so I've changed it that way.

2) Dumps panel doesn't show location

It's quite useful to see the location of the dump call right away in this panel do I've added it. It is rendered as a link to your editor/IDE as well so you can just click it and delete it when you don't need the dump anymore.

3) Auto collapsing of variables

Dumper automatically collapses all structures (arrays and objects) with more then 7 fields (configurable by Dumper::COLLAPSE_COUNT option). This is however applied recursively so you can get into a situation when the strustrue holds 6 other structures, these hold 6 more structures each and so on and all of that will NOT be collapsed by default. I think it would be better to collapse everything beneath second level by default even if the structure is small. I've added new option Dumper::COLLAPSE_LEVEL for that with default value of 2.

The second thing is that now if the dumped variable itself is a structure, it's not collapsed by default now even if it holds more then 7 fields (even if it holds hundreds of fields). I think it should be collapsed in that case so I've changed that.

I also had to add new option Dumper::LOCATION_LEVEL. Without it you would just always get the possition of your shortcut function. This option is meant to be used by the shortcut function like this:

function bd($var) {
	\Nette\Diagnostics\Debugger::barDump($var, NULL, array(
		\Nette\Diagnostics\Dumper::LOCATION_LEVEL => 3,
	));
}

4) Links to classes

When dumping classes you see protected and private fields as well but you have no way to know how to get them. In such case you often end up finding the class in source code to see its methods. For convenience I've added a link to the editor so you can open the class with one click.

The link is rendered ad a $ before the class name for now (see the screenshot below). This probably should be changed. Keep in mind that putting the link after the class name is pretty much impossible with the current implementation and I don't have time to change that. Done by @milo. THX!

Result

Tests

Unfortunately these changes have broken some tests. I don't know how to fix some of them though because debugging why the Tester\Assert::match() assert failed is real pain for large outputs. I've tried it in past and failed (@dg fixed it himself back then). Tests fixed by @milo. THX!

Implementation

https://github.com/…te/pull/1238
The commits are intentionally separate for now. I will squash them to one later if needed.

Last edited by enumag (2013-11-17 10:59)

Milo
Nette Core | 1283
+
0
-

This is really better for big variables. But few notes, imho…

  • a visibility of links to source file shoud depends on Debugger::$showLocation as in dump().
  • BUG: when you enable Debugger::$showLocation = TRUE, do the dump, and hold cursor over the dumped variable, the title i broken. It contains )) in file .... but right is dump($var) in file ....
  • links to class definition is handy, I like it, but by other way then $ :)
enumag
Member | 2118
+
0
-

a visibility of links to source file shoud depends on Debugger::$showLocation as in dump().

Agreed, fixed.

BUG: when you enable Debugger::$showLocation = TRUE, …

Thanks, fixed.

links to class definition is handy, I like it, but by other way then $ :)

I know. It's kind of pointless to say it without better suggestion though. ;-)

Last edited by enumag (2013-09-15 11:24)

Milo
Nette Core | 1283
+
0
-

enumag wrote:

BUG: when you enable Debugger::$showLocation = TRUE, …

Thanks, fixed.

dump() is OK, but still buggy in barDump() output

links to class definition is handy, I like it, but by other way then $ :)

I know. It's kind of pointless to say it without better suggestion though. ;-)

That's true :-) I tried to change the position and the best seems to me after the class name:

Namespace\Class\Name § #2312

It requires generate something like: <span class="nette-toggle-stop"><a href="">§</a></span> and modify dumper.js to

if (!link || $(link).hasClass('nette-toggle-stop')) {
Milo
Nette Core | 1283
+
0
-

@enumag What do you think about my solution? If you wish, fix the mentioned bug and I'll send you PR with this change and fixed tests.

enumag
Member | 2118
+
0
-

@Milo: Your solution seems good to me, I just don't have time this week. :-(

enumag
Member | 2118
+
0
-

@Milo: The bug basically happens if you use multiline call – this is also buggy:

dump(
	$var
);

Fixing it is not worth the effort in my opinion. Link to source code is far more useful than hidden information in title attribute.

Last edited by enumag (2013-10-02 22:49)

Milo
Nette Core | 1283
+
0
-

@enumag: I see. I'll send you a PR for § behaviour.

enumag
Member | 2118
+
+1
-

Thanks to @milo the implementation is finally complete. The result looks like this:

As you may have noticed I've changed the source link text prom “§” to “(source)”. That's because the paragraph sign seems too small to hit by mouse quickly and also it was not clear what it does until you hover it to see the title. @milo was against this change though and I'm willing to revert it if needed. Tell us your opinion about this.

Last edited by enumag (2013-11-17 11:13)