Skip to content

Commit

Permalink
Add possibility to configure wrapping length of Dumper-class
Browse files Browse the repository at this point in the history
Closes nette#55
  • Loading branch information
duxthefux committed Feb 10, 2020
1 parent 8fe7e69 commit 0b88edd
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
16 changes: 13 additions & 3 deletions src/PhpGenerator/Dumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ final class Dumper
public $maxDepth = 50;

/** @var int */
public $wrapLength = 120;
protected static $wrapLength = 120;


/**
Expand Down Expand Up @@ -111,7 +111,7 @@ private function dumpArray(array &$var, array $parents, int $level, int $column)
}

array_pop($parents);
$wrap = strpos($outInline, "\n") !== false || $level * self::INDENT_LENGTH + $column + strlen($outInline) + 3 > $this->wrapLength; // 3 = [],
$wrap = strpos($outInline, "\n") !== false || $level * self::INDENT_LENGTH + $column + strlen($outInline) + 3 > self::getWrapLength(); // 3 = [],
return '[' . ($wrap ? $outWrapped : $outInline) . ']';
}

Expand Down Expand Up @@ -213,7 +213,7 @@ private function dumpArguments(array &$var, int $column): string
$outWrapped .= ($outWrapped === '' ? '' : ',') . "\n\t" . $this->dumpVar($v, [$var], 1);
}

return count($var) > 1 && (strpos($outInline, "\n") !== false || $column + strlen($outInline) > $this->wrapLength)
return count($var) > 1 && (strpos($outInline, "\n") !== false || $column + strlen($outInline) > self::getWrapLength())
? $outWrapped . "\n"
: $outInline;
}
Expand All @@ -227,4 +227,14 @@ public static function createObject(string $class, array $props)
{
return unserialize('O' . substr(serialize($class), 1, -1) . substr(serialize($props), 1));
}

public static function getWrapLength(): int
{
return self::$wrapLength;
}

public static function setWrapLength(int $wrapLength): void
{
self::$wrapLength = $wrapLength;
}
}
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)) > (new Dumper)->wrapLength && count($uses) > 1
$useStr = strlen($tmp = implode(', ', $uses)) > Dumper::getWrapLength() && count($uses) > 1
? "\n" . $this->indentation . implode(",\n" . $this->indentation, $uses) . "\n"
: $tmp;

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

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

0 comments on commit 0b88edd

Please sign in to comment.