Skip to content

Commit 8845419

Browse files
committed
Dumper: describeArray() refactoring
1 parent ff0e806 commit 8845419

File tree

2 files changed

+14
-28
lines changed

2 files changed

+14
-28
lines changed

src/Tracy/Dumper/Describer.php

Lines changed: 13 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -129,45 +129,30 @@ private function describeString(string $s, int $depth = 0): StringNode|string
129129
private function describeArray(array $arr, int $depth = 0, ?int $refId = null): ArrayNode|ReferenceNode
130130
{
131131
if ($refId) {
132-
$res = new ReferenceNode('p' . $refId);
133-
$node = &$this->snapshot[$res->targetId];
132+
$ref = new ReferenceNode('p' . $refId);
133+
$node = &$this->snapshot[$ref->targetId];
134134
if ($node && $node->depth <= $depth) {
135-
return $res;
135+
return $ref;
136136
}
137137

138138
$node = new ArrayNode;
139-
$node->id = $res->targetId;
139+
$node->id = $ref->targetId;
140140
$node->depth = $depth;
141-
if ($this->maxDepth && $depth >= $this->maxDepth) {
142-
$node->length = count($arr);
143-
return $res;
144-
} elseif ($depth && $this->maxItems && count($arr) > $this->maxItems) {
145-
$node->length = count($arr);
146-
$arr = array_slice($arr, 0, $this->maxItems, preserve_keys: true);
147-
}
148-
149-
$items = &$node->items;
150-
151-
} elseif ($arr && $this->maxDepth && $depth >= $this->maxDepth) {
152-
$res = new ArrayNode;
153-
$res->length = count($arr);
154-
return $res;
141+
} else {
142+
$node = new ArrayNode;
143+
}
155144

145+
$node->length = count($arr);
146+
if ($arr && $this->maxDepth && $depth >= $this->maxDepth) {
147+
return $ref ?? $node;
156148
} elseif ($depth && $this->maxItems && count($arr) > $this->maxItems) {
157-
$res = new ArrayNode;
158-
$res->length = count($arr);
159-
$res->depth = $depth;
160-
$items = &$res->items;
161149
$arr = array_slice($arr, 0, $this->maxItems, preserve_keys: true);
162-
} else {
163-
$res = new ArrayNode;
164-
$items = &$res->items;
165150
}
166151

167-
$items = [];
152+
$node->items = [];
168153
foreach ($arr as $k => $v) {
169154
$refId = $this->getReferenceId($arr, $k);
170-
$items[] = new CollectionItem(
155+
$node->items[] = new CollectionItem(
171156
$this->describeVar($k, $depth + 1),
172157
$this->isSensitive((string) $k, $v)
173158
? new TextNode(self::hideValue($v))
@@ -176,7 +161,7 @@ private function describeArray(array $arr, int $depth = 0, ?int $refId = null):
176161
);
177162
}
178163

179-
return $res;
164+
return $ref ?? $node;
180165
}
181166

182167

tests/Dumper/Dumper.exposeObject.phpt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ $value = new ObjectNode;
5555
Exposer::exposeObject(new Test, $value, new Describer);
5656
$arr = new ArrayNode;
5757
$arr->items = [new CollectionItem(0, 10), new CollectionItem(1, null)];
58+
$arr->length = 2;
5859
Assert::equal([
5960
new CollectionItem('x', $arr, type: ObjectNode::PropertyPublic),
6061
new CollectionItem('y', 'hello', type: 'Test'),

0 commit comments

Comments
 (0)