Skip to content

Commit

Permalink
PhpNamespace: better use-statements sorting behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Nov 2, 2022
1 parent b0f47e5 commit a8d6abe
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/PhpGenerator/PhpNamespace.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ public function addUseConstant(string $name, ?string $alias = null): static
/** @return string[] */
public function getUses(string $of = self::NameNormal): array
{
asort($this->aliases[$of]);
uasort($this->aliases[$of], fn(string $a, string $b): int => strtr($a, '\\', ' ') <=> strtr($b, '\\', ' '));
return array_filter(
$this->aliases[$of],
fn($name, $alias) => strcasecmp(($this->name ? $this->name . '\\' : '') . $alias, $name),
Expand Down
28 changes: 28 additions & 0 deletions tests/PhpGenerator/Printer.use-order.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

use Nette\PhpGenerator\PhpNamespace;
use Nette\PhpGenerator\Printer;
use Tester\Assert;

require __DIR__ . '/../bootstrap.php';


$printer = new Printer;
$namespace = new PhpNamespace('Foo');
$namespace->addUse('Example\Foo\EmailAlias\Bar');
$namespace->addUse('Example\Foo\Email\Test');
$namespace->addUse('Example\Foo\MyClass');

Assert::match(
<<<'XX'
namespace Foo;
use Example\Foo\Email\Test;
use Example\Foo\EmailAlias\Bar;
use Example\Foo\MyClass;

XX,
$printer->printNamespace($namespace),
);

0 comments on commit a8d6abe

Please sign in to comment.