Skip to content

Commit 64ffb63

Browse files
committed
Printer: added support for printing attributes on PhpFile and TraitUse
1 parent 7e113c0 commit 64ffb63

File tree

6 files changed

+62
-0
lines changed

6 files changed

+62
-0
lines changed

src/PhpGenerator/PhpFile.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
final class PhpFile
2323
{
2424
use Traits\CommentAware;
25+
use Traits\AttributeAware;
2526

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

src/PhpGenerator/Printer.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ public function printClass(
153153
$resolutions = implode(";\n", $trait->getResolutions());
154154
$resolutions = Helpers::simplifyTaggedNames($resolutions, $this->namespace);
155155
$traits[] = $this->printDocComment($trait)
156+
. $this->printAttributes($trait->getAttributes())
156157
. 'use ' . $resolver($trait->getName())
157158
. ($resolutions
158159
? " {\n" . $this->indent($resolutions) . ";\n}\n"
@@ -282,6 +283,7 @@ public function printFile(PhpFile $file): string
282283
return "<?php\n"
283284
. ($file->getComment() ? "\n" . $this->printDocComment($file) : '')
284285
. "\n"
286+
. ($file->getAttributes() ? $this->printAttributes($file->getAttributes()) . "\n" : '')
285287
. ($file->hasStrictTypes() ? "declare(strict_types=1);\n\n" : '')
286288
. implode("\n\n", $namespaces);
287289
}

src/PhpGenerator/TraitUse.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ final class TraitUse
1717
{
1818
use Traits\NameAware;
1919
use Traits\CommentAware;
20+
use Traits\AttributeAware;
2021

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

tests/PhpGenerator/ClassType.attributes.phpt

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

26+
$class->addTrait('ExampleTrait')
27+
->addAttribute('TraitAttribute');
28+
2629
$class->addConstant('FOO', 123)
2730
->addComment('Commented')
2831
->addAttribute('ExampleAttribute')

tests/PhpGenerator/PhpFile.phpt

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

137137

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