Skip to content

Commit deb644f

Browse files
committed
count items recursively
1 parent 0a94d0c commit deb644f

File tree

2 files changed

+41
-29
lines changed

2 files changed

+41
-29
lines changed

src/Exporter.php

Lines changed: 36 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -63,41 +63,19 @@ public function export(mixed $value, int $indentation = 0): string
6363

6464
public function shortenedRecursiveExport(array &$data, ?RecursionContext $processed = null): string
6565
{
66-
$result = [];
67-
6866
if (!$processed) {
6967
$processed = new RecursionContext;
7068
}
69+
$overallCount = count($data, COUNT_RECURSIVE);
70+
$counter = 0;
7171

72-
$array = $data;
73-
74-
/* @noinspection UnusedFunctionResultInspection */
75-
$processed->add($data);
76-
77-
$i = 0;
78-
$count = count($data, COUNT_RECURSIVE);
79-
80-
foreach ($array as $key => $value) {
81-
if ($count > $this->shortenArraysLongerThan && $i > $this->shortenArraysLongerThan) {
82-
$result[] = sprintf('...%d more elements', $count - $this->shortenArraysLongerThan);
83-
84-
break;
85-
}
86-
87-
if (is_array($value)) {
88-
if ($processed->contains($data[$key]) !== false) {
89-
$result[] = '*RECURSION*';
90-
} else {
91-
$result[] = '[' . $this->shortenedRecursiveExport($data[$key], $processed) . ']';
92-
}
93-
} else {
94-
$result[] = $this->shortenedExport($value);
95-
}
72+
$export = $this->_shortenedRecursiveExport($data, $processed, $overallCount, $counter);
9673

97-
$i++;
74+
if ($overallCount > $this->shortenArraysLongerThan) {
75+
$export .= sprintf(' ...%d more elements', $overallCount - $this->shortenArraysLongerThan);
9876
}
9977

100-
return implode(', ', $result);
78+
return $export;
10179
}
10280

10381
/**
@@ -211,6 +189,36 @@ public function toArray(mixed $value): array
211189
return $array;
212190
}
213191

192+
private function _shortenedRecursiveExport(array &$data, RecursionContext $processed, int $overallCount, int &$counter): string
193+
{
194+
$result = [];
195+
196+
$array = $data;
197+
198+
/* @noinspection UnusedFunctionResultInspection */
199+
$processed->add($data);
200+
201+
foreach ($array as $key => $value) {
202+
if ($overallCount > $this->shortenArraysLongerThan && $counter > $this->shortenArraysLongerThan) {
203+
break;
204+
}
205+
206+
if (is_array($value)) {
207+
if ($processed->contains($data[$key]) !== false) {
208+
$result[] = '*RECURSION*';
209+
} else {
210+
$result[] = '[' . $this->_shortenedRecursiveExport($data[$key], $processed, $overallCount, $counter) . ']';
211+
}
212+
} else {
213+
$result[] = $this->shortenedExport($value);
214+
}
215+
216+
$counter++;
217+
}
218+
219+
return implode(', ', $result);
220+
}
221+
214222
private function recursiveExport(mixed &$value, int $indentation = 0, ?RecursionContext $processed = null): string
215223
{
216224
if ($value === null) {

tests/ExporterTest.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,9 @@ public static function shortenedRecursiveExportProvider(): array
327327
$bigArray[] = 'cast(\'foo' . $i . '\' as blob)';
328328
}
329329

330+
$array = [1, 2, 'hello', 'world', true, false];
331+
$deepArray = [$array, [$array, [$array, [$array, [$array, [$array, [$array, [$array, [$array, [$array]]]]]]]]]];
332+
330333
return [
331334
'null' => [[null], 'null'],
332335
'boolean true' => [[true], 'true'],
@@ -339,7 +342,8 @@ public static function shortenedRecursiveExportProvider(): array
339342
'with assoc array key' => [['foo' => 'bar'], '\'bar\''],
340343
'multidimensional array' => [[[1, 2, 3], [3, 4, 5]], '[1, 2, 3], [3, 4, 5]'],
341344
'object' => [[new stdClass], 'stdClass Object ()'],
342-
'big array' => [$bigArray, "'cast('foo0' as blob)', 'cast('foo1' as blob)', 'cast('foo2' as blob)', 'cast('foo3' as blob)', 'cast('foo4' as blob)', 'cast('foo5' as blob)', 'cast('foo6' as blob)', 'cast('foo7' as blob)', 'cast('foo8' as blob)', 'cast('foo9' as blob)', 'cast('foo10' as blob)', ...19992 more elements"],
345+
'big array' => [$bigArray, "'cast('foo0' as blob)', 'cast('foo1' as blob)', 'cast('foo2' as blob)', 'cast('foo3' as blob)', 'cast('foo4' as blob)', 'cast('foo5' as blob)', 'cast('foo6' as blob)', 'cast('foo7' as blob)', 'cast('foo8' as blob)', 'cast('foo9' as blob)', 'cast('foo10' as blob)' ...19990 more elements"],
346+
'deep array' => [$deepArray, "[1, 2, 'hello', 'world', true, false], [[1, 2, 'hello', 'world']] ...69 more elements"],
343347
];
344348
}
345349

0 commit comments

Comments
 (0)