Skip to content

Commit

Permalink
Allow to limit the max-depth of Exporter->export()
Browse files Browse the repository at this point in the history
  • Loading branch information
clxmstaab committed Mar 28, 2024
1 parent e58279a commit 464d680
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions src/Exporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@

final class Exporter
{
private ?int $maxLevel;

Check failure on line 39 in src/Exporter.php

View workflow job for this annotation

GitHub Actions / Type Checker

MissingConstructor

src/Exporter.php:39:18: MissingConstructor: SebastianBergmann\Exporter\Exporter has an uninitialized property SebastianBergmann\Exporter\Exporter::$maxLevel, but no constructor (see https://psalm.dev/073)

/**
* Exports a value as a string.
*
Expand All @@ -49,9 +51,11 @@ final class Exporter
* - Carriage returns and newlines are normalized to \n
* - Recursion and repeated rendering is treated properly
*/
public function export(mixed $value, int $indentation = 0): string
public function export(mixed $value, int $indentation = 0, ?int $maxLevel = null): string
{
return $this->recursiveExport($value, $indentation);
$this->maxLevel = $maxLevel;

return $this->recursiveExport($value, $indentation, null, 0);
}

public function shortenedRecursiveExport(array &$data, ?Context $context = null): string
Expand Down Expand Up @@ -192,8 +196,14 @@ public function toArray(mixed $value): array
return $array;
}

private function recursiveExport(mixed &$value, int $indentation, ?Context $processed = null): string
private function recursiveExport(mixed &$value, int $indentation, ?Context $processed = null, $level = 0): string

Check failure on line 199 in src/Exporter.php

View workflow job for this annotation

GitHub Actions / Type Checker

MissingParamType

src/Exporter.php:199:99: MissingParamType: Parameter $level has no provided type (see https://psalm.dev/154)
{
if ($this->maxLevel !== null) {
if ($level > $this->maxLevel) {
return '** Max recursion level reached **';

Check warning on line 203 in src/Exporter.php

View check run for this annotation

Codecov / codecov/patch

src/Exporter.php#L202-L203

Added lines #L202 - L203 were not covered by tests
}
}

if ($value === null) {
return 'null';
}
Expand Down Expand Up @@ -296,7 +306,7 @@ private function recursiveExport(mixed &$value, int $indentation, ?Context $proc
. ' ' .
$this->recursiveExport($k, $indentation)
. ' => ' .
$this->recursiveExport($value[$k], $indentation + 1, $processed)
$this->recursiveExport($value[$k], $indentation + 1, $processed, $level + 1)
. ",\n";
}

Expand Down Expand Up @@ -324,7 +334,7 @@ private function recursiveExport(mixed &$value, int $indentation, ?Context $proc
. ' ' .
$this->recursiveExport($k, $indentation)
. ' => ' .
$this->recursiveExport($v, $indentation + 1, $processed)
$this->recursiveExport($v, $indentation + 1, $processed, $level + 1)
. ",\n";
}

Expand Down

0 comments on commit 464d680

Please sign in to comment.