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 a4dee37
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 15 deletions.
18 changes: 15 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,16 @@ 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
10 changes: 5 additions & 5 deletions tests/PhpGenerator/Dumper.dump().indent.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,26 @@ require __DIR__ . '/../bootstrap.php';

$dumper = new Dumper;

Assert::same('[1, 2, 3]', $dumper->dump([1, 2, 3], $dumper->wrapLength - 10));
Assert::same('[1, 2, 3]', $dumper->dump([1, 2, 3], Dumper::getWrapLength() - 10));

same('[
1,
2,
3,
]', $dumper->dump([1, 2, 3], $dumper->wrapLength - 8));
]', $dumper->dump([1, 2, 3], Dumper::getWrapLength() - 8));


// ignore indent after new line
same('[
[1, 2, 3],
]', $dumper->dump([[1, 2, 3]], $dumper->wrapLength - 8));
]', $dumper->dump([[1, 2, 3]], Dumper::getWrapLength() - 8));


// counts with length of key
Assert::same('[8 => 1, 2, 3]', $dumper->dump([8 => 1, 2, 3], $dumper->wrapLength - 15));
Assert::same('[8 => 1, 2, 3]', $dumper->dump([8 => 1, 2, 3], Dumper::getWrapLength() - 15));

same('[
8 => 1,
2,
3,
]', $dumper->dump([8 => 1, 2, 3], $dumper->wrapLength - 13));
]', $dumper->dump([8 => 1, 2, 3], Dumper::getWrapLength() - 13));
3 changes: 2 additions & 1 deletion tests/PhpGenerator/Dumper.dump().phpt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ require __DIR__ . '/../bootstrap.php';

ini_set('serialize_precision', '14');

Dumper::setWrapLength(100);

$dumper = new Dumper;
Assert::same('0', $dumper->dump(0));
Assert::same('1', $dumper->dump(1));
Expand Down Expand Up @@ -50,7 +52,6 @@ Assert::same("[-2 => 'a', -1 => 'b']", $dumper->dump([-2 => 'a', -1 => 'b']));
Assert::same("[-2 => 'a', 0 => 'b']", $dumper->dump([-2 => 'a', 'b']));
Assert::same("[0 => 'a', -2 => 'b', 1 => 'c']", $dumper->dump(['a', -2 => 'b', 'c']));

$dumper->wrapLength = 100;
same("[
[
'a',
Expand Down
4 changes: 2 additions & 2 deletions tests/PhpGenerator/Dumper.dump().wrap.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ use Nette\PhpGenerator\PhpLiteral;
require __DIR__ . '/../bootstrap.php';


Dumper::setWrapLength(21);
$dumper = new Dumper;
$dumper->wrapLength = 21;
same("[
'a' => [1, 2, 3],
'aaaaaaaaa' => [
Expand Down Expand Up @@ -56,8 +56,8 @@ same("(object) [
);


Dumper::setWrapLength(100);
$dumper = new Dumper;
$dumper->wrapLength = 100;
same("[
[
'a',
Expand Down
2 changes: 1 addition & 1 deletion tests/PhpGenerator/Dumper.format().phpt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use Tester\Assert;
require __DIR__ . '/../bootstrap.php';


Dumper::setWrapLength(100);
$dumper = new Dumper;
Assert::same('func', $dumper->format('func'));
Assert::same('func(1)', $dumper->format('func(?)', 1));
Expand All @@ -21,7 +22,6 @@ Assert::same('func([1, 2])', $dumper->format('func(?)', [1, 2]));
Assert::same('func(1, 2)', $dumper->format('func(...?)', [1, 2]));
Assert::same('func(1, 2)', $dumper->format('func(?*)', [1, 2])); // old way

$dumper->wrapLength = 100;
same(
'func(
10,
Expand Down
2 changes: 1 addition & 1 deletion tests/PhpGenerator/Dumper.format().wrap.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ use Tester\Assert;
require __DIR__ . '/../bootstrap.php';


Dumper::setWrapLength(100);
$dumper = new Dumper;
$dumper->wrapLength = 100;

Assert::same('func([1, 2, 3])', $dumper->format('func(?)', [1, 2, 3]));

Expand Down

0 comments on commit a4dee37

Please sign in to comment.