Skip to content

Commit d961149

Browse files
committed
Dumper: simplified list with negative keys
is supported since PHP 8.0 https://3v4l.org/dvYQj
1 parent 37cbd9a commit d961149

File tree

2 files changed

+4
-6
lines changed

2 files changed

+4
-6
lines changed

src/PhpGenerator/Dumper.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -114,15 +114,13 @@ private function dumpArray(array $var, array $parents, int $level, int $column):
114114
$outInline = '';
115115
$outWrapped = "\n$space";
116116
$parents[] = $var;
117-
$counter = 0;
118-
$hideKeys = is_int(($tmp = array_keys($var))[0]) && $tmp === range($tmp[0], $tmp[0] + count($var) - 1);
117+
$hideKeys = is_int(($keys = array_keys($var))[0]) && $keys === range($keys[0], $keys[0] + count($var) - 1);
119118

120119
foreach ($var as $k => $v) {
121-
$keyPart = $hideKeys && $k === $counter
120+
$keyPart = $hideKeys && ($k !== $keys[0] || $k === 0)
122121
? ''
123122
: $this->dumpVar($k) . ' => ';
124-
$counter = is_int($k) ? max($k + 1, $counter) : $counter;
125-
$outInline .= ($outInline === '' ? '' : ', ') . $keyPart;
123+
$outInline .= ($k === $keys[0] ? '' : ', ') . $keyPart;
126124
$outInline .= $this->dumpVar($v, $parents, 0, $column + strlen($outInline));
127125
$outWrapped .= $this->indentation
128126
. $keyPart

tests/PhpGenerator/Dumper.dump().phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ Assert::same('[1, 2, 3]', $dumper->dump([1, 2, 3]));
6363
Assert::same("['a']", $dumper->dump(['a']));
6464
Assert::same("[2 => 'a']", $dumper->dump([2 => 'a']));
6565
Assert::same("[2 => 'a', 'b']", $dumper->dump([2 => 'a', 'b']));
66-
Assert::same("[-2 => 'a', -1 => 'b']", $dumper->dump([-2 => 'a', -1 => 'b']));
66+
Assert::same("[-2 => 'a', 'b']", $dumper->dump([-2 => 'a', -1 => 'b']));
6767
Assert::same("[-2 => 'a', 0 => 'b']", $dumper->dump([-2 => 'a', 0 => 'b']));
6868
Assert::same("[0 => 'a', -2 => 'b', 1 => 'c']", $dumper->dump(['a', -2 => 'b', 1 => 'c']));
6969

0 commit comments

Comments
 (0)