Skip to content

Commit 7880996

Browse files
committed
PhpNamespace::unresolveName() prefers shorter names
1 parent 74ffdd5 commit 7880996

File tree

3 files changed

+10
-7
lines changed

3 files changed

+10
-7
lines changed

src/PhpGenerator/PhpNamespace.php

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,11 @@ public function unresolveName(string $name): string
141141
return $name;
142142
}
143143
$name = ltrim($name, '\\');
144-
$res = null;
145144
$lower = strtolower($name);
145+
$res = Strings::startsWith($lower, strtolower($this->name) . '\\')
146+
? substr($name, strlen($this->name) + 1)
147+
: null;
148+
146149
foreach ($this->uses as $alias => $original) {
147150
if (Strings::startsWith($lower . '\\', strtolower($original) . '\\')) {
148151
$short = $alias . substr($name, strlen($original));
@@ -152,11 +155,7 @@ public function unresolveName(string $name): string
152155
}
153156
}
154157

155-
if (!$res && Strings::startsWith($lower, strtolower($this->name) . '\\')) {
156-
return substr($name, strlen($this->name) + 1);
157-
} else {
158-
return $res ?: ($this->name ? '\\' : '') . $name;
159-
}
158+
return $res ?: ($this->name ? '\\' : '') . $name;
160159
}
161160

162161

tests/PhpGenerator/PhpNamespace.phpt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,11 @@ Assert::same('?A', $namespace->unresolveType('?foo\A'));
4242
Assert::same('A&\Countable', $namespace->unresolveType('foo\A&Countable'));
4343
Assert::same('', $namespace->unresolveType(''));
4444

45+
$namespace->addUse('Foo');
46+
Assert::same('B', $namespace->unresolveName('Foo\B'));
47+
4548
$namespace->addUse('Bar\C');
46-
Assert::same(['C' => 'Bar\C'], $namespace->getUses());
49+
Assert::same(['C' => 'Bar\C', 'Foo' => 'Foo'], $namespace->getUses());
4750

4851
Assert::same('\Bar', $namespace->unresolveName('Bar'));
4952
Assert::same('C', $namespace->unresolveName('\bar\C'));

tests/PhpGenerator/expected/PhpNamespace.expect

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
namespace Foo;
22

33
use Bar\C;
4+
use Foo;
45

56
#[A]
67
class A implements A, C

0 commit comments

Comments
 (0)