@@ -254,23 +254,31 @@ public function simplifyName(string $name, string $of = self::NameNormal): strin
254254
255255
256256 /**
257- * Adds a class-like type to the namespace. If it already exists, throws an exception.
257+ * Adds a class-like type or function to the namespace. If it already exists, throws an exception.
258258 */
259- public function add (ClassType |InterfaceType |TraitType |EnumType $ class ): static
259+ public function add (ClassType |InterfaceType |TraitType |EnumType | GlobalFunction $ item ): static
260260 {
261- $ name = $ class ->getName ();
262- if ($ name === null ) {
263- throw new Nette \InvalidArgumentException ('Class does not have a name. ' );
264- }
265-
261+ $ name = $ item ->getName () ?? throw new Nette \InvalidArgumentException ('Class does not have a name. ' );
266262 $ lower = strtolower ($ name );
267- if (isset ($ this ->classes [$ lower ]) && $ this ->classes [$ lower ] !== $ class ) {
268- throw new Nette \InvalidStateException ("Cannot add ' $ name', because it already exists. " );
269- } elseif ($ orig = array_change_key_case ($ this ->aliases [self ::NameNormal])[$ lower ] ?? null ) {
270- throw new Nette \InvalidStateException ("Name ' $ name' used already as alias for $ orig. " );
263+
264+ if ($ item instanceof GlobalFunction) {
265+ if (isset ($ this ->functions [$ lower ]) && $ this ->functions [$ lower ] !== $ item ) {
266+ throw new Nette \InvalidStateException ("Cannot add ' $ name', because it already exists. " );
267+ } elseif ($ orig = array_change_key_case ($ this ->aliases [self ::NameFunction])[$ lower ] ?? null ) {
268+ throw new Nette \InvalidStateException ("Name ' $ name' used already as alias for $ orig. " );
269+ }
270+
271+ $ this ->functions [$ lower ] = $ item ;
272+ } else {
273+ if (isset ($ this ->classes [$ lower ]) && $ this ->classes [$ lower ] !== $ item ) {
274+ throw new Nette \InvalidStateException ("Cannot add ' $ name', because it already exists. " );
275+ } elseif ($ orig = array_change_key_case ($ this ->aliases [self ::NameNormal])[$ lower ] ?? null ) {
276+ throw new Nette \InvalidStateException ("Name ' $ name' used already as alias for $ orig. " );
277+ }
278+
279+ $ this ->classes [$ lower ] = $ item ;
271280 }
272281
273- $ this ->classes [$ lower ] = $ class ;
274282 return $ this ;
275283 }
276284
@@ -354,14 +362,8 @@ public function removeClass(string $name): static
354362 */
355363 public function addFunction (string $ name ): GlobalFunction
356364 {
357- $ lower = strtolower ($ name );
358- if (isset ($ this ->functions [$ lower ])) {
359- throw new Nette \InvalidStateException ("Cannot add ' $ name', because it already exists. " );
360- } elseif ($ orig = array_change_key_case ($ this ->aliases [self ::NameFunction])[$ lower ] ?? null ) {
361- throw new Nette \InvalidStateException ("Name ' $ name' used already as alias for $ orig. " );
362- }
363-
364- return $ this ->functions [$ lower ] = new GlobalFunction ($ name );
365+ $ this ->add ($ function = new GlobalFunction ($ name ));
366+ return $ function ;
365367 }
366368
367369
0 commit comments