Skip to content

Commit

Permalink
Exposer: recognizes virtual properties of native classes
Browse files Browse the repository at this point in the history
'(array) $obj' and 'get_mangled_object_vars($obj)' differ in that the function does not return a virtual properties. So it can be used to recognize them.
  • Loading branch information
dg committed Mar 29, 2023
1 parent 9f3bdbc commit 86eeb97
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 4 deletions.
6 changes: 5 additions & 1 deletion src/Tracy/Dumper/Exposer.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,13 @@ final class Exposer
{
public static function exposeObject(object $obj, Value $value, Describer $describer): void
{
$values = (array) $obj;
$values = get_mangled_object_vars($obj);
$props = self::getProperties($obj::class);

foreach (array_diff_key((array) $obj, $values) as $k => $v) {
$describer->addPropertyTo($value, (string) $k, $v);
}

foreach (array_diff_key($values, $props) as $k => $v) {
$describer->addPropertyTo(
$value,
Expand Down
50 changes: 50 additions & 0 deletions tests/Tracy/Dumper.toHtml().specials.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -145,3 +145,53 @@ Assert::match(
XX,
Dumper::toHtml($obj),
);


// ArrayIterator
$obj = new ArrayIterator(['a', 'b']);
Assert::match(
<<<'XX'
<pre class="tracy-dump tracy-light"
><span class="tracy-toggle"><span class="tracy-dump-object">ArrayIterator</span> <span class="tracy-dump-hash">#%d%</span></span>
<div><span class="tracy-dump-indent"> </span><span class="tracy-dump-virtual">0</span>: <span class="tracy-dump-string"><span>'</span>a<span>'</span></span>
<span class="tracy-dump-indent"> </span><span class="tracy-dump-virtual">1</span>: <span class="tracy-dump-string"><span>'</span>b<span>'</span></span>
</div></pre>
XX,
Dumper::toHtml($obj),
);


// DateTime
$obj = new DateTime('1978-01-23');
Assert::match(
<<<'XX'
<pre class="tracy-dump tracy-light"
><span class="tracy-toggle"><span class="tracy-dump-object">DateTime</span> <span class="tracy-dump-hash">#%d%</span></span>
<div><span class="tracy-dump-indent"> </span><span class="tracy-dump-virtual">date</span>: <span class="tracy-dump-string" title="26 characters"><span>'</span>1978-01-23 00:00:00.000000<span>'</span></span>
<span class="tracy-dump-indent"> </span><span class="tracy-dump-virtual">timezone_type</span>: <span class="tracy-dump-number">3</span>
<span class="tracy-dump-indent"> </span><span class="tracy-dump-virtual">timezone</span>: <span class="tracy-dump-string" title="%d% characters"><span>'</span>%a%<span>'</span></span>
</div></pre>
XX,
Dumper::toHtml($obj),
);


// Tracy\Dumper\Value
$obj = new Tracy\Dumper\Value(Tracy\Dumper\Value::TypeText, 'ahoj');
Assert::match(
<<<'XX'
<pre class="tracy-dump tracy-light"
><span class="tracy-toggle"><span class="tracy-dump-object">Tracy\Dumper\<b>Value</b></span> <span class="tracy-dump-hash">#%d%</span></span>
<div><span class="tracy-dump-indent"> </span><span class="tracy-dump-public">type</span>: <span class="tracy-dump-string" title="4 characters"><span>'</span>text<span>'</span></span>
<span class="tracy-dump-indent"> </span><span class="tracy-dump-public">value</span>: <span class="tracy-dump-string" title="4 characters"><span>'</span>ahoj<span>'</span></span>
<span class="tracy-dump-indent"> </span><span class="tracy-dump-public">length</span>: <span class="tracy-dump-null">null</span>
<span class="tracy-dump-indent"> </span><span class="tracy-dump-public">depth</span>: <span class="tracy-dump-null">null</span>
<span class="tracy-dump-indent"> </span><span class="tracy-dump-public">id</span>: <span class="tracy-dump-null">null</span>
<span class="tracy-dump-indent"> </span><span class="tracy-dump-public">holder</span>: <span class="tracy-dump-virtual">unset</span>
<span class="tracy-dump-indent"> </span><span class="tracy-dump-public">items</span>: <span class="tracy-dump-null">null</span>
<span class="tracy-dump-indent"> </span><span class="tracy-dump-public">editor</span>: <span class="tracy-dump-null">null</span>
<span class="tracy-dump-indent"> </span><span class="tracy-dump-public">collapsed</span>: <span class="tracy-dump-null">null</span>
</div></pre>
XX,
Dumper::toHtml($obj),
);
19 changes: 16 additions & 3 deletions tests/Tracy/Dumper.toText().specials.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -107,12 +107,25 @@ Assert::match(<<<'XX'


// ArrayIterator
$obj = new ArrayIterator(['a' => 1, 'b' => 2]);
$obj = new ArrayIterator(['a', 'b']);
Assert::match(
<<<'XX'
ArrayIterator #%d%
a: 1
b: 2
0: 'a'
1: 'b'
XX,
Dumper::toText($obj),
);


// DateTime
$obj = new DateTime('1978-01-23');
Assert::match(
<<<'XX'
DateTime #%d%
date: '1978-01-23 00:00:00.000000'
timezone_type: %d%
timezone: '%a%'
XX,
Dumper::toText($obj),
);
Expand Down

0 comments on commit 86eeb97

Please sign in to comment.