Skip to content

Commit

Permalink
Dumper::dumpArray(), dumpArguments() optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Feb 12, 2024
1 parent d961149 commit 64cdb3e
Showing 1 changed file with 17 additions and 23 deletions.
40 changes: 17 additions & 23 deletions src/PhpGenerator/Dumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,26 +110,22 @@ private function dumpArray(array $var, array $parents, int $level, int $column):
throw new Nette\InvalidStateException('Nesting level too deep or recursive dependency.');
}

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

foreach ($var as $k => $v) {
$keyPart = $hideKeys && ($k !== $keys[0] || $k === 0)
? ''
: $this->dumpVar($k) . ' => ';
$outInline .= ($k === $keys[0] ? '' : ', ') . $keyPart;
$outInline .= $this->dumpVar($v, $parents, 0, $column + strlen($outInline));
$outWrapped .= $this->indentation
. $keyPart
. $this->dumpVar($v, $parents, $level + 1, strlen($keyPart))
. ",\n$space";
$pairs[] = $keyPart . $this->dumpVar($v, $parents, $level + 1, strlen($keyPart) + 1); // 1 = comma after item
}

$wrap = str_contains($outInline, "\n") || $level * self::IndentLength + $column + strlen($outInline) + 3 > $this->wrapLength; // 3 = [],
return '[' . ($wrap ? $outWrapped : $outInline) . ']';
$line = '[' . implode(', ', $pairs) . ']';
$space = str_repeat($this->indentation, $level);
return !str_contains($line, "\n") && $level * self::IndentLength + $column + strlen($line) <= $this->wrapLength
? $line
: "[\n$space" . $this->indentation . implode(",\n$space" . $this->indentation, $pairs) . ",\n$space]";
}


Expand Down Expand Up @@ -262,21 +258,19 @@ public function format(string $statement, mixed ...$args): string
}


/** @param mixed[] $var */
private function dumpArguments(array $var, int $column, bool $named): string
/** @param mixed[] $args */
private function dumpArguments(array $args, int $column, bool $named): string
{
$outInline = $outWrapped = '';

foreach ($var as $k => $v) {
$k = !$named || is_int($k) ? '' : $k . ': ';
$outInline .= $outInline === '' ? '' : ', ';
$outInline .= $k . $this->dumpVar($v, [$var], 0, $column + strlen($outInline));
$outWrapped .= "\n" . $this->indentation . $k . $this->dumpVar($v, [$var], 1) . ',';
$pairs = [];
foreach ($args as $k => $v) {
$name = $named && !is_int($k) ? $k . ': ' : '';
$pairs[] = $name . $this->dumpVar($v, [$args], 0, $column + strlen($name) + 1); // 1 = ) after args
}

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


Expand Down

0 comments on commit 64cdb3e

Please sign in to comment.