Skip to content

Commit 5cacadc

Browse files
committed
Dumper: changed placeholder for named arguments to ...?:
1 parent 9162f74 commit 5cacadc

File tree

4 files changed

+13
-12
lines changed

4 files changed

+13
-12
lines changed

readme.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,7 @@ function foo()
425425

426426
You can use special placeholders for handy way to inject variables.
427427

428-
Simple placeholders:
428+
Simple placeholders `?`
429429

430430
```php
431431
$str = 'any string';
@@ -444,7 +444,7 @@ function foo()
444444
}
445445
```
446446

447-
Variadic placeholder:
447+
Variadic placeholder `...?`
448448

449449
```php
450450
$items = [1, 2, 3];
@@ -462,16 +462,16 @@ function foo()
462462
}
463463
```
464464

465-
You can also use PHP 8 named parameters:
465+
You can also use PHP 8 named parameters using placeholder `...?:`
466466

467467
```php
468468
$items = ['foo' => 1, 'bar' => true];
469-
$function->setBody('myfunc(...?);', [$items]);
469+
$function->setBody('myfunc(...?:);', [$items]);
470470

471471
// myfunc(foo: 1, bar: true);
472472
```
473473

474-
Escape placeholder using slash:
474+
Escape placeholder using slash `\?`
475475

476476
```php
477477
$num = 3;

src/PhpGenerator/Dumper.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ private function dumpObject(&$var, array $parents, int $level): string
172172
*/
173173
public function format(string $statement, ...$args): string
174174
{
175-
$tokens = preg_split('#(\.\.\.\?|\$\?|->\?|::\?|\\\\\?|\?\*|\?)#', $statement, -1, PREG_SPLIT_DELIM_CAPTURE);
175+
$tokens = preg_split('#(\.\.\.\?:?|\$\?|->\?|::\?|\\\\\?|\?\*|\?)#', $statement, -1, PREG_SPLIT_DELIM_CAPTURE);
176176
$res = '';
177177
foreach ($tokens as $n => $token) {
178178
if ($n % 2 === 0) {
@@ -183,12 +183,12 @@ public function format(string $statement, ...$args): string
183183
throw new Nette\InvalidArgumentException('Insufficient number of arguments.');
184184
} elseif ($token === '?') {
185185
$res .= $this->dump(array_shift($args), strlen($res) - strrpos($res, "\n"));
186-
} elseif ($token === '...?' || $token === '?*') {
186+
} elseif ($token === '...?' || $token === '...?:' || $token === '?*') {
187187
$arg = array_shift($args);
188188
if (!is_array($arg)) {
189189
throw new Nette\InvalidArgumentException('Argument must be an array.');
190190
}
191-
$res .= $this->dumpArguments($arg, strlen($res) - strrpos($res, "\n"));
191+
$res .= $this->dumpArguments($arg, strlen($res) - strrpos($res, "\n"), $token === '...?:');
192192

193193
} else { // $ -> ::
194194
$arg = array_shift($args);
@@ -205,12 +205,12 @@ public function format(string $statement, ...$args): string
205205
}
206206

207207

208-
private function dumpArguments(array &$var, int $column): string
208+
private function dumpArguments(array &$var, int $column, bool $named): string
209209
{
210210
$outInline = $outWrapped = '';
211211

212212
foreach ($var as $k => &$v) {
213-
$k = is_int($k) ? '' : $k . ': ';
213+
$k = !$named || is_int($k) ? '' : $k . ': ';
214214
$outInline .= $outInline === '' ? '' : ', ';
215215
$outInline .= $k . $this->dumpVar($v, [$var], 0, $column + strlen($outInline));
216216
$outWrapped .= ($outWrapped === '' ? '' : ',') . "\n\t" . $k . $this->dumpVar($v, [$var], 1);

src/PhpGenerator/Printer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ private function printAttributes(array $attrs, ?PhpNamespace $namespace, bool $i
311311
}
312312
$items = [];
313313
foreach ($attrs as $attr) {
314-
$args = (new Dumper)->format('...?', $attr->getArguments());
314+
$args = (new Dumper)->format('...?:', $attr->getArguments());
315315
$items[] = $this->printType($attr->getName(), false, $namespace) . ($args ? "($args)" : '');
316316
}
317317
return $inline

tests/PhpGenerator/Dumper.format().phpt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ Assert::same('func(1)', $dumper->format('func(?)', 1));
1919
Assert::same('func(1 ? 2 : 3)', $dumper->format('func(1 \? 2 : 3)'));
2020
Assert::same('func([1, 2])', $dumper->format('func(?)', [1, 2]));
2121
Assert::same('func(1, 2)', $dumper->format('func(...?)', [1, 2]));
22-
Assert::same('func(1, a: 2)', $dumper->format('func(...?)', [1, 'a' => 2]));
22+
Assert::same('func(1, 2)', $dumper->format('func(...?)', [1, 'a' => 2]));
23+
Assert::same('func(1, a: 2)', $dumper->format('func(...?:)', [1, 'a' => 2])); // named args
2324
Assert::same('func(1, 2)', $dumper->format('func(?*)', [1, 2])); // old way
2425

2526
$dumper->wrapLength = 100;

0 commit comments

Comments
 (0)