Skip to content

Commit 49f1dcd

Browse files
committed
PhpNamespace::add() accepts GlobalFunction
1 parent ca1dc6a commit 49f1dcd

2 files changed

Lines changed: 21 additions & 19 deletions

File tree

src/PhpGenerator/PhpNamespace.php

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

tests/PhpGenerator/PhpNamespace.add.phpt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php declare(strict_types=1);
22

33
use Nette\PhpGenerator\ClassType;
4+
use Nette\PhpGenerator\GlobalFunction;
45
use Nette\PhpGenerator\PhpNamespace;
56
use Tester\Assert;
67
require __DIR__ . '/../bootstrap.php';
@@ -14,7 +15,8 @@ testException('adding class without name throws exception', function () {
1415
test('adding classes preserves their original namespaces', function () {
1516
$namespace = (new PhpNamespace('Foo'))
1617
->add($classA = new ClassType('A'))
17-
->add($classB = new ClassType('B', new PhpNamespace('X')));
18+
->add($classB = new ClassType('B', new PhpNamespace('X')))
19+
->add(new GlobalFunction('myFunc'));
1820

1921
same(
2022
<<<'XX'
@@ -28,6 +30,10 @@ test('adding classes preserves their original namespaces', function () {
2830
{
2931
}
3032
33+
function myFunc()
34+
{
35+
}
36+
3137
XX,
3238
(string) $namespace,
3339
);

0 commit comments

Comments
 (0)