@@ -338,6 +338,9 @@ public function getTraits(): array
338338
339339 public function addTrait (string $ name , array |bool |null $ deprecatedParam = null ): TraitUse
340340 {
341+ if (isset ($ this ->traits [$ name ])) {
342+ throw new Nette \InvalidStateException ("Cannot add trait ' $ name', because it already exists. " );
343+ }
341344 $ this ->traits [$ name ] = $ trait = new TraitUse ($ name , $ this );
342345 if (is_array ($ deprecatedParam )) {
343346 array_map (fn ($ item ) => $ trait ->addResolution ($ item ), $ deprecatedParam );
@@ -356,13 +359,18 @@ public function removeTrait(string $name): static
356359
357360 public function addMember (Method |Property |Constant |EnumCase |TraitUse $ member ): static
358361 {
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 ],
365369 };
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 ;
366374 return $ this ;
367375 }
368376
@@ -395,6 +403,9 @@ public function getConstants(): array
395403
396404 public function addConstant (string $ name , $ value ): Constant
397405 {
406+ if (isset ($ this ->consts [$ name ])) {
407+ throw new Nette \InvalidStateException ("Cannot add constant ' $ name', because it already exists. " );
408+ }
398409 return $ this ->consts [$ name ] = (new Constant ($ name ))
399410 ->setValue ($ value )
400411 ->setPublic ();
@@ -434,6 +445,9 @@ public function getCases(): array
434445 /** Adds case to enum */
435446 public function addCase (string $ name , string |int |null $ value = null ): EnumCase
436447 {
448+ if (isset ($ this ->cases [$ name ])) {
449+ throw new Nette \InvalidStateException ("Cannot add cases ' $ name', because it already exists. " );
450+ }
437451 return $ this ->cases [$ name ] = (new EnumCase ($ name ))
438452 ->setValue ($ value );
439453 }
@@ -483,6 +497,9 @@ public function getProperty(string $name): Property
483497 */
484498 public function addProperty (string $ name , $ value = null ): Property
485499 {
500+ if (isset ($ this ->properties [$ name ])) {
501+ throw new Nette \InvalidStateException ("Cannot add property ' $ name', because it already exists. " );
502+ }
486503 return $ this ->properties [$ name ] = func_num_args () > 1
487504 ? (new Property ($ name ))->setValue ($ value )
488505 : new Property ($ name );
@@ -545,12 +562,16 @@ public function getMethod(string $name): Method
545562
546563 public function addMethod (string $ name ): Method
547564 {
565+ $ lower = strtolower ($ name );
566+ if (isset ($ this ->methods [$ lower ])) {
567+ throw new Nette \InvalidStateException ("Cannot add method ' $ name', because it already exists. " );
568+ }
548569 $ method = new Method ($ name );
549570 if (!$ this ->isInterface ()) {
550571 $ method ->setPublic ();
551572 }
552573
553- return $ this ->methods [strtolower ( $ name ) ] = $ method ;
574+ return $ this ->methods [$ lower ] = $ method ;
554575 }
555576
556577
0 commit comments