Skip to content

Commit 1170323

Browse files
committed
"use Trait" can have comments [Closes #90]
1 parent 1cc5a68 commit 1170323

File tree

8 files changed

+18
-11
lines changed

8 files changed

+18
-11
lines changed

readme.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,8 @@ Using Traits
314314
$class = new Nette\PhpGenerator\ClassType('Demo');
315315
$class->addTrait('SmartObject');
316316
$class->addTrait('MyTrait', true)
317-
->addResolution('sayHello as protected');
317+
->addResolution('sayHello as protected')
318+
->addComment('@use MyTrait<Foo>');
318319
echo $class;
319320
```
320321

@@ -324,6 +325,7 @@ Result:
324325
class Demo
325326
{
326327
use SmartObject;
328+
/** @use MyTrait<Foo> */
327329
use MyTrait {
328330
sayHello as protected;
329331
}

src/PhpGenerator/Extractor.php

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -291,14 +291,13 @@ private function addFunctionToFile(PhpFile $phpFile, Node\Stmt\Function_ $node):
291291

292292
private function addTraitToClass(ClassType $class, Node\Stmt\TraitUse $node): void
293293
{
294-
$res = [];
295-
foreach ($node->adaptations as $item) {
296-
$res[] = trim($this->toPhp($item), ';');
294+
foreach ($node->traits as $item) {
295+
$trait = $class->addTrait($item->toString(), true);
297296
}
298-
foreach ($node->traits as $trait) {
299-
$class->addTrait($trait->toString(), $res);
300-
$res = [];
297+
foreach ($node->adaptations as $item) {
298+
$trait->addResolution(trim($this->toPhp($item), ';'));
301299
}
300+
$this->addCommentAndAttributes($trait, $node);
302301
}
303302

304303

src/PhpGenerator/Printer.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,8 @@ public function printClass(ClassType $class, PhpNamespace $namespace = null): st
142142
$traits = [];
143143
foreach ($class->getTraitResolutions() as $trait) {
144144
$resolutions = $trait->getResolutions();
145-
$traits[] = 'use ' . $resolver($trait->getName())
145+
$traits[] = Helpers::formatDocComment((string) $trait->getComment())
146+
. 'use ' . $resolver($trait->getName())
146147
. ($resolutions ? " {\n" . $this->indentation . implode(";\n" . $this->indentation, $resolutions) . ";\n}\n" : ";\n");
147148
}
148149

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 Nette\SmartObject;
2121
use Traits\NameAware;
22+
use Traits\CommentAware;
2223

2324
/** @var array */
2425
private $resolutions = [];

tests/PhpGenerator/ClassType.phpt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,8 @@ $class->removeImplement('foo');
141141
$class
142142
->addTrait('ThirdTrait', true)
143143
->addResolution('a as private foo')
144-
->addResolution('b as private bar');
144+
->addResolution('b as private bar')
145+
->addComment('@use Foo');
145146

146147

147148
sameFile(__DIR__ . '/expected/ClassType.expect', (string) $class);

tests/PhpGenerator/expected/ClassType.expect

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ abstract class Example extends ParentClass implements IExample, IOne
1010
use AnotherTrait {
1111
sayHello as protected;
1212
}
13+
/** @use Foo */
1314
use ThirdTrait {
1415
a as private foo;
1516
b as private bar;

tests/PhpGenerator/expected/Factory.fromCode.traits.expect

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ class ParentClass
4343

4444
class Class1 extends ParentClass
4545
{
46+
/** @use Foo */
4647
use Trait2;
4748
}
4849

@@ -98,10 +99,10 @@ trait Trait1b
9899

99100
class Class5
100101
{
101-
use Trait1 {
102+
use Trait1;
103+
use Trait1b {
102104
\Trait1b::f1 insteadof \Trait1;
103105
// not yet supported
104106
\Trait1b::f1 as private;
105107
}
106-
use Trait1b;
107108
}

tests/PhpGenerator/fixtures/traits.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ public function f1()
4646

4747
class Class1 extends ParentClass
4848
{
49+
/** @use Foo */
4950
use Trait2;
5051
}
5152

0 commit comments

Comments
 (0)