Skip to content

Commit

Permalink
Dumper: constants changed to properties $maxDepth, $wrapLength WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Nov 19, 2019
1 parent 9c15cd3 commit 1fa23e4
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 13 deletions.
25 changes: 14 additions & 11 deletions src/PhpGenerator/Dumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,14 @@
*/
final class Dumper
{
public const WRAP_LENGTH = 100;
/** @var int|null */
public $maxDepth = 50;

private const INDENT_LENGTH = 4;
/** @var int */
public $wrapLength = 100;

private const MAX_DEPTH = 50;
/** @var string */
public $indentation = "\t";


/**
Expand Down Expand Up @@ -86,7 +89,7 @@ private function dumpString($var): string

private function dumpArray(&$var, int $level): string
{
$space = str_repeat("\t", $level);
$space = str_repeat($this->indentation, $level);

static $marker;
if ($marker === null) {
Expand All @@ -95,7 +98,7 @@ private function dumpArray(&$var, int $level): string
if (empty($var)) {
$out = '';

} elseif ($level > self::MAX_DEPTH || isset($var[$marker])) {
} elseif ($level > $this->maxDepth || isset($var[$marker])) {
throw new Nette\InvalidArgumentException('Nesting level too deep or recursive dependency.');

} else {
Expand All @@ -108,12 +111,12 @@ private function dumpArray(&$var, int $level): string
$item = ($k === $counter ? '' : $this->dumpVar($k, $level + 1) . ' => ') . $this->dumpVar($v, $level + 1);
$counter = is_int($k) ? max($k + 1, $counter) : $counter;
$out .= ($out === '' ? '' : ', ') . $item;
$outWrapped .= "\t$item,\n$space";
$outWrapped .= $this->indentation . $item . ",\n" . $space;
}
}
unset($var[$marker]);
}
$wrap = strpos($out, "\n") !== false || strlen($out) > self::WRAP_LENGTH - $level * self::INDENT_LENGTH;
$wrap = strpos($out, "\n") !== false || strlen($out) > $this->wrapLength - $level * 4; // TODO
return '[' . ($wrap ? $outWrapped : $out) . ']';
}

Expand All @@ -137,10 +140,10 @@ private function dumpObject(&$var, int $level): string
}

$arr = (array) $var;
$space = str_repeat("\t", $level);
$space = str_repeat($this->indentation, $level);

static $list = [];
if ($level > self::MAX_DEPTH || in_array($var, $list, true)) {
if ($level > $this->maxDepth || in_array($var, $list, true)) {
throw new Nette\InvalidArgumentException('Nesting level too deep or recursive dependency.');

} else {
Expand All @@ -153,7 +156,7 @@ private function dumpObject(&$var, int $level): string
}
foreach ($arr as $k => &$v) {
if (!isset($props) || isset($props[$k])) {
$out .= "$space\t" . $this->dumpVar($k, $level + 1) . ' => ' . $this->dumpVar($v, $level + 1) . ",\n";
$out .= $space . $this->indentation . $this->dumpVar($k, $level + 1) . ' => ' . $this->dumpVar($v, $level + 1) . ",\n";
}
}
array_pop($list);
Expand Down Expand Up @@ -190,7 +193,7 @@ public function format(string $statement, ...$args): string
foreach ($arg as $tmp) {
$items[] = $this->dump($tmp);
}
$res .= strlen($tmp = implode(', ', $items)) > self::WRAP_LENGTH && count($items) > 1
$res .= strlen($tmp = implode(', ', $items)) > $this->wrapLength && count($items) > 1
? "\n" . Nette\Utils\Strings::indent(implode(",\n", $items)) . "\n"
: $tmp;

Expand Down
4 changes: 2 additions & 2 deletions src/PhpGenerator/Printer.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function printClosure(Closure $closure): string
foreach ($closure->getUses() as $param) {
$uses[] = ($param->isReference() ? '&' : '') . '$' . $param->getName();
}
$useStr = strlen($tmp = implode(', ', $uses)) > Dumper::WRAP_LENGTH && count($uses) > 1
$useStr = strlen($tmp = implode(', ', $uses)) > (new Dumper)->wrapLength && count($uses) > 1
? "\n" . $this->indentation . implode(",\n" . $this->indentation, $uses) . "\n"
: $tmp;

Expand Down Expand Up @@ -247,7 +247,7 @@ protected function printParameters($function, ?PhpNamespace $namespace): string
. ($param->hasDefaultValue() && !$variadic ? ' = ' . $this->dump($param->getDefaultValue()) : '');
}

return strlen($tmp = implode(', ', $params)) > Dumper::WRAP_LENGTH && count($params) > 1
return strlen($tmp = implode(', ', $params)) > (new Dumper)->wrapLength && count($params) > 1
? "(\n" . $this->indentation . implode(",\n" . $this->indentation, $params) . "\n)"
: "($tmp)";
}
Expand Down

0 comments on commit 1fa23e4

Please sign in to comment.