Skip to content

Commit d3e893b

Browse files
committed
PhpNamespace::add() accepts GlobalFunction
1 parent 162f27e commit d3e893b

File tree

2 files changed

+29
-21
lines changed

2 files changed

+29
-21
lines changed

src/PhpGenerator/PhpNamespace.php

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -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

tests/PhpGenerator/PhpNamespace.add.phpt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
declare(strict_types=1);
44

55
use Nette\PhpGenerator\ClassType;
6+
use Nette\PhpGenerator\GlobalFunction;
67
use Nette\PhpGenerator\PhpNamespace;
78
use Tester\Assert;
89
require __DIR__ . '/../bootstrap.php';
@@ -16,7 +17,8 @@ testException('adding class without name throws exception', function () {
1617
test('adding classes preserves their original namespaces', function () {
1718
$namespace = (new PhpNamespace('Foo'))
1819
->add($classA = new ClassType('A'))
19-
->add($classB = new ClassType('B', new PhpNamespace('X')));
20+
->add($classB = new ClassType('B', new PhpNamespace('X')))
21+
->add(new GlobalFunction('myFunc'));
2022

2123
same(
2224
<<<'XX'
@@ -30,6 +32,10 @@ test('adding classes preserves their original namespaces', function () {
3032
{
3133
}
3234
35+
function myFunc()
36+
{
37+
}
38+
3339
XX,
3440
(string) $namespace,
3541
);

0 commit comments

Comments
 (0)