Skip to content

Commit 1294f5d

Browse files
committed
Add trait support for class builder - Close #13
1 parent 4673690 commit 1294f5d

File tree

2 files changed

+46
-2
lines changed

2 files changed

+46
-2
lines changed

src/Builder/ClassBuilder.php

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use OpenCodeModeling\CodeAst\NodeVisitor\ClassFile;
1616
use OpenCodeModeling\CodeAst\NodeVisitor\ClassImplements;
1717
use OpenCodeModeling\CodeAst\NodeVisitor\ClassNamespace;
18+
use OpenCodeModeling\CodeAst\NodeVisitor\ClassUseTrait;
1819
use OpenCodeModeling\CodeAst\NodeVisitor\NamespaceUse;
1920
use OpenCodeModeling\CodeAst\NodeVisitor\StrictType;
2021
use PhpParser\Node;
@@ -50,6 +51,9 @@ final class ClassBuilder
5051
/** @var string[] */
5152
private $namespaceUse = [];
5253

54+
/** @var string[] */
55+
private $traits = [];
56+
5357
/** @var ClassConstBuilder[] */
5458
private $constants = [];
5559

@@ -125,6 +129,13 @@ public function setNamespaceUse(string ...$namespaces): self
125129
return $this;
126130
}
127131

132+
public function setUseTrait(string ...$traits): self
133+
{
134+
$this->traits = $traits;
135+
136+
return $this;
137+
}
138+
128139
public function setConstants(ClassConstBuilder ...$constants): self
129140
{
130141
$this->constants = $constants;
@@ -183,6 +194,14 @@ public function getNamespaceUse(): array
183194
return $this->namespaceUse;
184195
}
185196

197+
/**
198+
* @return string[]
199+
*/
200+
public function getUseTrait(): array
201+
{
202+
return $this->traits;
203+
}
204+
186205
/**
187206
* @return ClassConstBuilder[]
188207
*/
@@ -218,6 +237,10 @@ public function generate(): array
218237
if ($this->implements) {
219238
$visitors[] = new ClassImplements(...$this->implements);
220239
}
240+
if ($this->traits) {
241+
$visitors[] = new ClassUseTrait(...$this->traits);
242+
}
243+
221244
if (\count($this->constants) > 0) {
222245
\array_push(
223246
$visitors,
@@ -254,12 +277,19 @@ private function unpackNode(Node $node): void
254277
}
255278
break;
256279
case $node instanceof Node\Stmt\UseUse:
257-
$this->namespaceUse[] = $node->name->toString();
280+
$this->namespaceUse[] = $node->name instanceof Node\Name\FullyQualified
281+
? '\\' . $node->name->toString()
282+
: $node->name->toString();
258283
break;
259284
case $node instanceof Node\Stmt\Class_:
260285
$this->name = $node->name->name;
261286
$this->final = $node->isFinal();
262-
$this->extends = $node->extends ? $node->extends->toString() : null;
287+
288+
if ($node->extends !== null) {
289+
$this->extends = $node->extends instanceof Node\Name\FullyQualified
290+
? '\\' . $node->extends->toString()
291+
: $node->extends->toString();
292+
}
263293

264294
foreach ($node->stmts as $stmt) {
265295
$this->unpackNode($stmt);
@@ -273,6 +303,16 @@ static function (Node\Name $name) {
273303
$node->implements
274304
);
275305
break;
306+
case $node instanceof Node\Stmt\TraitUse:
307+
$this->traits = \array_map(
308+
static function (Node\Name $name) {
309+
return $name instanceof Node\Name\FullyQualified
310+
? '\\' . $name->toString()
311+
: $name->toString();
312+
},
313+
$node->traits
314+
);
315+
break;
276316
case $node instanceof Node\Stmt\ClassConst:
277317
$this->constants[] = ClassConstBuilder::fromNode($node);
278318
break;

tests/Builder/ClassBuilderTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ public function it_generates_class_for_empty_file(): void
4343
->setExtends('BaseClass')
4444
->setNamespaceUse('Foo\\Bar')
4545
->setImplements('\\Iterator', 'Bar')
46+
->setUseTrait('\\My\\TestTrait')
4647
->setConstants(ClassConstBuilder::fromScratch('PRIV', 'private')->setPrivate());
4748

4849
$nodeTraverser = new NodeTraverser();
@@ -57,6 +58,7 @@ public function it_generates_class_for_empty_file(): void
5758
use Foo\Bar;
5859
final class TestClass extends BaseClass implements \Iterator, Bar
5960
{
61+
use \My\TestTrait;
6062
private const PRIV = 'private';
6163
}
6264
EOF;
@@ -78,6 +80,7 @@ public function it_generates_class_for_empty_file_from_template(): void
7880
use Foo\Bar;
7981
final class TestClass extends BaseClass implements \Iterator, Bar
8082
{
83+
use \My\TestTrait;
8184
const FIRST = 1;
8285
private const PRIV = 'private';
8386
protected const PROT = 'protected';
@@ -94,6 +97,7 @@ final class TestClass extends BaseClass implements \Iterator, Bar
9497
$this->assertTrue($classFactory->isFinal());
9598
$this->assertTrue($classFactory->isStrict());
9699
$this->assertFalse($classFactory->isAbstract());
100+
$this->assertSame(['\\My\\TestTrait'], $classFactory->getUseTrait());
97101

98102
$nodeTraverser = new NodeTraverser();
99103
$classFactory->injectVisitors($nodeTraverser);

0 commit comments

Comments
 (0)