Skip to content

Commit c1f6b21

Browse files
committed
Helpers::formatArgs() chops ?* parameters when are longer than WRAP_LENGTH
1 parent 2e55362 commit c1f6b21

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
@@ -186,11 +186,14 @@ public static function formatArgs($statement, array $args)
186186
if (!is_array($arg)) {
187187
throw new Nette\InvalidArgumentException('Argument must be an array.');
188188
}
189-
$sep = '';
189+
$items = [];
190190
foreach ($arg as $tmp) {
191-
$res .= $sep . self::dump($tmp);
192-
$sep = strlen($res) - strrpos($res, "\n") > self::WRAP_LENGTH ? ",\n\t" : ', ';
191+
$items[] = self::dump($tmp);
193192
}
193+
$res .= strlen($tmp = implode(', ', $items)) > self::WRAP_LENGTH && count($items) > 1
194+
? "\n" . Nette\Utils\Strings::indent(implode(",\n", $items), 1) . "\n"
195+
: $tmp;
196+
194197
} else { // $ -> ::
195198
$res .= substr($token, 0, -1) . self::formatMember(array_shift($args));
196199
}

tests/PhpGenerator/Helpers.format.phpt

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

2956
Assert::exception(function () {

0 commit comments

Comments
 (0)