Skip to content

Commit

Permalink
Helpers: fixed wrapping array because the property name length [Closes
Browse files Browse the repository at this point in the history
  • Loading branch information
zeleznypa authored and dg committed Nov 19, 2019
1 parent 1fa23e4 commit 9c6b2e3
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 19 deletions.
22 changes: 11 additions & 11 deletions src/PhpGenerator/Dumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ final class Dumper
/**
* Returns a PHP representation of a variable.
*/
public function dump($var): string
public function dump($var, int $initLength = 0): string
{
return $this->dumpVar($var);
return $this->dumpVar($var, $initLength);
}


private function dumpVar(&$var, int $level = 0)
private function dumpVar(&$var, int $initLength = 0, int $level = 0)
{
if ($var instanceof PhpLiteral) {
return (string) $var;
Expand All @@ -51,10 +51,10 @@ private function dumpVar(&$var, int $level = 0)
return $this->dumpString($var);

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

} elseif (is_object($var)) {
return $this->dumpObject($var, $level);
return $this->dumpObject($var, $initLength, $level);

} elseif (is_resource($var)) {
throw new Nette\InvalidArgumentException('Cannot dump resource.');
Expand Down Expand Up @@ -87,7 +87,7 @@ private function dumpString($var): string
}


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

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


private function dumpObject(&$var, int $level): string
private function dumpObject(&$var, int $initLength = 0, int $level): string
{
if ($var instanceof \Serializable) {
$var = serialize($var);
return 'unserialize(' . $this->dumpVar($var, $level) . ')';
return 'unserialize(' . $this->dumpVar($var, $initLength, $level) . ')';

} elseif ($var instanceof \Closure) {
throw new Nette\InvalidArgumentException('Cannot dump closure.');
Expand Down Expand Up @@ -156,7 +156,7 @@ private function dumpObject(&$var, int $level): string
}
foreach ($arr as $k => &$v) {
if (!isset($props) || isset($props[$k])) {
$out .= $space . $this->indentation . $this->dumpVar($k, $level + 1) . ' => ' . $this->dumpVar($v, $level + 1) . ",\n";
$out .= $space . $this->indentation . $this->dumpVar($k, $initLength, $level + 1) . ' => ' . $this->dumpVar($v, $initLength, $level + 1) . ",\n";
}
}
array_pop($list);
Expand Down
15 changes: 9 additions & 6 deletions src/PhpGenerator/Printer.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,13 +112,16 @@ public function printClass(ClassType $class, PhpNamespace $namespace = null): st
}

$properties = [];
$initLengthDefault = strlen($this->indentation) + 4;
foreach ($class->getProperties() as $property) {
$type = $property->getType();
$properties[] = Helpers::formatDocComment((string) $property->getComment())
. ($property->getVisibility() ?: 'public') . ($property->isStatic() ? ' static' : '') . ' '
$definition = (($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())
. $definition
. ($property->getValue() === null && !$property->isInitialized() ? '' : ' = ' . $this->dump($property->getValue(), strlen($definition) + $initLengthDefault))
. ";\n";
}

Expand Down Expand Up @@ -207,9 +210,9 @@ protected function indent(string $s): string
}


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


Expand Down
4 changes: 2 additions & 2 deletions src/PhpGenerator/PsrPrinter.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ protected function indent(string $s): string
}


protected function dump($var): string
protected function dump($var, int $initLength = 0): string
{
return str_replace("\t", $this->indentation, parent::dump($var));
return str_replace("\t", $this->indentation, parent::dump($var, $initLength));
}
}
6 changes: 6 additions & 0 deletions tests/PhpGenerator/Dumper.dump().phpt
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ Assert::same('[]', $dumper->dump([]));
Assert::same('[$s]', $dumper->dump([new PhpLiteral('$s')]));

Assert::same('[1, 2, 3]', $dumper->dump([1, 2, 3]));
Assert::same('[1, 2, 3]', $dumper->dump([1, 2, 3], 91));
same('[
1,
2,
3,
]', $dumper->dump([1, 2, 3], 92));
Assert::same("['a', 7 => 'b', 'c', '9a' => 'd', 'e']", $dumper->dump(['a', 7 => 'b', 'c', '9a' => 'd', 9 => 'e']));
same("[
[
Expand Down
4 changes: 4 additions & 0 deletions tests/PhpGenerator/Printer.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ $class->addProperty('order')

$class->addProperty('multiline', ['aaaaaaaaaaaa' => 1, 'bbbbbbbbbbb' => 2, 'cccccccccccccc' => 3, 'dddddddddddd' => 4, 'eeeeeeeeeeee' => 5]);

$class->addProperty('multilineShort', ['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')
->setFinal(true)
Expand Down
4 changes: 4 additions & 0 deletions tests/PhpGenerator/PsrPrinter.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ $class->addProperty('order')

$class->addProperty('multiline', ['aaaaaaaaaaaa' => 1, 'bbbbbbbbbbb' => 2, 'cccccccccccccc' => 3, 'dddddddddddd' => 4, 'eeeeeeeeeeee' => 5]);

$class->addProperty('multilineShort', ['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')
->setFinal(true)
Expand Down
10 changes: 10 additions & 0 deletions tests/PhpGenerator/expected/Printer.class.expect
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,16 @@ final class Example extends ParentClass implements IExample
'eeeeeeeeeeee' => 5,
];

public $multilineShort = [
'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
10 changes: 10 additions & 0 deletions tests/PhpGenerator/expected/PsrPrinter.class.expect
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,16 @@ final class Example extends ParentClass implements IExample
'eeeeeeeeeeee' => 5,
];

public $multilineShort = [
'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 9c6b2e3

Please sign in to comment.