Skip to content

Commit 26f6a09

Browse files
[VarDumper] collapse/inline cut structures
1 parent a3e475e commit 26f6a09

File tree

2 files changed

+27
-31
lines changed

2 files changed

+27
-31
lines changed

Cloner/Data.php

Lines changed: 26 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -127,16 +127,20 @@ private function dumpItem($dumper, $cursor, &$refs, $item)
127127
break;
128128

129129
case Stub::TYPE_ARRAY:
130-
$dumper->enterHash($cursor, $item->class, $item->value, (bool) $children);
131-
$cut = $this->dumpChildren($dumper, $cursor, $refs, $children, $cut, $item->class);
132-
$dumper->leaveHash($cursor, $item->class, $item->value, (bool) $children, $cut);
133-
break;
134-
130+
$item = clone $item;
131+
$item->type = $item->class;
132+
$item->class = $item->value;
133+
// No break;
135134
case Stub::TYPE_OBJECT:
136135
case Stub::TYPE_RESOURCE:
137-
$dumper->enterHash($cursor, $item->type, $item->class, (bool) $children);
138-
$cut = $this->dumpChildren($dumper, $cursor, $refs, $children, $cut, $item->type);
139-
$dumper->leaveHash($cursor, $item->type, $item->class, (bool) $children, $cut);
136+
$withChildren = $children && $cursor->depth !== $this->maxDepth && $this->maxItemsPerDepth;
137+
$dumper->enterHash($cursor, $item->type, $item->class, $withChildren);
138+
if ($withChildren) {
139+
$cut = $this->dumpChildren($dumper, $cursor, $refs, $children, $cut, $item->type);
140+
} elseif ($children && 0 <= $cut) {
141+
$cut += count($children);
142+
}
143+
$dumper->leaveHash($cursor, $item->type, $item->class, $withChildren, $cut);
140144
break;
141145

142146
default:
@@ -166,26 +170,20 @@ private function dumpItem($dumper, $cursor, &$refs, $item)
166170
*/
167171
private function dumpChildren($dumper, $parentCursor, &$refs, $children, $hashCut, $hashType)
168172
{
169-
if ($children) {
170-
if ($parentCursor->depth !== $this->maxDepth && $this->maxItemsPerDepth) {
171-
$cursor = clone $parentCursor;
172-
++$cursor->depth;
173-
$cursor->hashType = $hashType;
174-
$cursor->hashIndex = 0;
175-
$cursor->hashLength = count($children);
176-
$cursor->hashCut = $hashCut;
177-
foreach ($children as $key => $child) {
178-
$cursor->hashKeyIsBinary = isset($key[0]) && !preg_match('//u', $key);
179-
$cursor->hashKey = $key;
180-
$this->dumpItem($dumper, $cursor, $refs, $child);
181-
if (++$cursor->hashIndex === $this->maxItemsPerDepth || $cursor->stop) {
182-
$parentCursor->stop = true;
183-
184-
return $hashCut >= 0 ? $hashCut + $cursor->hashLength - $cursor->hashIndex : $hashCut;
185-
}
186-
}
187-
} elseif ($hashCut >= 0) {
188-
$hashCut += count($children);
173+
$cursor = clone $parentCursor;
174+
++$cursor->depth;
175+
$cursor->hashType = $hashType;
176+
$cursor->hashIndex = 0;
177+
$cursor->hashLength = count($children);
178+
$cursor->hashCut = $hashCut;
179+
foreach ($children as $key => $child) {
180+
$cursor->hashKeyIsBinary = isset($key[0]) && !preg_match('//u', $key);
181+
$cursor->hashKey = $key;
182+
$this->dumpItem($dumper, $cursor, $refs, $child);
183+
if (++$cursor->hashIndex === $this->maxItemsPerDepth || $cursor->stop) {
184+
$parentCursor->stop = true;
185+
186+
return $hashCut >= 0 ? $hashCut + $cursor->hashLength - $cursor->hashIndex : $hashCut;
189187
}
190188
}
191189

Tests/CliDumperTest.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -224,9 +224,7 @@ public function testBuggyRefs()
224224
array:1 [
225225
0 => array:1 [
226226
0 => array:1 [
227-
0 => array:1 [
228-
…1
229-
]
227+
0 => array:1 [ …1]
230228
]
231229
]
232230
]

0 commit comments

Comments
 (0)