Skip to content

Commit b381eca

Browse files
committed
Helpers::formatArgs() chops ?* parameters when are longer than WRAP_LENGTH
1 parent 466ef30 commit b381eca

File tree

2 files changed

+36
-6
lines changed

2 files changed

+36
-6
lines changed

src/PhpGenerator/Helpers.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -183,11 +183,14 @@ public static function formatArgs(string $statement, array $args): string
183183
if (!is_array($arg)) {
184184
throw new Nette\InvalidArgumentException('Argument must be an array.');
185185
}
186-
$sep = '';
186+
$items = [];
187187
foreach ($arg as $tmp) {
188-
$res .= $sep . self::dump($tmp);
189-
$sep = strlen($res) - strrpos($res, "\n") > self::WRAP_LENGTH ? ",\n\t" : ', ';
188+
$items[] = self::dump($tmp);
190189
}
190+
$res .= strlen($tmp = implode(', ', $items)) > self::WRAP_LENGTH && count($items) > 1
191+
? "\n" . Nette\Utils\Strings::indent(implode(",\n", $items), 1) . "\n"
192+
: $tmp;
193+
191194
} else { // $ -> ::
192195
$res .= substr($token, 0, -1) . self::formatMember(array_shift($args));
193196
}

tests/PhpGenerator/Helpers.format.phpt

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,36 @@ Assert::same('func([1, 2])', Helpers::formatArgs('func(?)', [[1, 2]]));
2222
Assert::same('func(1, 2)', Helpers::formatArgs('func(...?)', [[1, 2]]));
2323
Assert::same('func(1, 2)', Helpers::formatArgs('func(?*)', [[1, 2]])); // old way
2424
Assert::match(
25-
'func(10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34,
26-
35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50)',
27-
Helpers::formatArgs('func(?*)', [range(10, 50)])
25+
'func(
26+
10,
27+
11,
28+
12,
29+
13,
30+
14,
31+
15,
32+
16,
33+
17,
34+
18,
35+
19,
36+
20,
37+
21,
38+
22,
39+
23,
40+
24,
41+
25,
42+
26,
43+
27,
44+
28,
45+
29,
46+
30,
47+
31,
48+
32,
49+
33,
50+
34,
51+
35,
52+
36
53+
)',
54+
Helpers::formatArgs('func(?*)', [range(10, 36)])
2855
);
2956

3057
Assert::exception(function () {

0 commit comments

Comments
 (0)