Skip to content

Commit 1270e12

Browse files
committed
ClassType::addTrait() returns TraitUse instead of ClassType (BC break)
1 parent af00991 commit 1270e12

File tree

9 files changed

+48
-23
lines changed

9 files changed

+48
-23
lines changed

readme.md

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

src/PhpGenerator/ClassType.php

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -343,15 +343,14 @@ public function getTraitResolutions(): array
343343
}
344344

345345

346-
public function addTrait(string $name, array|bool $resolutions = []): static|TraitUse
346+
public function addTrait(string $name, array|bool|null $deprecatedParam = null): TraitUse
347347
{
348-
$this->traits[$name] = $trait = new TraitUse($name);
349-
if ($resolutions === true) {
350-
return $trait;
348+
$this->traits[$name] = $trait = new TraitUse($name, $this);
349+
if (is_array($deprecatedParam)) {
350+
array_map(fn($item) => $trait->addResolution($item), $deprecatedParam);
351351
}
352352

353-
array_map(fn($item) => $trait->addResolution($item), $resolutions);
354-
return $this;
353+
return $trait;
355354
}
356355

357356

src/PhpGenerator/TraitUse.php

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,25 @@
1717
*/
1818
final class TraitUse
1919
{
20-
use Nette\SmartObject;
20+
use Nette\SmartObject {
21+
__call as private parentCall;
22+
}
2123
use Traits\NameAware;
2224
use Traits\CommentAware;
2325

2426
private array $resolutions = [];
2527

28+
private ?ClassType $parent;
29+
2630

27-
public function __construct(string $name)
31+
public function __construct(string $name, ?ClassType $parent = null)
2832
{
2933
if (!Nette\PhpGenerator\Helpers::isNamespaceIdentifier($name, true)) {
3034
throw new Nette\InvalidArgumentException("Value '$name' is not valid trait name.");
3135
}
3236

3337
$this->name = $name;
38+
$this->parent = $parent;
3439
}
3540

3641

@@ -45,4 +50,18 @@ public function getResolutions(): array
4550
{
4651
return $this->resolutions;
4752
}
53+
54+
55+
public function __call(string $nm, array $args): mixed
56+
{
57+
if (!$this->parent) {
58+
return $this->parentCall($nm, $args);
59+
}
60+
$trace = debug_backtrace(0);
61+
$loc = isset($trace[0]['file'])
62+
? ' in ' . $trace[0]['file'] . ':' . $trace[0]['line']
63+
: '';
64+
trigger_error('The ClassType::addTrait() method now returns a TraitUse object instead of ClassType. Please fix the method chaining' . $loc, E_USER_DEPRECATED);
65+
return $this->parent->$nm(...$args);
66+
}
4867
}

tests/PhpGenerator/ClassType.phpt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ $class
3535
->addComment('@property-read Nette\Forms\Form $form');
3636

3737
$class->addTrait('ObjectTrait');
38-
$class->addTrait('AnotherTrait', ['sayHello as protected']);
38+
$class->addTrait('AnotherTrait')
39+
->addResolution('sayHello as protected');
3940

4041
$class->addConstant('ROLE', 'admin');
4142
$class->addConstant('ACTIVE', false)
@@ -141,7 +142,7 @@ $class->addImplement('foo');
141142
$class->removeImplement('foo');
142143

143144
$class
144-
->addTrait('ThirdTrait', true)
145+
->addTrait('ThirdTrait')
145146
->addResolution('a as private foo')
146147
->addResolution('b as private bar')
147148
->addComment('@use Foo');

tests/PhpGenerator/PhpFile.phpt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,10 @@ Assert::same($namespaceFoo, $traitC->getNamespace());
3232

3333
$classA
3434
->addImplement('Foo\A')
35-
->addTrait('Foo\C')
36-
->addImplement('Bar\C')
37-
->addTrait('Bar\D');
35+
->addImplement('Bar\C');
36+
37+
$classA->addTrait('Foo\C');
38+
$classA->addTrait('Bar\D');
3839

3940

4041
$namespaceBar = $file->addNamespace('Bar');

tests/PhpGenerator/PhpNamespace.fqn.phpt

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,10 @@ $class = new ClassType('Example');
1818
$class
1919
->setExtends('\ParentClass')
2020
->addImplement('One')
21-
->addImplement('\Two')
22-
->addTrait('Three')
23-
->addTrait('\Four');
21+
->addImplement('\Two');
22+
23+
$class->addTrait('Three');
24+
$class->addTrait('\Four');
2425

2526
$class->addMethod('one')
2627
->setReturnType('One');
@@ -45,9 +46,10 @@ $class = new ClassType('Example', new PhpNamespace(''));
4546
$class
4647
->setExtends('\ParentClass')
4748
->addImplement('One')
48-
->addImplement('\Two')
49-
->addTrait('Three')
50-
->addTrait('\Four');
49+
->addImplement('\Two');
50+
51+
$class->addTrait('Three');
52+
$class->addTrait('\Four');
5153

5254
$class->addMethod('one')
5355
->setReturnType('One');

tests/PhpGenerator/PhpNamespace.print.phpt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,10 @@ $interfaceB = $namespace->addInterface('B');
2020
$classA
2121
->addImplement('Foo\A')
2222
->addImplement('Bar\C')
23-
->addTrait('Bar\D')
2423
->addAttribute('Foo\A');
2524

25+
$classA->addTrait('Bar\D');
26+
2627
$method = $classA->addMethod('test');
2728
$method->addAttribute('Foo\A');
2829
$method->setReturnType('static|Foo\A');

tests/PhpGenerator/Printer.phpt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ $class = (new ClassType('Example'))
2121
->addComment("Description of class.\nThis is example\n");
2222

2323
$class->addTrait('ObjectTrait');
24-
$class->addTrait('AnotherTrait', ['sayHello as protected']);
24+
$class->addTrait('AnotherTrait')
25+
->addResolution('sayHello as protected');
2526

2627
$class->addConstant('FORCE_ARRAY', new Literal('Nette\Utils\Json::FORCE_ARRAY'))
2728
->setPrivate()

tests/PhpGenerator/PsrPrinter.phpt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ $class = (new ClassType('Example'))
2020
->addComment("Description of class.\nThis is example\n");
2121

2222
$class->addTrait('ObjectTrait');
23-
$class->addTrait('AnotherTrait', ['sayHello as protected']);
23+
$class->addTrait('AnotherTrait')
24+
->addResolution('sayHello as protected');
2425

2526
$class->addConstant('FORCE_ARRAY', new Literal('Nette\Utils\Json::FORCE_ARRAY'))
2627
->setVisibility('private')

0 commit comments

Comments
 (0)