Skip to content

Commit 1cc5a68

Browse files
committed
added TraitUse, represents use-statement
1 parent 7971f45 commit 1cc5a68

File tree

8 files changed

+94
-21
lines changed

8 files changed

+94
-21
lines changed

readme.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ $class
5353
->setFinal()
5454
->setExtends(ParentClass::class)
5555
->addImplement(Countable::class)
56-
->addTrait(Nette\SmartObject::class)
5756
->addComment("Description of class.\nSecond line\n")
5857
->addComment('@property-read Nette\Forms\Form $form');
5958

@@ -72,7 +71,6 @@ It will render this result:
7271
*/
7372
final class Demo extends ParentClass implements Countable
7473
{
75-
use Nette\SmartObject;
7674
}
7775
```
7876

@@ -315,7 +313,8 @@ Using Traits
315313
```php
316314
$class = new Nette\PhpGenerator\ClassType('Demo');
317315
$class->addTrait('SmartObject');
318-
$class->addTrait('MyTrait', ['sayHello as protected']);
316+
$class->addTrait('MyTrait', true)
317+
->addResolution('sayHello as protected');
319318
echo $class;
320319
```
321320

src/PhpGenerator/ClassType.php

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ final class ClassType
5656
/** @var string[] */
5757
private $implements = [];
5858

59-
/** @var array[] */
59+
/** @var TraitUse[] */
6060
private $traits = [];
6161

6262
/** @var Constant[] name => Constant */
@@ -330,13 +330,18 @@ public function removeImplement(string $name): self
330330

331331

332332
/**
333-
* @param string[] $names
333+
* @param string[]|TraitUse[] $traits
334334
* @return static
335335
*/
336-
public function setTraits(array $names): self
336+
public function setTraits(array $traits): self
337337
{
338-
$this->validateNames($names);
339-
$this->traits = array_fill_keys($names, []);
338+
$this->traits = [];
339+
foreach ($traits as $trait) {
340+
if (!$trait instanceof TraitUse) {
341+
$trait = new TraitUse($trait);
342+
}
343+
$this->traits[$trait->getName()] = $trait;
344+
}
340345
return $this;
341346
}
342347

@@ -355,11 +360,19 @@ public function getTraitResolutions(): array
355360
}
356361

357362

358-
/** @return static */
359-
public function addTrait(string $name, array $resolutions = []): self
363+
/**
364+
* @param array|bool $resolutions
365+
* @return static|TraitUse
366+
*/
367+
public function addTrait(string $name, $resolutions = [])
360368
{
361-
$this->validateNames([$name]);
362-
$this->traits[$name] = $resolutions;
369+
$this->traits[$name] = $trait = new TraitUse($name);
370+
if ($resolutions === true) {
371+
return $trait;
372+
}
373+
array_map(function ($item) use ($trait) {
374+
$trait->addResolution($item);
375+
}, $resolutions);
363376
return $this;
364377
}
365378

@@ -373,7 +386,7 @@ public function removeTrait(string $name): self
373386

374387

375388
/**
376-
* @param Method|Property|Constant|EnumCase $member
389+
* @param Method|Property|Constant|EnumCase|TraitUse $member
377390
* @return static
378391
*/
379392
public function addMember($member): self
@@ -393,8 +406,11 @@ public function addMember($member): self
393406
} elseif ($member instanceof EnumCase) {
394407
$this->cases[$member->getName()] = $member;
395408

409+
} elseif ($member instanceof TraitUse) {
410+
$this->traits[$member->getName()] = $member;
411+
396412
} else {
397-
throw new Nette\InvalidArgumentException('Argument must be Method|Property|Constant|EnumCase.');
413+
throw new Nette\InvalidArgumentException('Argument must be Method|Property|Constant|EnumCase|TraitUse.');
398414
}
399415

400416
return $this;

src/PhpGenerator/Printer.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,9 @@ public function printClass(ClassType $class, PhpNamespace $namespace = null): st
140140
: function ($s) { return $s; };
141141

142142
$traits = [];
143-
foreach ($class->getTraitResolutions() as $trait => $resolutions) {
144-
$traits[] = 'use ' . $resolver($trait)
143+
foreach ($class->getTraitResolutions() as $trait) {
144+
$resolutions = $trait->getResolutions();
145+
$traits[] = 'use ' . $resolver($trait->getName())
145146
. ($resolutions ? " {\n" . $this->indentation . implode(";\n" . $this->indentation, $resolutions) . ";\n}\n" : ";\n");
146147
}
147148

src/PhpGenerator/TraitUse.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
/**
4+
* This file is part of the Nette Framework (https://nette.org)
5+
* Copyright (c) 2004 David Grudl (https://davidgrudl.com)
6+
*/
7+
8+
declare(strict_types=1);
9+
10+
namespace Nette\PhpGenerator;
11+
12+
use Nette;
13+
14+
15+
/**
16+
* use Trait
17+
*/
18+
final class TraitUse
19+
{
20+
use Nette\SmartObject;
21+
use Traits\NameAware;
22+
23+
/** @var array */
24+
private $resolutions = [];
25+
26+
27+
public function __construct(string $name)
28+
{
29+
if (!Nette\PhpGenerator\Helpers::isNamespaceIdentifier($name, true)) {
30+
throw new Nette\InvalidArgumentException("Value '$name' is not valid trait name.");
31+
}
32+
$this->name = $name;
33+
}
34+
35+
36+
public function addResolution(string $resolution): self
37+
{
38+
$this->resolutions[] = $resolution;
39+
return $this;
40+
}
41+
42+
43+
public function getResolutions(): array
44+
{
45+
return $this->resolutions;
46+
}
47+
}

tests/PhpGenerator/ClassType.addMember.phpt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,19 @@ require __DIR__ . '/../bootstrap.php';
1212
Assert::exception(function () {
1313
(new ClassType('Example'))
1414
->addMember(new stdClass);
15-
}, Nette\InvalidArgumentException::class, 'Argument must be Method|Property|Constant|EnumCase.');
15+
}, Nette\InvalidArgumentException::class, 'Argument must be Method|Property|Constant|EnumCase|TraitUse.');
1616

1717

1818
$class = (new ClassType('Example'))
1919
->addMember($method = new Nette\PhpGenerator\Method('getHandle'))
2020
->addMember($property = new Nette\PhpGenerator\Property('handle'))
21-
->addMember($const = new Nette\PhpGenerator\Constant('ROLE'));
21+
->addMember($const = new Nette\PhpGenerator\Constant('ROLE'))
22+
->addMember($trait = new Nette\PhpGenerator\TraitUse('Foo\Bar'));
2223

2324
Assert::same(['getHandle' => $method], $class->getMethods());
2425
Assert::same(['handle' => $property], $class->getProperties());
2526
Assert::same(['ROLE' => $const], $class->getConstants());
27+
Assert::same(['Foo\Bar'], $class->getTraits());
2628
Assert::same('', $method->getBody());
2729

2830

tests/PhpGenerator/ClassType.phpt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ Assert::false($class->isFinal());
4343
Assert::true($class->isAbstract());
4444
Assert::same('ParentClass', $class->getExtends());
4545
Assert::same(['ObjectTrait', 'AnotherTrait'], $class->getTraits());
46-
Assert::same(['ObjectTrait' => [], 'AnotherTrait' => ['sayHello as protected']], $class->getTraitResolutions());
4746
Assert::count(2, $class->getConstants());
4847
Assert::type(Nette\PhpGenerator\Constant::class, $class->getConstants()['ROLE']);
4948

@@ -139,6 +138,11 @@ $class->removeTrait('foo');
139138
$class->addImplement('foo');
140139
$class->removeImplement('foo');
141140

141+
$class
142+
->addTrait('ThirdTrait', true)
143+
->addResolution('a as private foo')
144+
->addResolution('b as private bar');
145+
142146

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

tests/PhpGenerator/expected/ClassType.expect

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ abstract class Example extends ParentClass implements IExample, IOne
1010
use AnotherTrait {
1111
sayHello as protected;
1212
}
13+
use ThirdTrait {
14+
a as private foo;
15+
b as private bar;
16+
}
1317

1418
const ROLE = 'admin';
1519
final public const ACTIVE = false;

tests/PhpGenerator/invalidNames.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,11 +110,11 @@ Assert::exception(function () use ($class) {
110110

111111
Assert::exception(function () use ($class) {
112112
$class->setTraits(['A', '*']);
113-
}, Nette\InvalidArgumentException::class, "Value '*' is not valid class name.");
113+
}, Nette\InvalidArgumentException::class, "Value '*' is not valid trait name.");
114114

115115
Assert::exception(function () use ($class) {
116116
$class->addTrait('*');
117-
}, Nette\InvalidArgumentException::class, "Value '*' is not valid class name.");
117+
}, Nette\InvalidArgumentException::class, "Value '*' is not valid trait name.");
118118

119119

120120
Assert::noError(function () {

0 commit comments

Comments
 (0)