Skip to content

Commit 0536bd0

Browse files
committed
Dumper::format() syntax ...? now supports named parameters (possible BC break)
1 parent 35becde commit 0536bd0

File tree

3 files changed

+14
-3
lines changed

3 files changed

+14
-3
lines changed

readme.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,15 @@ function foo()
388388
}
389389
```
390390

391+
You can also use PHP 8 named parameters:
392+
393+
```php
394+
$items = ['foo' => 1, 'bar' => true];
395+
$function->setBody('myfunc(...?);', [$items]);
396+
397+
// myfunc(foo: 1, bar: true);
398+
```
399+
391400
Escape placeholder using slash:
392401

393402
```php

src/PhpGenerator/Dumper.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -209,10 +209,11 @@ private function dumpArguments(array &$var, int $column): string
209209
{
210210
$outInline = $outWrapped = '';
211211

212-
foreach ($var as &$v) {
212+
foreach ($var as $k => &$v) {
213+
$k = is_int($k) ? '' : $k . ': ';
213214
$outInline .= $outInline === '' ? '' : ', ';
214-
$outInline .= $this->dumpVar($v, [$var], 0, $column + strlen($outInline));
215-
$outWrapped .= ($outWrapped === '' ? '' : ',') . "\n\t" . $this->dumpVar($v, [$var], 1);
215+
$outInline .= $k . $this->dumpVar($v, [$var], 0, $column + strlen($outInline));
216+
$outWrapped .= ($outWrapped === '' ? '' : ',') . "\n\t" . $k . $this->dumpVar($v, [$var], 1);
216217
}
217218

218219
return count($var) > 1 && (strpos($outInline, "\n") !== false || $column + strlen($outInline) > $this->wrapLength)

tests/PhpGenerator/Dumper.format().phpt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ 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]));
2223
Assert::same('func(1, 2)', $dumper->format('func(?*)', [1, 2])); // old way
2324

2425
$dumper->wrapLength = 100;

0 commit comments

Comments
 (0)