Skip to content

Commit

Permalink
count items recursively
Browse files Browse the repository at this point in the history
  • Loading branch information
staabm committed Jun 17, 2024
1 parent 0a94d0c commit 36324e5
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 32 deletions.
65 changes: 34 additions & 31 deletions src/Exporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,41 +63,12 @@ public function export(mixed $value, int $indentation = 0): string

public function shortenedRecursiveExport(array &$data, ?RecursionContext $processed = null): string
{
$result = [];

if (!$processed) {
$processed = new RecursionContext;
}
$overallCount = count($data, COUNT_RECURSIVE);

$array = $data;

/* @noinspection UnusedFunctionResultInspection */
$processed->add($data);

$i = 0;
$count = count($data, COUNT_RECURSIVE);

foreach ($array as $key => $value) {
if ($count > $this->shortenArraysLongerThan && $i > $this->shortenArraysLongerThan) {
$result[] = sprintf('...%d more elements', $count - $this->shortenArraysLongerThan);

break;
}

if (is_array($value)) {
if ($processed->contains($data[$key]) !== false) {
$result[] = '*RECURSION*';
} else {
$result[] = '[' . $this->shortenedRecursiveExport($data[$key], $processed) . ']';
}
} else {
$result[] = $this->shortenedExport($value);
}

$i++;
}

return implode(', ', $result);
return $this->_shortenedRecursiveExport($data, $processed, $overallCount);
}

/**
Expand Down Expand Up @@ -211,6 +182,38 @@ public function toArray(mixed $value): array
return $array;
}

private function _shortenedRecursiveExport(array &$data, RecursionContext $processed, int $overallCount, int &$counter = 0): string
{
$result = [];

$array = $data;

/* @noinspection UnusedFunctionResultInspection */
$processed->add($data);

foreach ($array as $key => $value) {
if ($overallCount > $this->shortenArraysLongerThan && $counter > $this->shortenArraysLongerThan) {
$result[] = sprintf('...%d more elements', $overallCount - $this->shortenArraysLongerThan);

break;
}

if (is_array($value)) {
if ($processed->contains($data[$key]) !== false) {
$result[] = '*RECURSION*';
} else {
$result[] = '[' . $this->_shortenedRecursiveExport($data[$key], $processed, $overallCount, $counter) . ']';
}
} else {
$result[] = $this->shortenedExport($value);
}

$counter++;
}

return implode(', ', $result);
}

private function recursiveExport(mixed &$value, int $indentation = 0, ?RecursionContext $processed = null): string
{
if ($value === null) {
Expand Down
6 changes: 5 additions & 1 deletion tests/ExporterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,9 @@ public static function shortenedRecursiveExportProvider(): array
$bigArray[] = 'cast(\'foo' . $i . '\' as blob)';
}

$array = [1, 2, 'hello', 'world', true, false];
$deepArray = [$array, [$array, [$array, [$array, [$array, [$array, [$array, [$array, [$array, [$array]]]]]]]]]];

return [
'null' => [[null], 'null'],
'boolean true' => [[true], 'true'],
Expand All @@ -339,7 +342,8 @@ public static function shortenedRecursiveExportProvider(): array
'with assoc array key' => [['foo' => 'bar'], '\'bar\''],
'multidimensional array' => [[[1, 2, 3], [3, 4, 5]], '[1, 2, 3], [3, 4, 5]'],
'object' => [[new stdClass], 'stdClass Object ()'],
'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"],
'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"],
'deep array' => [$deepArray, "[1, 2, 'hello', 'world', true, false], [[1, 2, 'hello', 'world', ...69 more elements], ...69 more elements]"],
];
}

Expand Down

0 comments on commit 36324e5

Please sign in to comment.