Skip to content

Commit 0d03544

Browse files
committed
ClassType::setConstants() and setTraits() accepts array of Constant resp TraitUse only (BC break)
1 parent 416a873 commit 0d03544

File tree

7 files changed

+25
-22
lines changed

7 files changed

+25
-22
lines changed

src/PhpGenerator/ClassType.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -307,14 +307,15 @@ public function removeImplement(string $name): static
307307

308308

309309
/**
310-
* @param string[]|TraitUse[] $traits
310+
* @param TraitUse[] $traits
311311
*/
312312
public function setTraits(array $traits): static
313313
{
314314
(function (TraitUse|string ...$traits) {})(...$traits);
315315
$this->traits = [];
316316
foreach ($traits as $trait) {
317317
if (!$trait instanceof TraitUse) {
318+
trigger_error(__METHOD__ . '() accepts an array of TraitUse as parameter, string given.', E_USER_DEPRECATED);
318319
$trait = new TraitUse($trait);
319320
}
320321

@@ -372,13 +373,14 @@ public function addMember(Method|Property|Constant|EnumCase|TraitUse $member): s
372373

373374

374375
/**
375-
* @param Constant[]|mixed[] $consts
376+
* @param Constant[] $consts
376377
*/
377378
public function setConstants(array $consts): static
378379
{
379380
$this->consts = [];
380381
foreach ($consts as $k => $const) {
381382
if (!$const instanceof Constant) {
383+
trigger_error(__METHOD__ . '() accepts an array of Constant as parameter, ' . get_debug_type($const) . ' given.', E_USER_DEPRECATED);
382384
$const = (new Constant($k))->setValue($const)->setPublic();
383385
}
384386

tests/PhpGenerator/ClassType.enum.phpt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@ $enum = ClassType::enum('Suit');
1717

1818
Assert::true($enum->isEnum());
1919

20-
$enum
21-
->setTraits(['ObjectTrait'])
22-
->addComment("Description of class.\nThis is example\n")
23-
->addAttribute('ExampleAttribute')
24-
->addConstant('ACTIVE', false);
20+
$enum->addComment("Description of class.\nThis is example\n")
21+
->addAttribute('ExampleAttribute');
22+
23+
$enum->addConstant('ACTIVE', false);
24+
$enum->addTrait('ObjectTrait');
2525

2626
$enum->addMethod('foo')
2727
->setBody('return 10;');

tests/PhpGenerator/ClassType.phpt

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,15 @@ $class
3131
->setExtends('ParentClass')
3232
->addImplement('IExample')
3333
->addImplement('IOne')
34-
->setTraits(['ObjectTrait'])
35-
->addTrait('AnotherTrait', ['sayHello as protected'])
3634
->addComment("Description of class.\nThis is example\n /**/")
37-
->addComment('@property-read Nette\Forms\Form $form')
38-
->setConstants(['ROLE' => 'admin'])
39-
->addConstant('ACTIVE', false)
40-
->setFinal();
35+
->addComment('@property-read Nette\Forms\Form $form');
36+
37+
$class->addTrait('ObjectTrait');
38+
$class->addTrait('AnotherTrait', ['sayHello as protected']);
39+
40+
$class->addConstant('ROLE', 'admin');
41+
$class->addConstant('ACTIVE', false)
42+
->setFinal();
4143

4244
Assert::false($class->isFinal());
4345
Assert::true($class->isAbstract());

tests/PhpGenerator/Printer.namespace.phpt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,10 @@ $class = $namespace->addClass('A')
2121
->setExtends('ParentClass')
2222
->addImplement('IExample')
2323
->addImplement('Foo\IOne')
24-
->setTraits(['Foo\ObjectTrait'])
2524
->addComment("Description of class.\nThis is example\n");
2625

26+
$class->addTrait('Foo\ObjectTrait');
27+
2728
$class->addMethod('first')
2829
->addComment('@return resource')
2930
->setFinal(true)

tests/PhpGenerator/Printer.phpt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,11 @@ $class = (new ClassType('Example'))
1818
->setFinal(true)
1919
->setExtends('ParentClass')
2020
->addImplement('IExample')
21-
->setTraits(['ObjectTrait'])
22-
->addTrait('AnotherTrait', ['sayHello as protected'])
2321
->addComment("Description of class.\nThis is example\n");
2422

23+
$class->addTrait('ObjectTrait');
24+
$class->addTrait('AnotherTrait', ['sayHello as protected']);
25+
2526
$class->addConstant('FORCE_ARRAY', new Literal('Nette\Utils\Json::FORCE_ARRAY'))
2627
->setPrivate()
2728
->addComment('Commented');

tests/PhpGenerator/PsrPrinter.phpt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,11 @@ $class = (new ClassType('Example'))
1717
->setFinal(true)
1818
->setExtends('ParentClass')
1919
->addImplement('IExample')
20-
->setTraits(['ObjectTrait'])
21-
->addTrait('AnotherTrait', ['sayHello as protected'])
2220
->addComment("Description of class.\nThis is example\n");
2321

22+
$class->addTrait('ObjectTrait');
23+
$class->addTrait('AnotherTrait', ['sayHello as protected']);
24+
2425
$class->addConstant('FORCE_ARRAY', new Literal('Nette\Utils\Json::FORCE_ARRAY'))
2526
->setVisibility('private')
2627
->addComment('Commented');

tests/PhpGenerator/invalidNames.phpt

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -108,10 +108,6 @@ Assert::exception(function () use ($class) {
108108
$class->addImplement('*');
109109
}, Nette\InvalidArgumentException::class, "Value '*' is not valid class name.");
110110

111-
Assert::exception(function () use ($class) {
112-
$class->setTraits(['A', '*']);
113-
}, Nette\InvalidArgumentException::class, "Value '*' is not valid trait name.");
114-
115111
Assert::exception(function () use ($class) {
116112
$class->addTrait('*');
117113
}, Nette\InvalidArgumentException::class, "Value '*' is not valid trait name.");

0 commit comments

Comments
 (0)