Skip to content

Commit b94c3da

Browse files
[VarDumper] minor optim on PHP 8.1
1 parent 42a7c86 commit b94c3da

File tree

1 file changed

+33
-27
lines changed

1 file changed

+33
-27
lines changed

Cloner/VarCloner.php

Lines changed: 33 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,12 @@ protected function doClone($var)
2727
$len = 1; // Length of $queue
2828
$pos = 0; // Number of cloned items past the minimum depth
2929
$refsCounter = 0; // Hard references counter
30-
$queue = [[$var]]; // This breadth-first queue is the return value
31-
$hardRefs = []; // Map of original zval ids to stub objects
32-
$objRefs = []; // Map of original object handles to their stub object counterpart
33-
$objects = []; // Keep a ref to objects to ensure their handle cannot be reused while cloning
34-
$resRefs = []; // Map of original resource handles to their stub object counterpart
35-
$values = []; // Map of stub objects' ids to original values
30+
$queue = [[$var]]; // This breadth-first queue is the return value
31+
$hardRefs = []; // Map of original zval ids to stub objects
32+
$objRefs = []; // Map of original object handles to their stub object counterpart
33+
$objects = []; // Keep a ref to objects to ensure their handle cannot be reused while cloning
34+
$resRefs = []; // Map of original resource handles to their stub object counterpart
35+
$values = []; // Map of stub objects' ids to original values
3636
$maxItems = $this->maxItems;
3737
$maxString = $this->maxString;
3838
$minDepth = $this->minDepth;
@@ -129,39 +129,45 @@ protected function doClone($var)
129129
continue 2;
130130
}
131131
$stub = $arrayStub;
132+
133+
if (\PHP_VERSION_ID >= 80100) {
134+
$stub->class = array_is_list($v) ? Stub::ARRAY_INDEXED : Stub::ARRAY_ASSOC;
135+
$a = $v;
136+
break;
137+
}
138+
132139
$stub->class = Stub::ARRAY_INDEXED;
133140

134141
$j = -1;
135142
foreach ($v as $gk => $gv) {
136143
if ($gk !== ++$j) {
137144
$stub->class = Stub::ARRAY_ASSOC;
145+
$a = $v;
146+
$a[$gid] = true;
138147
break;
139148
}
140149
}
141-
$a = $v;
142-
143-
if (Stub::ARRAY_ASSOC === $stub->class) {
144-
// Copies of $GLOBALS have very strange behavior,
145-
// let's detect them with some black magic
146-
if (\PHP_VERSION_ID < 80100 && ($a[$gid] = true) && isset($v[$gid])) {
147-
unset($v[$gid]);
148-
$a = [];
149-
foreach ($v as $gk => &$gv) {
150-
if ($v === $gv) {
151-
unset($v);
152-
$v = new Stub();
153-
$v->value = [$v->cut = \count($gv), Stub::TYPE_ARRAY => 0];
154-
$v->handle = -1;
155-
$gv = &$hardRefs[spl_object_id($v)];
156-
$gv = $v;
157-
}
158150

159-
$a[$gk] = &$gv;
151+
// Copies of $GLOBALS have very strange behavior,
152+
// let's detect them with some black magic
153+
if (isset($v[$gid])) {
154+
unset($v[$gid]);
155+
$a = [];
156+
foreach ($v as $gk => &$gv) {
157+
if ($v === $gv) {
158+
unset($v);
159+
$v = new Stub();
160+
$v->value = [$v->cut = \count($gv), Stub::TYPE_ARRAY => 0];
161+
$v->handle = -1;
162+
$gv = &$hardRefs[spl_object_id($v)];
163+
$gv = $v;
160164
}
161-
unset($gv);
162-
} else {
163-
$a = $v;
165+
166+
$a[$gk] = &$gv;
164167
}
168+
unset($gv);
169+
} else {
170+
$a = $v;
165171
}
166172
break;
167173

0 commit comments

Comments
 (0)