Skip to content

Commit b2409d4

Browse files
committed
Dumper::format() array wrapping counts with code length
1 parent 1d0d54c commit b2409d4

File tree

2 files changed

+68
-8
lines changed

2 files changed

+68
-8
lines changed

src/PhpGenerator/Dumper.php

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -179,19 +179,13 @@ public function format(string $statement, ...$args): string
179179
} elseif (!$args) {
180180
throw new Nette\InvalidArgumentException('Insufficient number of arguments.');
181181
} elseif ($token === '?') {
182-
$res .= $this->dump(array_shift($args));
182+
$res .= $this->dump(array_shift($args), strlen($res) - strrpos($res, "\n"));
183183
} elseif ($token === '...?' || $token === '?*') {
184184
$arg = array_shift($args);
185185
if (!is_array($arg)) {
186186
throw new Nette\InvalidArgumentException('Argument must be an array.');
187187
}
188-
$items = [];
189-
foreach ($arg as $tmp) {
190-
$items[] = $this->dump($tmp);
191-
}
192-
$res .= strlen($tmp = implode(', ', $items)) > $this->wrapLength && count($items) > 1
193-
? "\n" . Nette\Utils\Strings::indent(implode(",\n", $items)) . "\n"
194-
: $tmp;
188+
$res .= $this->dumpArguments($arg, strlen($res) - strrpos($res, "\n"));
195189

196190
} else { // $ -> ::
197191
$arg = array_shift($args);
@@ -208,6 +202,22 @@ public function format(string $statement, ...$args): string
208202
}
209203

210204

205+
private function dumpArguments(array &$var, int $column): string
206+
{
207+
$outInline = $outWrapped = '';
208+
209+
foreach ($var as &$v) {
210+
$outInline .= $outInline === '' ? '' : ', ';
211+
$outInline .= $this->dumpVar($v, [$var], 0, $column + strlen($outInline));
212+
$outWrapped .= ($outWrapped === '' ? '' : ',') . "\n\t" . $this->dumpVar($v, [$var], 1);
213+
}
214+
215+
return count($var) > 1 && (strpos($outInline, "\n") !== false || $column + strlen($outInline) > $this->wrapLength)
216+
? $outWrapped . "\n"
217+
: $outInline;
218+
}
219+
220+
211221
/**
212222
* @return object
213223
* @internal
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
/**
4+
* Test: Nette\PhpGenerator\Dumper::format()
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+
$dumper->wrapLength = 100;
18+
19+
Assert::same('func([1, 2, 3])', $dumper->format('func(?)', [1, 2, 3]));
20+
21+
same('loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong([
22+
1,
23+
2,
24+
3,
25+
])', $dumper->format('loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong(?)', [1, 2, 3]));
26+
27+
28+
same('looooooooooooooooooooooooooooooooooooooooo([1, 2, 3]) + ooooooooooooooooooooooooooooooooooooooooooooooong([
29+
1,
30+
2,
31+
3,
32+
])', $dumper->format('looooooooooooooooooooooooooooooooooooooooo(?) + ooooooooooooooooooooooooooooooooooooooooooooooong(?)', [1, 2, 3], [1, 2, 3]));
33+
34+
35+
// variadics
36+
Assert::same('func(1, 2, 3)', $dumper->format('func(...?)', [1, 2, 3]));
37+
38+
39+
same('loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong(
40+
1,
41+
2,
42+
3
43+
)', $dumper->format('loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong(...?)', [1, 2, 3]));
44+
45+
46+
same('looooooooooooooooooooooooooooooooooooooooo(1, 2, 3) + ooooooooooooooooooooooooooooooooooooooooooooooong(
47+
1,
48+
2,
49+
3
50+
)', $dumper->format('looooooooooooooooooooooooooooooooooooooooo(...?) + ooooooooooooooooooooooooooooooooooooooooooooooong(...?)', [1, 2, 3], [1, 2, 3]));

0 commit comments

Comments
 (0)