Skip to content

Commit c48096c

Browse files
committed
PhpNamespace::add*() throws exception when class/function already exists (BC break)
1 parent 739de57 commit c48096c

File tree

4 files changed

+27
-3
lines changed

4 files changed

+27
-3
lines changed

readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -574,7 +574,7 @@ $class = new Nette\PhpGenerator\ClassType('Task');
574574
$namespace->add($class);
575575
```
576576

577-
If the class already exists, it will be overwritten.
577+
If the class already exists, it throws exception.
578578

579579
You can define use-statements:
580580

src/PhpGenerator/PhpNamespace.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,9 @@ public function add(ClassType $class): static
242242
}
243243

244244
$lower = strtolower($name);
245-
if ($orig = array_change_key_case($this->aliases[self::NAME_NORMAL])[$lower] ?? null) {
245+
if (isset($this->classes[$lower]) && $this->classes[$lower] !== $class) {
246+
throw new Nette\InvalidStateException("Cannot add '$name', because it already exists.");
247+
} elseif ($orig = array_change_key_case($this->aliases[self::NAME_NORMAL])[$lower] ?? null) {
246248
throw new Nette\InvalidStateException("Name '$name' used already as alias for $orig.");
247249
}
248250

@@ -286,7 +288,9 @@ public function removeClass(string $name): static
286288
public function addFunction(string $name): GlobalFunction
287289
{
288290
$lower = strtolower($name);
289-
if ($orig = array_change_key_case($this->aliases[self::NAME_FUNCTION])[$lower] ?? null) {
291+
if (isset($this->functions[$lower])) {
292+
throw new Nette\InvalidStateException("Cannot add '$name', because it already exists.");
293+
} elseif ($orig = array_change_key_case($this->aliases[self::NAME_FUNCTION])[$lower] ?? null) {
290294
throw new Nette\InvalidStateException("Name '$name' used already as alias for $orig.");
291295
}
292296

tests/PhpGenerator/PhpNamespace.add.phpt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,13 @@ class B
3333
// namespaces are not changed
3434
Assert::null($classA->getNamespace());
3535
Assert::same('X', $classB->getNamespace()->getName());
36+
37+
38+
// duplicity
39+
Assert::noError(function () use ($namespace, $classA) {
40+
$namespace->add($classA);
41+
});
42+
43+
Assert::exception(function () use ($namespace) {
44+
$namespace->add(new ClassType('a'));
45+
}, Nette\InvalidStateException::class, "Cannot add 'a', because it already exists.");

tests/PhpGenerator/PhpNamespace.phpt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ Assert::same('Foo', $namespace->getName());
1717
$classA = $namespace->addClass('A');
1818
Assert::same($namespace, $classA->getNamespace());
1919

20+
Assert::exception(function () use ($namespace) {
21+
$namespace->addClass('a');
22+
}, Nette\InvalidStateException::class, "Cannot add 'a', because it already exists.");
23+
2024
$interfaceB = $namespace->addInterface('B');
2125
Assert::same($namespace, $interfaceB->getNamespace());
2226

@@ -25,7 +29,13 @@ Assert::type(Nette\PhpGenerator\ClassType::class, $namespace->getClasses()['A'])
2529
$namespace->removeClass('a');
2630
Assert::count(1, $namespace->getClasses());
2731

32+
2833
$function = $namespace->addFunction('foo');
34+
35+
Assert::exception(function () use ($namespace) {
36+
$namespace->addFunction('Foo');
37+
}, Nette\InvalidStateException::class, "Cannot add 'Foo', because it already exists.");
38+
2939
Assert::count(1, $namespace->getFunctions());
3040
Assert::same($function, $namespace->getFunctions()['foo']);
3141
$namespace->removeFunction('FOO');

0 commit comments

Comments
 (0)