Skip to content

Commit 1d0d54c

Browse files
zeleznypadg
authored andcommitted
Printer, Dumper: array wrapping counts with property/constant name length [Closes #45] (#47)
1 parent f68312b commit 1d0d54c

File tree

8 files changed

+159
-46
lines changed

8 files changed

+159
-46
lines changed

src/PhpGenerator/Dumper.php

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,13 @@ final class Dumper
2929
/**
3030
* Returns a PHP representation of a variable.
3131
*/
32-
public function dump($var): string
32+
public function dump($var, int $column = 0): string
3333
{
34-
return $this->dumpVar($var);
34+
return $this->dumpVar($var, [], 0, $column);
3535
}
3636

3737

38-
private function dumpVar(&$var, array $parents = [], int $level = 0): string
38+
private function dumpVar(&$var, array $parents = [], int $level = 0, int $column = 0): string
3939
{
4040
if ($var instanceof PhpLiteral) {
4141
return (string) $var;
@@ -47,7 +47,7 @@ private function dumpVar(&$var, array $parents = [], int $level = 0): string
4747
return $this->dumpString($var);
4848

4949
} elseif (is_array($var)) {
50-
return $this->dumpArray($var, $parents, $level);
50+
return $this->dumpArray($var, $parents, $level, $column);
5151

5252
} elseif (is_object($var)) {
5353
return $this->dumpObject($var, $parents, $level);
@@ -83,7 +83,7 @@ private function dumpString(string $var): string
8383
}
8484

8585

86-
private function dumpArray(array &$var, array $parents, int $level): string
86+
private function dumpArray(array &$var, array $parents, int $level, int $column): string
8787
{
8888
if (empty($var)) {
8989
return '[]';
@@ -99,14 +99,18 @@ private function dumpArray(array &$var, array $parents, int $level): string
9999
$counter = 0;
100100

101101
foreach ($var as $k => &$v) {
102-
$item = ($k === $counter ? '' : $this->dumpVar($k) . ' => ') . $this->dumpVar($v, $parents, $level + 1);
102+
$keyPart = $k === $counter ? '' : $this->dumpVar($k) . ' => ';
103103
$counter = is_int($k) ? max($k + 1, $counter) : $counter;
104-
$outInline .= ($outInline === '' ? '' : ', ') . $item;
105-
$outWrapped .= "\t$item,\n$space";
104+
$outInline .= ($outInline === '' ? '' : ', ') . $keyPart;
105+
$outInline .= $this->dumpVar($v, $parents, 0, $column + strlen($outInline));
106+
$outWrapped .= "\t"
107+
. $keyPart
108+
. $this->dumpVar($v, $parents, $level + 1, strlen($keyPart))
109+
. ",\n$space";
106110
}
107111

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

@@ -145,7 +149,10 @@ private function dumpObject(&$var, array $parents, int $level): string
145149

146150
foreach ($arr as $k => &$v) {
147151
if (!isset($props) || isset($props[$k])) {
148-
$out .= "$space\t" . $this->dumpVar($k) . ' => ' . $this->dumpVar($v, $parents, $level + 1) . ",\n";
152+
$out .= "$space\t"
153+
. ($keyPart = $this->dumpVar($k) . ' => ')
154+
. $this->dumpVar($v, $parents, $level + 1, strlen($keyPart))
155+
. ",\n";
149156
}
150157
}
151158

src/PhpGenerator/Printer.php

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -112,19 +112,22 @@ public function printClass(ClassType $class, PhpNamespace $namespace = null): st
112112

113113
$consts = [];
114114
foreach ($class->getConstants() as $const) {
115+
$def = ($const->getVisibility() ? $const->getVisibility() . ' ' : '') . 'const ' . $const->getName() . ' = ';
115116
$consts[] = Helpers::formatDocComment((string) $const->getComment())
116-
. ($const->getVisibility() ? $const->getVisibility() . ' ' : '')
117-
. 'const ' . $const->getName() . ' = ' . $this->dump($const->getValue()) . ";\n";
117+
. $def
118+
. $this->dump($const->getValue(), strlen($def)) . ";\n";
118119
}
119120

120121
$properties = [];
121122
foreach ($class->getProperties() as $property) {
122123
$type = $property->getType();
123-
$properties[] = Helpers::formatDocComment((string) $property->getComment())
124-
. ($property->getVisibility() ?: 'public') . ($property->isStatic() ? ' static' : '') . ' '
124+
$def = (($property->getVisibility() ?: 'public') . ($property->isStatic() ? ' static' : '') . ' '
125125
. ($type ? ($property->isNullable() ? '?' : '') . ($this->resolveTypes && $namespace ? $namespace->unresolveName($type) : $type) . ' ' : '')
126-
. '$' . $property->getName()
127-
. ($property->getValue() === null && !$property->isInitialized() ? '' : ' = ' . $this->dump($property->getValue()))
126+
. '$' . $property->getName());
127+
128+
$properties[] = Helpers::formatDocComment((string) $property->getComment())
129+
. $def
130+
. ($property->getValue() === null && !$property->isInitialized() ? '' : ' = ' . $this->dump($property->getValue(), strlen($def) + 3)) // 3 = ' = '
128131
. ";\n";
129132
}
130133

@@ -214,9 +217,9 @@ protected function indent(string $s): string
214217
}
215218

216219

217-
protected function dump($var): string
220+
protected function dump($var, int $column = 0): string
218221
{
219-
return (new Dumper)->dump($var);
222+
return (new Dumper)->dump($var, $column);
220223
}
221224

222225

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
/**
4+
* Test: Nette\PhpGenerator\Dumper::dump()
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
use Nette\PhpGenerator\Dumper;
10+
use Tester\Assert;
11+
12+
13+
require __DIR__ . '/../bootstrap.php';
14+
15+
16+
$dumper = new Dumper;
17+
18+
Assert::same('[1, 2, 3]', $dumper->dump([1, 2, 3], $dumper->wrapLength - 10));
19+
20+
same('[
21+
1,
22+
2,
23+
3,
24+
]', $dumper->dump([1, 2, 3], $dumper->wrapLength - 8));
25+
26+
27+
// ignore indent after new line
28+
same('[
29+
[1, 2, 3],
30+
]', $dumper->dump([[1, 2, 3]], $dumper->wrapLength - 8));
31+
32+
33+
// counts with length of key
34+
Assert::same('[8 => 1, 2, 3]', $dumper->dump([8 => 1, 2, 3], $dumper->wrapLength - 15));
35+
36+
same('[
37+
8 => 1,
38+
2,
39+
3,
40+
]', $dumper->dump([8 => 1, 2, 3], $dumper->wrapLength - 13));
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
/**
4+
* Test: Nette\PhpGenerator\Dumper::dump()
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
use Nette\PhpGenerator\Dumper;
10+
11+
12+
require __DIR__ . '/../bootstrap.php';
13+
14+
15+
$dumper = new Dumper;
16+
$dumper->wrapLength = 21;
17+
same("[
18+
'a' => [1, 2, 3],
19+
'aaaaaaaaa' => [
20+
1,
21+
2,
22+
3,
23+
],
24+
]",
25+
$dumper->dump([
26+
'a' => [1, 2, 3],
27+
'aaaaaaaaa' => [1, 2, 3],
28+
])
29+
);
30+
31+
same("(object) [
32+
'a' => [1, 2, 3],
33+
'aaaaaaaaa' => [
34+
1,
35+
2,
36+
3,
37+
],
38+
]",
39+
$dumper->dump((object) [
40+
'a' => [1, 2, 3],
41+
'aaaaaaaaa' => [1, 2, 3],
42+
])
43+
);
44+
45+
46+
$dumper = new Dumper;
47+
$dumper->wrapLength = 100;
48+
same("[
49+
[
50+
'a',
51+
'looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong',
52+
],
53+
]", $dumper->dump([['a', 'looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong']]));

tests/PhpGenerator/Printer.phpt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ $class->addConstant('FORCE_ARRAY', new PhpLiteral('Nette\Utils\Json::FORCE_ARRAY
2626
->setVisibility('private')
2727
->addComment('Commented');
2828

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

3132
$class->addProperty('handle')
3233
->setVisibility('private')
@@ -35,7 +36,8 @@ $class->addProperty('handle')
3536
$class->addProperty('order')
3637
->setValue(new PhpLiteral('RecursiveIteratorIterator::SELF_FIRST'));
3738

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

4042
$class->addMethod('first')
4143
->addComment('@return resource')

tests/PhpGenerator/PsrPrinter.phpt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ $class->addConstant('FORCE_ARRAY', new PhpLiteral('Nette\Utils\Json::FORCE_ARRAY
2525
->setVisibility('private')
2626
->addComment('Commented');
2727

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

3031
$class->addProperty('handle')
3132
->setVisibility('private')
@@ -34,7 +35,8 @@ $class->addProperty('handle')
3435
$class->addProperty('order')
3536
->setValue(new PhpLiteral('RecursiveIteratorIterator::SELF_FIRST'));
3637

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

3941
$class->addMethod('first')
4042
->addComment('@return resource')

tests/PhpGenerator/expected/Printer.class.expect

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,27 +11,30 @@ final class Example extends ParentClass implements IExample
1111

1212
/** Commented */
1313
private const FORCE_ARRAY = Nette\Utils\Json::FORCE_ARRAY;
14-
const MULTILINE = [
15-
'aaaaaaaaaaaa' => 1,
16-
'bbbbbbbbbbb' => 2,
17-
'cccccccccccccc' => 3,
18-
'dddddddddddd' => 4,
19-
'eeeeeeeeeeee' => 5,
14+
const MULTILINE_LONG = [
15+
'aaaaaaa' => 1,
16+
'bbbbbbb' => 2,
17+
'ccccccc' => 3,
18+
'ddddddd' => 4,
19+
'eeeeee' => 5,
2020
];
21+
const SHORT = ['aaaaaaa' => 1, 'bbbbbbb' => 2, 'ccccccc' => 3, 'ddddddd' => 4, 'eeeeee' => 5];
2122

2223
/** @var resource orignal file handle */
2324
private $handle;
2425

2526
public $order = RecursiveIteratorIterator::SELF_FIRST;
2627

27-
public $multiline = [
28-
'aaaaaaaaaaaa' => 1,
29-
'bbbbbbbbbbb' => 2,
30-
'cccccccccccccc' => 3,
31-
'dddddddddddd' => 4,
32-
'eeeeeeeeeeee' => 5,
28+
public $multilineLong = [
29+
'aaaaaaa' => 1,
30+
'bbbbbbb' => 2,
31+
'ccccccc' => 3,
32+
'ddddddd' => 4,
33+
'eeeeee' => 5,
3334
];
3435

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

3639
/**
3740
* @return resource

tests/PhpGenerator/expected/PsrPrinter.class.expect

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,27 +11,30 @@ final class Example extends ParentClass implements IExample
1111

1212
/** Commented */
1313
private const FORCE_ARRAY = Nette\Utils\Json::FORCE_ARRAY;
14-
const MULTILINE = [
15-
'aaaaaaaaaaaa' => 1,
16-
'bbbbbbbbbbb' => 2,
17-
'cccccccccccccc' => 3,
18-
'dddddddddddd' => 4,
19-
'eeeeeeeeeeee' => 5,
14+
const MULTILINE_LONG = [
15+
'aaaaaaa' => 1,
16+
'bbbbbbb' => 2,
17+
'ccccccc' => 3,
18+
'ddddddd' => 4,
19+
'eeeeee' => 5,
2020
];
21+
const SHORT = ['aaaaaaa' => 1, 'bbbbbbb' => 2, 'ccccccc' => 3, 'ddddddd' => 4, 'eeeeee' => 5];
2122

2223
/** @var resource orignal file handle */
2324
private $handle;
2425

2526
public $order = RecursiveIteratorIterator::SELF_FIRST;
2627

27-
public $multiline = [
28-
'aaaaaaaaaaaa' => 1,
29-
'bbbbbbbbbbb' => 2,
30-
'cccccccccccccc' => 3,
31-
'dddddddddddd' => 4,
32-
'eeeeeeeeeeee' => 5,
28+
public $multilineLong = [
29+
'aaaaaaa' => 1,
30+
'bbbbbbb' => 2,
31+
'ccccccc' => 3,
32+
'ddddddd' => 4,
33+
'eeeeee' => 5,
3334
];
3435

36+
public $short = ['aaaaaaa' => 1, 'bbbbbbb' => 2, 'ccccccc' => 3, 'ddddddd' => 4, 'eeeeee' => 5];
37+
3538
/**
3639
* @return resource
3740
*/

0 commit comments

Comments
 (0)