Skip to content

Commit d281b08

Browse files
committed
Dumper::dumpArray(), dumpArguments() optimization
1 parent 4e7668e commit d281b08

File tree

1 file changed

+17
-24
lines changed

1 file changed

+17
-24
lines changed

src/PhpGenerator/Dumper.php

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -110,27 +110,22 @@ 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+
$pairs = [];
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+
$pairs[] = $keyPart . $this->dumpVar($v, $parents, $level + 1, strlen($keyPart) + 1); // 1 = comma after item
129122
}
130123

131-
array_pop($parents);
132-
$wrap = str_contains($outInline, "\n") || $level * self::IndentLength + $column + strlen($outInline) + 3 > $this->wrapLength; // 3 = [],
133-
return '[' . ($wrap ? $outWrapped : $outInline) . ']';
124+
$line = '[' . implode(', ', $pairs) . ']';
125+
$space = str_repeat($this->indentation, $level);
126+
return !str_contains($line, "\n") && $level * self::IndentLength + $column + strlen($line) <= $this->wrapLength
127+
? $line
128+
: "[\n$space" . $this->indentation . implode(",\n$space" . $this->indentation, $pairs) . ",\n$space]";
134129
}
135130

136131

@@ -263,21 +258,19 @@ public function format(string $statement, mixed ...$args): string
263258
}
264259

265260

266-
/** @param mixed[] $var */
267-
private function dumpArguments(array &$var, int $column, bool $named): string
261+
/** @param mixed[] $args */
262+
private function dumpArguments(array $args, int $column, bool $named): string
268263
{
269-
$outInline = $outWrapped = '';
270-
271-
foreach ($var as $k => &$v) {
272-
$k = !$named || is_int($k) ? '' : $k . ': ';
273-
$outInline .= $outInline === '' ? '' : ', ';
274-
$outInline .= $k . $this->dumpVar($v, [$var], 0, $column + strlen($outInline));
275-
$outWrapped .= "\n" . $this->indentation . $k . $this->dumpVar($v, [$var], 1) . ',';
264+
$pairs = [];
265+
foreach ($args as $k => $v) {
266+
$name = $named && !is_int($k) ? $k . ': ' : '';
267+
$pairs[] = $name . $this->dumpVar($v, [$args], 0, $column + strlen($name) + 1); // 1 = ) after args
276268
}
277269

278-
return count($var) > 1 && (str_contains($outInline, "\n") || $column + strlen($outInline) > $this->wrapLength)
279-
? $outWrapped . "\n"
280-
: $outInline;
270+
$line = implode(', ', $pairs);
271+
return count($args) < 2 || (!str_contains($line, "\n") && $column + strlen($line) <= $this->wrapLength)
272+
? $line
273+
: "\n" . $this->indentation . implode(",\n" . $this->indentation, $pairs) . ",\n";
281274
}
282275

283276

0 commit comments

Comments
 (0)