Skip to content

Commit e3ac591

Browse files
committed
Dumper::dumpArray(), dumpArguments() optimization
1 parent d961149 commit e3ac591

1 file changed

Lines changed: 38 additions & 20 deletions

File tree

src/PhpGenerator/Dumper.php

Lines changed: 38 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -110,26 +110,37 @@ private function dumpArray(array $var, array $parents, int $level, int $column):
110110
throw new Nette\InvalidStateException('Nesting level too deep or recursive dependency.');
111111
}
112112

113-
$space = str_repeat($this->indentation, $level);
114-
$outInline = '';
115-
$outWrapped = "\n$space";
116113
$parents[] = $var;
117114
$hideKeys = is_int(($keys = array_keys($var))[0]) && $keys === range($keys[0], $keys[0] + count($var) - 1);
115+
$res = '';
118116

119117
foreach ($var as $k => $v) {
120118
$keyPart = $hideKeys && ($k !== $keys[0] || $k === 0)
121119
? ''
122120
: $this->dumpVar($k) . ' => ';
123-
$outInline .= ($k === $keys[0] ? '' : ', ') . $keyPart;
124-
$outInline .= $this->dumpVar($v, $parents, 0, $column + strlen($outInline));
125-
$outWrapped .= $this->indentation
126-
. $keyPart
127-
. $this->dumpVar($v, $parents, $level + 1, strlen($keyPart))
128-
. ",\n$space";
121+
$res .= ($k === $keys[0] ? '' : ', ') . $keyPart;
122+
$res .= $this->dumpVar($v, $parents, $level + 1, $column + strlen($res));
123+
124+
if (
125+
str_contains($res, "\n")
126+
|| $level * self::IndentLength + $column + strlen($res) + 3 > $this->wrapLength // 3 = [],
127+
) { // wrap
128+
$space = str_repeat($this->indentation, $level);
129+
$res = "\n$space";
130+
foreach ($var as $k => $v) {
131+
$keyPart = $hideKeys && ($k !== $keys[0] || $k === 0)
132+
? ''
133+
: $this->dumpVar($k) . ' => ';
134+
$res .= $this->indentation
135+
. $keyPart
136+
. $this->dumpVar($v, $parents, $level + 1, strlen($keyPart))
137+
. ",\n$space";
138+
}
139+
break;
140+
}
129141
}
130142

131-
$wrap = str_contains($outInline, "\n") || $level * self::IndentLength + $column + strlen($outInline) + 3 > $this->wrapLength; // 3 = [],
132-
return '[' . ($wrap ? $outWrapped : $outInline) . ']';
143+
return '[' . $res . ']';
133144
}
134145

135146

@@ -265,18 +276,25 @@ public function format(string $statement, mixed ...$args): string
265276
/** @param mixed[] $var */
266277
private function dumpArguments(array $var, int $column, bool $named): string
267278
{
268-
$outInline = $outWrapped = '';
269-
279+
$res = '';
270280
foreach ($var as $k => $v) {
271-
$k = !$named || is_int($k) ? '' : $k . ': ';
272-
$outInline .= $outInline === '' ? '' : ', ';
273-
$outInline .= $k . $this->dumpVar($v, [$var], 0, $column + strlen($outInline));
274-
$outWrapped .= "\n" . $this->indentation . $k . $this->dumpVar($v, [$var], 1) . ',';
281+
$namePart = $named && !is_int($k) ? $k . ': ' : '';
282+
$res .= ($res === '' ? '' : ', ') . $namePart;
283+
$res .= $this->dumpVar($v, [$var], 0, $column + strlen($res));
284+
285+
if (count($var) > 1 && (str_contains($res, "\n") || $column + strlen($res) > $this->wrapLength)) { // wrap
286+
$res = '';
287+
foreach ($var as $k => $v) {
288+
$namePart = $named && !is_int($k) ? $k . ': ' : '';
289+
$res .= "\n" . $this->indentation
290+
. $namePart
291+
. $this->dumpVar($v, [$var], 1, strlen($namePart)) . ',';
292+
}
293+
return $res . "\n";
294+
}
275295
}
276296

277-
return count($var) > 1 && (str_contains($outInline, "\n") || $column + strlen($outInline) > $this->wrapLength)
278-
? $outWrapped . "\n"
279-
: $outInline;
297+
return $res;
280298
}
281299

282300

0 commit comments

Comments
 (0)