Skip to content

Commit 06976aa

Browse files
committed
Support arguments in dumpAttribute
1 parent dc9b51a commit 06976aa

File tree

5 files changed

+48
-8
lines changed

5 files changed

+48
-8
lines changed

README.md

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,8 @@ echo $generator->dumpFile([
105105
'',
106106

107107
$generator->dumpAttribute('Example\Attributes\Something'),
108+
$generator->dumpAttribute('Example\Attributes\Single', ['value: "Hello, World!"']),
109+
$generator->dumpAttribute('Example\Attributes\Multiple', ['value: "Hello, World!"', 'other: "Other value"']),
108110
sprintf(
109111
'final readonly class %s extends %s',
110112
$generator->import('Example\Demo'),
@@ -140,13 +142,20 @@ declare(strict_types=1);
140142
namespace Example\Demo;
141143

142144
use DateTimeImmutable;
145+
use Example\Attributes\Multiple;
146+
use Example\Attributes\Single;
143147
use Example\Attributes\Something;
144148
use Example\Demo;
145149
use Example\Parent;
146150

147151
// Auto-generated example file
148152

149153
#[Something]
154+
#[Single(value: "Hello, World!")]
155+
#[Multiple(
156+
value: "Hello, World!",
157+
other: "Other value",
158+
)]
150159
final readonly class Demo extends Parent
151160
{
152161
public function __construct(
@@ -183,8 +192,8 @@ echo $generator->dumpFile(function () use ($generator) {
183192
yield '';
184193

185194
// Class with attributes
186-
yield $generator->dumpAttribute('Example\Attributes\Entity');
187-
yield $generator->dumpAttribute('Example\Attributes\Table');
195+
yield from $generator->dumpAttribute('Example\Attributes\Entity');
196+
yield from $generator->dumpAttribute('Example\Attributes\Table');
188197
yield sprintf(
189198
'final class DemoClass extends %s implements %s, %s',
190199
$generator->import('Example\BaseClass'),

examples/class.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
'',
1414

1515
$generator->dumpAttribute('Example\Attributes\Something'),
16+
$generator->dumpAttribute('Example\Attributes\Single', ['value: "Hello, World!"']),
17+
$generator->dumpAttribute('Example\Attributes\Multiple', ['value: "Hello, World!"', 'other: "Other value"']),
1618
sprintf(
1719
'final readonly class %s extends %s',
1820
$generator->import('Example\Demo'),

examples/example.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
yield '';
1818

1919
// Class with attributes
20-
yield $generator->dumpAttribute('Example\Attributes\Entity');
21-
yield $generator->dumpAttribute('Example\Attributes\Table');
20+
yield from $generator->dumpAttribute('Example\Attributes\Entity');
21+
yield from $generator->dumpAttribute('Example\Attributes\Table');
2222
yield sprintf(
2323
'final class DemoClass extends %s implements %s, %s',
2424
$generator->import('Example\BaseClass'),

src/CodeGenerator.php

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -255,10 +255,26 @@ public function importByParent(Importable | string $name) : string
255255

256256
/**
257257
* Generates a PHP attribute string for the given fully qualified class name
258+
*
259+
* @param CodeLines $args
260+
* @return Generator<CodeLine>
258261
*/
259-
public function dumpAttribute(FullyQualified | string $fqcn) : string
260-
{
261-
return sprintf('#[%s]', $this->import($fqcn));
262+
public function dumpAttribute(
263+
FullyQualified | string $fqcn,
264+
array | Closure | Generator | string $args = [],
265+
bool $addCommaAfterEachArgument = true,
266+
) : Generator {
267+
$args = self::resolveIterable($args);
268+
269+
if ($args === []) {
270+
yield sprintf('#[%s]', $this->import($fqcn));
271+
} elseif (count($args) === 1) {
272+
yield from $this->wrap(sprintf('#[%s(', $this->import($fqcn)), $args, ')]');
273+
} else {
274+
yield sprintf('#[%s(', $this->import($fqcn));
275+
yield Group::indent($addCommaAfterEachArgument ? $this->allSuffix(',', $args) : $args);
276+
yield ')]';
277+
}
262278
}
263279

264280
/**

tests/CodeGeneratorTest.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -414,10 +414,23 @@ public function testImportByParentWithDeepSubNamespace() : void
414414

415415
public function testDumpAttribute() : void
416416
{
417-
self::assertSame(
417+
$this->assertDump(
418418
'#[Required]',
419419
$this->generator->dumpAttribute('App\\Attributes\\Required'),
420420
);
421+
$this->assertDump(
422+
'#[Required(true)]',
423+
$this->generator->dumpAttribute('App\\Attributes\\Required', ['true']),
424+
);
425+
$this->assertDump(
426+
<<<'PHP'
427+
#[Required(
428+
true,
429+
false,
430+
)]
431+
PHP,
432+
$this->generator->dumpAttribute('App\\Attributes\\Required', ['true', 'false']),
433+
);
421434
}
422435

423436
public function testDumpClassReference() : void

0 commit comments

Comments
 (0)