Skip to content

Commit

Permalink
Dumper: simplified list with negative keys
Browse files Browse the repository at this point in the history
is supported since PHP 8.0 https://3v4l.org/dvYQj
  • Loading branch information
dg committed Feb 11, 2024
1 parent 37cbd9a commit d961149
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 6 deletions.
8 changes: 3 additions & 5 deletions src/PhpGenerator/Dumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,15 +114,13 @@ private function dumpArray(array $var, array $parents, int $level, int $column):
$outInline = '';
$outWrapped = "\n$space";
$parents[] = $var;
$counter = 0;
$hideKeys = is_int(($tmp = array_keys($var))[0]) && $tmp === range($tmp[0], $tmp[0] + count($var) - 1);
$hideKeys = is_int(($keys = array_keys($var))[0]) && $keys === range($keys[0], $keys[0] + count($var) - 1);

foreach ($var as $k => $v) {
$keyPart = $hideKeys && $k === $counter
$keyPart = $hideKeys && ($k !== $keys[0] || $k === 0)
? ''
: $this->dumpVar($k) . ' => ';
$counter = is_int($k) ? max($k + 1, $counter) : $counter;
$outInline .= ($outInline === '' ? '' : ', ') . $keyPart;
$outInline .= ($k === $keys[0] ? '' : ', ') . $keyPart;
$outInline .= $this->dumpVar($v, $parents, 0, $column + strlen($outInline));
$outWrapped .= $this->indentation
. $keyPart
Expand Down
2 changes: 1 addition & 1 deletion tests/PhpGenerator/Dumper.dump().phpt
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ Assert::same('[1, 2, 3]', $dumper->dump([1, 2, 3]));
Assert::same("['a']", $dumper->dump(['a']));
Assert::same("[2 => 'a']", $dumper->dump([2 => 'a']));
Assert::same("[2 => 'a', 'b']", $dumper->dump([2 => 'a', 'b']));
Assert::same("[-2 => 'a', -1 => 'b']", $dumper->dump([-2 => 'a', -1 => 'b']));
Assert::same("[-2 => 'a', 'b']", $dumper->dump([-2 => 'a', -1 => 'b']));
Assert::same("[-2 => 'a', 0 => 'b']", $dumper->dump([-2 => 'a', 0 => 'b']));
Assert::same("[0 => 'a', -2 => 'b', 1 => 'c']", $dumper->dump(['a', -2 => 'b', 1 => 'c']));

Expand Down

0 comments on commit d961149

Please sign in to comment.