Skip to content

Commit 08451e0

Browse files
committed
PhpNamespace: global namespace represents '' instead of NULL (BC break)
1 parent c182b96 commit 08451e0

File tree

4 files changed

+12
-7
lines changed

4 files changed

+12
-7
lines changed

src/PhpGenerator/PhpFile.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public function addTrait($name)
6767

6868

6969
/**
70-
* @param string NULL means global namespace
70+
* @param string
7171
* @return PhpNamespace
7272
*/
7373
public function addNamespace($name)
@@ -85,7 +85,7 @@ public function addNamespace($name)
8585
public function __toString()
8686
{
8787
foreach ($this->namespaces as $namespace) {
88-
$namespace->setBracketedSyntax(count($this->namespaces) > 1 && isset($this->namespaces[NULL]));
88+
$namespace->setBracketedSyntax(count($this->namespaces) > 1 && isset($this->namespaces['']));
8989
}
9090

9191
return Strings::normalize(

src/PhpGenerator/PhpNamespace.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,11 @@ class PhpNamespace
4545

4646

4747
/**
48-
* @param string|NULL
48+
* @param string
4949
*/
5050
public function __construct($name)
5151
{
52-
if ($name && !Helpers::isNamespace($name)) {
52+
if ($name !== '' && !Helpers::isNamespace($name)) {
5353
throw new Nette\InvalidArgumentException("Value '$name' is not valid name.");
5454
}
5555
$this->name = (string) $name;
@@ -66,11 +66,11 @@ public function setName($name)
6666

6767

6868
/**
69-
* @return string|NULL
69+
* @return string
7070
*/
7171
public function getName()
7272
{
73-
return $this->name ?: NULL;
73+
return $this->name;
7474
}
7575

7676

tests/PhpGenerator/PhpNamespace.phpt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ require __DIR__ . '/../bootstrap.php';
1414

1515
$namespace = new PhpNamespace('');
1616

17+
Assert::same('', $namespace->getName());
1718
Assert::same('A', $namespace->unresolveName('A'));
1819
Assert::same('foo\A', $namespace->unresolveName('foo\A'));
1920

@@ -30,6 +31,7 @@ foreach (['String', 'string', 'int', 'float', 'bool', 'array', 'callable', 'self
3031

3132
$namespace = new PhpNamespace('Foo');
3233

34+
Assert::same('Foo', $namespace->getName());
3335
Assert::same('\A', $namespace->unresolveName('\A'));
3436
Assert::same('\A', $namespace->unresolveName('A'));
3537
Assert::same('A', $namespace->unresolveName('foo\A'));

tests/PhpGenerator/invalidNames.phpt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,13 @@ require __DIR__ . '/../bootstrap.php';
1313

1414
Assert::noError(function () {
1515
new Nette\PhpGenerator\PhpNamespace(''); // global namespace
16-
new Nette\PhpGenerator\PhpNamespace(NULL); // global namespace for back compatibility
1716
new Nette\PhpGenerator\PhpNamespace('Iñtërnâti\ônàlizætiøn');
1817
});
1918

19+
Assert::exception(function () {
20+
new Nette\PhpGenerator\PhpNamespace(NULL);
21+
}, Nette\InvalidArgumentException::class);
22+
2023
Assert::exception(function () {
2124
new Nette\PhpGenerator\PhpNamespace('*');
2225
}, Nette\InvalidArgumentException::class);

0 commit comments

Comments
 (0)