Skip to content

Commit

Permalink
Printer, Dumper: array wrapping counts with property/constant name le…
Browse files Browse the repository at this point in the history
…ngth [Closes #45] (#47)
  • Loading branch information
zeleznypa authored and dg committed Nov 20, 2019
1 parent 4d46580 commit 22228fd
Show file tree
Hide file tree
Showing 8 changed files with 159 additions and 46 deletions.
27 changes: 17 additions & 10 deletions src/PhpGenerator/Dumper.php
Expand Up @@ -32,13 +32,13 @@ final class Dumper
/**
* Returns a PHP representation of a variable.
*/
public function dump($var): string
public function dump($var, int $indent = 0): string
{
return $this->dumpVar($var);
return $this->dumpVar($var, [], 0, $indent);
}


private function dumpVar(&$var, array $parents = [], int $level = 0): string
private function dumpVar(&$var, array $parents = [], int $level = 0, int $indent = 0): string
{
if ($var instanceof PhpLiteral) {
return (string) $var;
Expand All @@ -50,7 +50,7 @@ private function dumpVar(&$var, array $parents = [], int $level = 0): string
return $this->dumpString($var);

} elseif (is_array($var)) {
return $this->dumpArray($var, $parents, $level);
return $this->dumpArray($var, $parents, $level, $indent);

} elseif (is_object($var)) {
return $this->dumpObject($var, $parents, $level);
Expand Down Expand Up @@ -86,7 +86,7 @@ private function dumpString(string $var): string
}


private function dumpArray(array &$var, array $parents, int $level): string
private function dumpArray(array &$var, array $parents, int $level, int $indent): string
{
if (empty($var)) {
return '[]';
Expand All @@ -102,14 +102,18 @@ private function dumpArray(array &$var, array $parents, int $level): string
$counter = 0;

foreach ($var as $k => &$v) {
$item = ($k === $counter ? '' : $this->dumpVar($k, $parents, $level + 1) . ' => ') . $this->dumpVar($v, $parents, $level + 1);
$keyPart = $k === $counter ? '' : $this->dumpVar($k, $parents) . ' => ';
$counter = is_int($k) ? max($k + 1, $counter) : $counter;
$outInline .= ($outInline === '' ? '' : ', ') . $item;
$outWrapped .= $this->indentation . $item . ",\n" . $space;
$outInline .= ($outInline === '' ? '' : ', ') . $keyPart;
$outInline .= $this->dumpVar($v, $parents, 0, $indent + strlen($outInline));
$outWrapped .= $this->indentation
. $keyPart
. $this->dumpVar($v, $parents, $level + 1, strlen($keyPart))
. ",\n$space";
}

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

Expand Down Expand Up @@ -148,7 +152,10 @@ private function dumpObject(&$var, array $parents, int $level): string

foreach ($arr as $k => &$v) {
if (!isset($props) || isset($props[$k])) {
$out .= $space . $this->indentation . $this->dumpVar($k, $parents, $level + 1) . ' => ' . $this->dumpVar($v, $parents, $level + 1) . ",\n";
$out .= $space . $this->indentation
. ($keyPart = $this->dumpVar($k, $parents) . ' => ')
. $this->dumpVar($v, $parents, $level + 1, strlen($keyPart))
. ",\n";
}
}

Expand Down
19 changes: 11 additions & 8 deletions src/PhpGenerator/Printer.php
Expand Up @@ -112,19 +112,22 @@ public function printClass(ClassType $class, PhpNamespace $namespace = null): st

$consts = [];
foreach ($class->getConstants() as $const) {
$def = ($const->getVisibility() ? $const->getVisibility() . ' ' : '') . 'const ' . $const->getName() . ' = ';
$consts[] = Helpers::formatDocComment((string) $const->getComment())
. ($const->getVisibility() ? $const->getVisibility() . ' ' : '')
. 'const ' . $const->getName() . ' = ' . $this->dump($const->getValue()) . ";\n";
. $def
. $this->dump($const->getValue(), strlen($def)) . ";\n";
}

$properties = [];
foreach ($class->getProperties() as $property) {
$type = $property->getType();
$properties[] = Helpers::formatDocComment((string) $property->getComment())
. ($property->getVisibility() ?: 'public') . ($property->isStatic() ? ' static' : '') . ' '
$def = (($property->getVisibility() ?: 'public') . ($property->isStatic() ? ' static' : '') . ' '
. ($type ? ($property->isNullable() ? '?' : '') . ($this->resolveTypes && $namespace ? $namespace->unresolveName($type) : $type) . ' ' : '')
. '$' . $property->getName()
. ($property->getValue() === null && !$property->isInitialized() ? '' : ' = ' . $this->dump($property->getValue()))
. '$' . $property->getName());

$properties[] = Helpers::formatDocComment((string) $property->getComment())
. $def
. ($property->getValue() === null && !$property->isInitialized() ? '' : ' = ' . $this->dump($property->getValue(), strlen($def) + 3)) // 3 = ' = '
. ";\n";
}

Expand Down Expand Up @@ -214,11 +217,11 @@ protected function indent(string $s): string
}


protected function dump($var): string
protected function dump($var, int $firstIndent = 0): string
{
$dumper = new Dumper;
$dumper->indentation = $this->indentation;
return $dumper->dump($var);
return $dumper->dump($var, $firstIndent);
}


Expand Down
40 changes: 40 additions & 0 deletions tests/PhpGenerator/Dumper.dump().indent.phpt
@@ -0,0 +1,40 @@
<?php

/**
* Test: Nette\PhpGenerator\Dumper::dump()
*/

declare(strict_types=1);

use Nette\PhpGenerator\Dumper;
use Tester\Assert;


require __DIR__ . '/../bootstrap.php';


$dumper = new Dumper;

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

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


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


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

same('[
8 => 1,
2,
3,
]', $dumper->dump([8 => 1, 2, 3], $dumper->wrapLength - 13));
53 changes: 53 additions & 0 deletions tests/PhpGenerator/Dumper.dump().wrap.phpt
@@ -0,0 +1,53 @@
<?php

/**
* Test: Nette\PhpGenerator\Dumper::dump()
*/

declare(strict_types=1);

use Nette\PhpGenerator\Dumper;


require __DIR__ . '/../bootstrap.php';


$dumper = new Dumper;
$dumper->wrapLength = 21;
same("[
'a' => [1, 2, 3],
'aaaaaaaaa' => [
1,
2,
3,
],
]",
$dumper->dump([
'a' => [1, 2, 3],
'aaaaaaaaa' => [1, 2, 3],
])
);

same("(object) [
'a' => [1, 2, 3],
'aaaaaaaaa' => [
1,
2,
3,
],
]",
$dumper->dump((object) [
'a' => [1, 2, 3],
'aaaaaaaaa' => [1, 2, 3],
])
);


$dumper = new Dumper;
$dumper->wrapLength = 100;
same("[
[
'a',
'looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong',
],
]", $dumper->dump([['a', 'looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong']]));
6 changes: 4 additions & 2 deletions tests/PhpGenerator/Printer.phpt
Expand Up @@ -26,7 +26,8 @@ $class->addConstant('FORCE_ARRAY', new PhpLiteral('Nette\Utils\Json::FORCE_ARRAY
->setVisibility('private')
->addComment('Commented');

$class->addConstant('MULTILINE', ['aaaaaaaaaaaa' => 1, 'bbbbbbbbbbb' => 2, 'cccccccccccccc' => 3, 'dddddddddddd' => 4, 'eeeeeeeeeeee' => 5]);
$class->addConstant('MULTILINE_LONG', ['aaaaaaa' => 1, 'bbbbbbb' => 2, 'ccccccc' => 3, 'ddddddd' => 4, 'eeeeee' => 5]);
$class->addConstant('SHORT', ['aaaaaaa' => 1, 'bbbbbbb' => 2, 'ccccccc' => 3, 'ddddddd' => 4, 'eeeeee' => 5]);

$class->addProperty('handle')
->setVisibility('private')
Expand All @@ -35,7 +36,8 @@ $class->addProperty('handle')
$class->addProperty('order')
->setValue(new PhpLiteral('RecursiveIteratorIterator::SELF_FIRST'));

$class->addProperty('multiline', ['aaaaaaaaaaaa' => 1, 'bbbbbbbbbbb' => 2, 'cccccccccccccc' => 3, 'dddddddddddd' => 4, 'eeeeeeeeeeee' => 5]);
$class->addProperty('multilineLong', ['aaaaaaa' => 1, 'bbbbbbb' => 2, 'ccccccc' => 3, 'ddddddd' => 4, 'eeeeee' => 5]);
$class->addProperty('short', ['aaaaaaa' => 1, 'bbbbbbb' => 2, 'ccccccc' => 3, 'ddddddd' => 4, 'eeeeee' => 5]);

$class->addMethod('first')
->addComment('@return resource')
Expand Down
6 changes: 4 additions & 2 deletions tests/PhpGenerator/PsrPrinter.phpt
Expand Up @@ -25,7 +25,8 @@ $class->addConstant('FORCE_ARRAY', new PhpLiteral('Nette\Utils\Json::FORCE_ARRAY
->setVisibility('private')
->addComment('Commented');

$class->addConstant('MULTILINE', ['aaaaaaaaaaaa' => 1, 'bbbbbbbbbbb' => 2, 'cccccccccccccc' => 3, 'dddddddddddd' => 4, 'eeeeeeeeeeee' => 5]);
$class->addConstant('MULTILINE_LONG', ['aaaaaaa' => 1, 'bbbbbbb' => 2, 'ccccccc' => 3, 'ddddddd' => 4, 'eeeeee' => 5]);
$class->addConstant('SHORT', ['aaaaaaa' => 1, 'bbbbbbb' => 2, 'ccccccc' => 3, 'ddddddd' => 4, 'eeeeee' => 5]);

$class->addProperty('handle')
->setVisibility('private')
Expand All @@ -34,7 +35,8 @@ $class->addProperty('handle')
$class->addProperty('order')
->setValue(new PhpLiteral('RecursiveIteratorIterator::SELF_FIRST'));

$class->addProperty('multiline', ['aaaaaaaaaaaa' => 1, 'bbbbbbbbbbb' => 2, 'cccccccccccccc' => 3, 'dddddddddddd' => 4, 'eeeeeeeeeeee' => 5]);
$class->addProperty('multilineLong', ['aaaaaaa' => 1, 'bbbbbbb' => 2, 'ccccccc' => 3, 'ddddddd' => 4, 'eeeeee' => 5]);
$class->addProperty('short', ['aaaaaaa' => 1, 'bbbbbbb' => 2, 'ccccccc' => 3, 'ddddddd' => 4, 'eeeeee' => 5]);

$class->addMethod('first')
->addComment('@return resource')
Expand Down
27 changes: 15 additions & 12 deletions tests/PhpGenerator/expected/Printer.class.expect
Expand Up @@ -11,27 +11,30 @@ final class Example extends ParentClass implements IExample

/** Commented */
private const FORCE_ARRAY = Nette\Utils\Json::FORCE_ARRAY;
const MULTILINE = [
'aaaaaaaaaaaa' => 1,
'bbbbbbbbbbb' => 2,
'cccccccccccccc' => 3,
'dddddddddddd' => 4,
'eeeeeeeeeeee' => 5,
const MULTILINE_LONG = [
'aaaaaaa' => 1,
'bbbbbbb' => 2,
'ccccccc' => 3,
'ddddddd' => 4,
'eeeeee' => 5,
];
const SHORT = ['aaaaaaa' => 1, 'bbbbbbb' => 2, 'ccccccc' => 3, 'ddddddd' => 4, 'eeeeee' => 5];

/** @var resource orignal file handle */
private $handle;

public $order = RecursiveIteratorIterator::SELF_FIRST;

public $multiline = [
'aaaaaaaaaaaa' => 1,
'bbbbbbbbbbb' => 2,
'cccccccccccccc' => 3,
'dddddddddddd' => 4,
'eeeeeeeeeeee' => 5,
public $multilineLong = [
'aaaaaaa' => 1,
'bbbbbbb' => 2,
'ccccccc' => 3,
'ddddddd' => 4,
'eeeeee' => 5,
];

public $short = ['aaaaaaa' => 1, 'bbbbbbb' => 2, 'ccccccc' => 3, 'ddddddd' => 4, 'eeeeee' => 5];


/**
* @return resource
Expand Down
27 changes: 15 additions & 12 deletions tests/PhpGenerator/expected/PsrPrinter.class.expect
Expand Up @@ -11,27 +11,30 @@ final class Example extends ParentClass implements IExample

/** Commented */
private const FORCE_ARRAY = Nette\Utils\Json::FORCE_ARRAY;
const MULTILINE = [
'aaaaaaaaaaaa' => 1,
'bbbbbbbbbbb' => 2,
'cccccccccccccc' => 3,
'dddddddddddd' => 4,
'eeeeeeeeeeee' => 5,
const MULTILINE_LONG = [
'aaaaaaa' => 1,
'bbbbbbb' => 2,
'ccccccc' => 3,
'ddddddd' => 4,
'eeeeee' => 5,
];
const SHORT = ['aaaaaaa' => 1, 'bbbbbbb' => 2, 'ccccccc' => 3, 'ddddddd' => 4, 'eeeeee' => 5];

/** @var resource orignal file handle */
private $handle;

public $order = RecursiveIteratorIterator::SELF_FIRST;

public $multiline = [
'aaaaaaaaaaaa' => 1,
'bbbbbbbbbbb' => 2,
'cccccccccccccc' => 3,
'dddddddddddd' => 4,
'eeeeeeeeeeee' => 5,
public $multilineLong = [
'aaaaaaa' => 1,
'bbbbbbb' => 2,
'ccccccc' => 3,
'ddddddd' => 4,
'eeeeee' => 5,
];

public $short = ['aaaaaaa' => 1, 'bbbbbbb' => 2, 'ccccccc' => 3, 'ddddddd' => 4, 'eeeeee' => 5];

/**
* @return resource
*/
Expand Down

0 comments on commit 22228fd

Please sign in to comment.