Skip to content

Commit fa745a5

Browse files
committed
Printer: added support for printing attributes on PhpFile and TraitUse
1 parent 57eaf0b commit fa745a5

6 files changed

Lines changed: 62 additions & 0 deletions

File tree

src/PhpGenerator/PhpFile.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
final class PhpFile
2525
{
2626
use Traits\CommentAware;
27+
use Traits\AttributeAware;
2728

2829
/** @var array<string, PhpNamespace> */
2930
private array $namespaces = [];

src/PhpGenerator/Printer.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ public function printClass(
155155
$resolutions = implode(";\n", $trait->getResolutions());
156156
$resolutions = Helpers::simplifyTaggedNames($resolutions, $this->namespace);
157157
$traits[] = $this->printDocComment($trait)
158+
. $this->printAttributes($trait->getAttributes())
158159
. 'use ' . $resolver($trait->getName())
159160
. ($resolutions
160161
? " {\n" . $this->indent($resolutions) . ";\n}\n"
@@ -284,6 +285,7 @@ public function printFile(PhpFile $file): string
284285
return "<?php\n"
285286
. ($file->getComment() ? "\n" . $this->printDocComment($file) : '')
286287
. "\n"
288+
. ($file->getAttributes() ? $this->printAttributes($file->getAttributes()) . "\n" : '')
287289
. ($file->hasStrictTypes() ? "declare(strict_types=1);\n\n" : '')
288290
. implode("\n\n", $namespaces);
289291
}

src/PhpGenerator/TraitUse.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ final class TraitUse
1919
{
2020
use Traits\NameAware;
2121
use Traits\CommentAware;
22+
use Traits\AttributeAware;
2223

2324
/** @var list<string> */
2425
private array $resolutions = [];

tests/PhpGenerator/ClassType.attributes.phpt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ $class
2525
],
2626
]);
2727

28+
$class->addTrait('ExampleTrait')
29+
->addAttribute('TraitAttribute');
30+
2831
$class->addConstant('FOO', 123)
2932
->addComment('Commented')
3033
->addAttribute('ExampleAttribute')

tests/PhpGenerator/PhpFile.phpt

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,58 @@ test('file with strict types declaration', function () {
137137
});
138138

139139

140+
test('file with attributes', function () {
141+
$file = new PhpFile;
142+
$file->addAttribute('FileAttribute');
143+
$file->addAttribute('AnotherAttribute', ['key' => 'value']);
144+
$file->addClass('A');
145+
146+
same(
147+
<<<'XX'
148+
<?php
149+
150+
#[FileAttribute]
151+
#[AnotherAttribute(key: 'value')]
152+
153+
class A
154+
{
155+
}
156+
157+
XX,
158+
(string) $file,
159+
);
160+
});
161+
162+
163+
test('file with comment, attributes and strict types', function () {
164+
$file = new PhpFile;
165+
$file->addComment('This file is auto-generated.');
166+
$file->addAttribute('FileAttribute');
167+
$file->setStrictTypes();
168+
$file->addClass('A');
169+
170+
same(
171+
<<<'XX'
172+
<?php
173+
174+
/**
175+
* This file is auto-generated.
176+
*/
177+
178+
#[FileAttribute]
179+
180+
declare(strict_types=1);
181+
182+
class A
183+
{
184+
}
185+
186+
XX,
187+
(string) $file,
188+
);
189+
});
190+
191+
140192
test('fromCode extracts file from PHP code', function () {
141193
$file = PhpFile::fromCode(file_get_contents(__DIR__ . '/fixtures/classes.php'));
142194
Assert::type(PhpFile::class, $file);

tests/PhpGenerator/expected/ClassType.attributes.expect

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
#[Table(name: 'user', constraints: [new UniqueConstraint(name: 'ean', columns: ['ean'])])]
77
class Example
88
{
9+
#[TraitAttribute]
10+
use ExampleTrait;
11+
912
/** Commented */
1013
#[ExampleAttribute]
1114
#[WithArguments(true)]

0 commit comments

Comments
 (0)