Skip to content

Commit ebbc88e

Browse files
committed
PhpNamespace::addUse() fixed generating of alias for classes in current namespace
1 parent 2e96bb6 commit ebbc88e

File tree

3 files changed

+9
-1
lines changed

3 files changed

+9
-1
lines changed

src/PhpGenerator/Helpers.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ public static function extractNamespace($fqn)
214214
*/
215215
public static function extractShortName($fqn)
216216
{
217-
return ($pos = strrpos($fqn, '\\')) ? substr($fqn, $pos + 1) : $fqn;
217+
return ($pos = strrpos($fqn, '\\')) === FALSE ? $fqn : substr($fqn, $pos + 1);
218218
}
219219

220220
}

src/PhpGenerator/PhpNamespace.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,9 @@ public function getUses()
103103
public function addUse($fqn, $alias = NULL, &$aliasOut = NULL)
104104
{
105105
$fqn = ltrim($fqn, '\\');
106+
if ($alias === NULL && $this->name === Helpers::extractNamespace($fqn)) {
107+
$alias = Helpers::extractShortName($fqn);
108+
}
106109
if ($alias === NULL) {
107110
$path = explode('\\', $fqn);
108111
$counter = NULL;

tests/PhpGenerator/PhpNamespace.phpt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ Assert::same($namespace, $classA->getNamespace());
2828
$interfaceB = $namespace->addInterface('B');
2929
Assert::same($namespace, $interfaceB->getNamespace());
3030

31+
Assert::exception(function() use ($namespace) {
32+
$traitC = $namespace->addTrait('C');
33+
Assert::same($namespace, $traitC->getNamespace());
34+
}, 'Nette\InvalidStateException', "Alias 'C' used already for 'Bar\C', cannot use for 'Foo\C'.");
35+
3136
$classA
3237
->addImplement('Foo\\A')
3338
->addImplement('Bar\\C')

0 commit comments

Comments
 (0)