Skip to content

Commit

Permalink
Printer: fixed use statements generation
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Nov 27, 2018
1 parent 9ecd301 commit 7c5e08e
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 3 deletions.
4 changes: 1 addition & 3 deletions src/PhpGenerator/Printer.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,7 @@ public function printNamespace(PhpNamespace $namespace): string

$uses = [];
foreach ($namespace->getUses() as $alias => $original) {
$useNamespace = Helpers::extractNamespace($original);

if ($name !== $useNamespace) {
if ($original !== ($name ? $name . '\\' . $alias : $alias)) {
if ($alias === $original || substr($original, -(strlen($alias) + 1)) === '\\' . $alias) {
$uses[] = "use $original;";
} else {
Expand Down
2 changes: 2 additions & 0 deletions tests/PhpGenerator/Printer.namespace.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ $printer = new Printer;

$namespace = new PhpNamespace('Foo');
$namespace->addUse('Bar\C');
$namespace->addUse('Foo\D');
$namespace->addUse('Foo\D', 'E');

$class = $namespace->addClass('A')
->setFinal(true)
Expand Down
1 change: 1 addition & 0 deletions tests/PhpGenerator/expected/Printer.namespace.expect
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
namespace Foo;

use Bar\C;
use Foo\D as E;

/**
* Description of class.
Expand Down

0 comments on commit 7c5e08e

Please sign in to comment.