@@ -252,23 +252,25 @@ public function simplifyName(string $name, string $of = self::NameNormal): strin
252252
253253
254254 /**
255- * Adds a class-like type to the namespace. If it already exists, throws an exception.
255+ * Adds a class-like type or function to the namespace. If it already exists, throws an exception.
256256 */
257- public function add (ClassType |InterfaceType |TraitType |EnumType $ class ): static
257+ public function add (ClassType |InterfaceType |TraitType |EnumType | GlobalFunction $ item ): static
258258 {
259- $ name = $ class ->getName ();
260- if ($ name === null ) {
261- throw new Nette \InvalidArgumentException ('Class does not have a name. ' );
262- }
263-
259+ $ name = $ item ->getName () ?? throw new Nette \InvalidArgumentException ('Class does not have a name. ' );
264260 $ lower = strtolower ($ name );
265- if (isset ($ this ->classes [$ lower ]) && $ this ->classes [$ lower ] !== $ class ) {
261+ [$ list , $ type ] = $ item instanceof GlobalFunction ? [$ this ->functions , self ::NameFunction] : [$ this ->classes , self ::NameNormal];
262+ if (isset ($ list [$ lower ]) && $ list [$ lower ] !== $ item ) {
266263 throw new Nette \InvalidStateException ("Cannot add ' $ name', because it already exists. " );
267- } elseif ($ orig = array_change_key_case ($ this ->aliases [self ::NameNormal ])[$ lower ] ?? null ) {
264+ } elseif ($ orig = array_change_key_case ($ this ->aliases [$ type ])[$ lower ] ?? null ) {
268265 throw new Nette \InvalidStateException ("Name ' $ name' used already as alias for $ orig. " );
269266 }
270267
271- $ this ->classes [$ lower ] = $ class ;
268+ if ($ item instanceof GlobalFunction) {
269+ $ this ->functions [$ lower ] = $ item ;
270+ } else {
271+ $ this ->classes [$ lower ] = $ item ;
272+ }
273+
272274 return $ this ;
273275 }
274276
@@ -352,14 +354,8 @@ public function removeClass(string $name): static
352354 */
353355 public function addFunction (string $ name ): GlobalFunction
354356 {
355- $ lower = strtolower ($ name );
356- if (isset ($ this ->functions [$ lower ])) {
357- throw new Nette \InvalidStateException ("Cannot add ' $ name', because it already exists. " );
358- } elseif ($ orig = array_change_key_case ($ this ->aliases [self ::NameFunction])[$ lower ] ?? null ) {
359- throw new Nette \InvalidStateException ("Name ' $ name' used already as alias for $ orig. " );
360- }
361-
362- return $ this ->functions [$ lower ] = new GlobalFunction ($ name );
357+ $ this ->add ($ function = new GlobalFunction ($ name ));
358+ return $ function ;
363359 }
364360
365361
0 commit comments