@@ -338,6 +338,9 @@ public function getTraits(): array
338
338
339
339
public function addTrait (string $ name , array |bool |null $ deprecatedParam = null ): TraitUse
340
340
{
341
+ if (isset ($ this ->traits [$ name ])) {
342
+ throw new Nette \InvalidStateException ("Cannot add trait ' $ name', because it already exists. " );
343
+ }
341
344
$ this ->traits [$ name ] = $ trait = new TraitUse ($ name , $ this );
342
345
if (is_array ($ deprecatedParam )) {
343
346
array_map (fn ($ item ) => $ trait ->addResolution ($ item ), $ deprecatedParam );
@@ -356,13 +359,18 @@ public function removeTrait(string $name): static
356
359
357
360
public function addMember (Method |Property |Constant |EnumCase |TraitUse $ member ): static
358
361
{
359
- match (true ) {
360
- $ member instanceof Method => $ this ->methods [strtolower ($ member ->getName ())] = $ member ,
361
- $ member instanceof Property => $ this ->properties [$ member ->getName ()] = $ member ,
362
- $ member instanceof Constant => $ this ->consts [$ member ->getName ()] = $ member ,
363
- $ member instanceof EnumCase => $ this ->cases [$ member ->getName ()] = $ member ,
364
- $ member instanceof TraitUse => $ this ->traits [$ member ->getName ()] = $ member ,
362
+ $ name = $ member ->getName ();
363
+ [$ type , $ n ] = match (true ) {
364
+ $ member instanceof Method => ['methods ' , strtolower ($ name )],
365
+ $ member instanceof Property => ['properties ' , $ name ],
366
+ $ member instanceof Constant => ['consts ' , $ name ],
367
+ $ member instanceof EnumCase => ['cases ' , $ name ],
368
+ $ member instanceof TraitUse => ['traits ' , $ name ],
365
369
};
370
+ if (isset ($ this ->$ type [$ n ])) {
371
+ throw new Nette \InvalidStateException ("Cannot add member ' $ name', because it already exists. " );
372
+ }
373
+ $ this ->$ type [$ n ] = $ member ;
366
374
return $ this ;
367
375
}
368
376
@@ -395,6 +403,9 @@ public function getConstants(): array
395
403
396
404
public function addConstant (string $ name , $ value ): Constant
397
405
{
406
+ if (isset ($ this ->consts [$ name ])) {
407
+ throw new Nette \InvalidStateException ("Cannot add constant ' $ name', because it already exists. " );
408
+ }
398
409
return $ this ->consts [$ name ] = (new Constant ($ name ))
399
410
->setValue ($ value )
400
411
->setPublic ();
@@ -434,6 +445,9 @@ public function getCases(): array
434
445
/** Adds case to enum */
435
446
public function addCase (string $ name , string |int |null $ value = null ): EnumCase
436
447
{
448
+ if (isset ($ this ->cases [$ name ])) {
449
+ throw new Nette \InvalidStateException ("Cannot add cases ' $ name', because it already exists. " );
450
+ }
437
451
return $ this ->cases [$ name ] = (new EnumCase ($ name ))
438
452
->setValue ($ value );
439
453
}
@@ -483,6 +497,9 @@ public function getProperty(string $name): Property
483
497
*/
484
498
public function addProperty (string $ name , $ value = null ): Property
485
499
{
500
+ if (isset ($ this ->properties [$ name ])) {
501
+ throw new Nette \InvalidStateException ("Cannot add property ' $ name', because it already exists. " );
502
+ }
486
503
return $ this ->properties [$ name ] = func_num_args () > 1
487
504
? (new Property ($ name ))->setValue ($ value )
488
505
: new Property ($ name );
@@ -545,12 +562,16 @@ public function getMethod(string $name): Method
545
562
546
563
public function addMethod (string $ name ): Method
547
564
{
565
+ $ lower = strtolower ($ name );
566
+ if (isset ($ this ->methods [$ lower ])) {
567
+ throw new Nette \InvalidStateException ("Cannot add method ' $ name', because it already exists. " );
568
+ }
548
569
$ method = new Method ($ name );
549
570
if (!$ this ->isInterface ()) {
550
571
$ method ->setPublic ();
551
572
}
552
573
553
- return $ this ->methods [strtolower ( $ name ) ] = $ method ;
574
+ return $ this ->methods [$ lower ] = $ method ;
554
575
}
555
576
556
577
0 commit comments