Skip to content

Commit a8d6abe

Browse files
committed
PhpNamespace: better use-statements sorting behavior
1 parent b0f47e5 commit a8d6abe

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

src/PhpGenerator/PhpNamespace.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ public function addUseConstant(string $name, ?string $alias = null): static
162162
/** @return string[] */
163163
public function getUses(string $of = self::NameNormal): array
164164
{
165-
asort($this->aliases[$of]);
165+
uasort($this->aliases[$of], fn(string $a, string $b): int => strtr($a, '\\', ' ') <=> strtr($b, '\\', ' '));
166166
return array_filter(
167167
$this->aliases[$of],
168168
fn($name, $alias) => strcasecmp(($this->name ? $this->name . '\\' : '') . $alias, $name),
+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Nette\PhpGenerator\PhpNamespace;
6+
use Nette\PhpGenerator\Printer;
7+
use Tester\Assert;
8+
9+
require __DIR__ . '/../bootstrap.php';
10+
11+
12+
$printer = new Printer;
13+
$namespace = new PhpNamespace('Foo');
14+
$namespace->addUse('Example\Foo\EmailAlias\Bar');
15+
$namespace->addUse('Example\Foo\Email\Test');
16+
$namespace->addUse('Example\Foo\MyClass');
17+
18+
Assert::match(
19+
<<<'XX'
20+
namespace Foo;
21+
22+
use Example\Foo\Email\Test;
23+
use Example\Foo\EmailAlias\Bar;
24+
use Example\Foo\MyClass;
25+
26+
XX,
27+
$printer->printNamespace($namespace),
28+
);

0 commit comments

Comments
 (0)