Skip to content

Commit 85080be

Browse files
committed
Helpers::formatArgs() ...? is substitution for ?*
1 parent ec39da7 commit 85080be

File tree

2 files changed

+5
-4
lines changed

2 files changed

+5
-4
lines changed

src/PhpGenerator/Helpers.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ public static function format(string $statement, ...$args): string
162162
*/
163163
public static function formatArgs(string $statement, array $args): string
164164
{
165-
$tokens = preg_split('#(\$\?|->\?|::\?|\?\*|\?)#', $statement, -1, PREG_SPLIT_DELIM_CAPTURE);
165+
$tokens = preg_split('#(\.\.\.\?|\$\?|->\?|::\?|\?\*|\?)#', $statement, -1, PREG_SPLIT_DELIM_CAPTURE);
166166
$res = '';
167167
foreach ($tokens as $n => $token) {
168168
if ($n % 2 === 0) {
@@ -171,7 +171,7 @@ public static function formatArgs(string $statement, array $args): string
171171
throw new Nette\InvalidArgumentException('Insufficient number of arguments.');
172172
} elseif ($token === '?') {
173173
$res .= self::dump(array_shift($args));
174-
} elseif ($token === '?*') {
174+
} elseif ($token === '...?' || $token === '?*') {
175175
$arg = array_shift($args);
176176
if (!is_array($arg)) {
177177
throw new Nette\InvalidArgumentException('Argument must be an array.');

tests/PhpGenerator/Helpers.format.phpt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,15 @@ Assert::same('func(1)', Helpers::format('func(?)', 1));
1818
Assert::same('func', Helpers::formatArgs('func', []));
1919
Assert::same('func(1)', Helpers::formatArgs('func(?)', [1]));
2020
Assert::same('func([1, 2])', Helpers::formatArgs('func(?)', [[1, 2]]));
21-
Assert::same('func(1, 2)', Helpers::formatArgs('func(?*)', [[1, 2]]));
21+
Assert::same('func(1, 2)', Helpers::formatArgs('func(...?)', [[1, 2]]));
22+
Assert::same('func(1, 2)', Helpers::formatArgs('func(?*)', [[1, 2]])); // old way
2223
Assert::same(
2324
"func(10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26,\n\t27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44,\n\t45, 46, 47, 48, 49, 50)",
2425
Helpers::formatArgs('func(?*)', [range(10, 50)])
2526
);
2627

2728
Assert::exception(function () {
28-
Helpers::formatArgs('func(?*)', [1, 2]);
29+
Helpers::formatArgs('func(...?)', [1, 2]);
2930
}, Nette\InvalidArgumentException::class, 'Argument must be an array.');
3031

3132
Assert::exception(function () {

0 commit comments

Comments
 (0)