Skip to content

Commit 2e96bb6

Browse files
committed
added PhpNamespace::unresolveName()
1 parent 1287ec7 commit 2e96bb6

File tree

4 files changed

+90
-11
lines changed

4 files changed

+90
-11
lines changed

src/PhpGenerator/ClassType.php

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -195,20 +195,17 @@ public function __toString()
195195
}
196196

197197
$extends = $implements = $traits = array();
198-
199198
if ($this->namespace) {
200-
$fqnToAlias = array_flip($this->namespace->getUses());
201-
202-
foreach ((array) $this->extends as $fqn) {
203-
$extends[] = isset($fqnToAlias[$fqn]) ? $fqnToAlias[$fqn] : '\\' . $fqn;
199+
foreach ((array) $this->extends as $name) {
200+
$extends[] = $this->namespace->unresolveName($name);
204201
}
205202

206-
foreach ((array) $this->implements as $fqn) {
207-
$implements[] = isset($fqnToAlias[$fqn]) ? $fqnToAlias[$fqn] : '\\' . $fqn;
203+
foreach ((array) $this->implements as $name) {
204+
$implements[] = $this->namespace->unresolveName($name);
208205
}
209206

210-
foreach ((array) $this->traits as $fqn) {
211-
$traits[] = isset($fqnToAlias[$fqn]) ? $fqnToAlias[$fqn] : '\\' . $fqn;
207+
foreach ((array) $this->traits as $name) {
208+
$traits[] = $this->namespace->unresolveName($name);
212209
}
213210

214211
} else {

src/PhpGenerator/PhpNamespace.php

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class PhpNamespace extends Object
3737
private $classes = array();
3838

3939

40-
public function __construct($name)
40+
public function __construct($name = NULL)
4141
{
4242
$this->name = $name;
4343
}
@@ -102,7 +102,7 @@ public function getUses()
102102
*/
103103
public function addUse($fqn, $alias = NULL, &$aliasOut = NULL)
104104
{
105-
$fqn = trim($fqn, '\\');
105+
$fqn = ltrim($fqn, '\\');
106106
if ($alias === NULL) {
107107
$path = explode('\\', $fqn);
108108
$counter = NULL;
@@ -127,6 +127,32 @@ public function addUse($fqn, $alias = NULL, &$aliasOut = NULL)
127127
}
128128

129129

130+
/**
131+
* @param string
132+
* @return string
133+
*/
134+
public function unresolveName($name)
135+
{
136+
$name = ltrim($name, '\\');
137+
$res = NULL;
138+
$lower = strtolower($name);
139+
foreach ($this->uses as $alias => $for) {
140+
if (Strings::startsWith($lower . '\\', strtolower($for) . '\\')) {
141+
$short = $alias . substr($name, strlen($for));
142+
if (!isset($res) || strlen($res) > strlen($short)) {
143+
$res = $short;
144+
}
145+
}
146+
}
147+
148+
if (!$res && Strings::startsWith($lower, strtolower($this->name) . '\\')) {
149+
return substr($name, strlen($this->name) + 1);
150+
} else {
151+
return $res ?: '\\' . $name;
152+
}
153+
}
154+
155+
130156
/**
131157
* @return ClassType[]
132158
*/
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
namespace Foo;
2+
3+
use Bar\C;
4+
5+
class A implements A, C
6+
{
7+
8+
use \Bar\D;
9+
10+
11+
12+
}
13+
14+
interface B
15+
{
16+
17+
18+
19+
}

tests/PhpGenerator/PhpNamespace.phpt

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
/**
4+
* Test: Nette\PhpGenerator for files.
5+
*/
6+
7+
use Nette\PhpGenerator\PhpNamespace;
8+
use Tester\Assert;
9+
10+
require __DIR__ . '/../bootstrap.php';
11+
12+
13+
$namespace = new PhpNamespace('Foo');
14+
15+
Assert::same('\A', $namespace->unresolveName('A'));
16+
Assert::same('A', $namespace->unresolveName('foo\A'));
17+
18+
$namespace->addUse('Bar\C');
19+
20+
Assert::same('\Bar', $namespace->unresolveName('Bar'));
21+
Assert::same('C', $namespace->unresolveName('bar\C'));
22+
Assert::same('C\D', $namespace->unresolveName('Bar\C\D'));
23+
24+
25+
$classA = $namespace->addClass('A');
26+
Assert::same($namespace, $classA->getNamespace());
27+
28+
$interfaceB = $namespace->addInterface('B');
29+
Assert::same($namespace, $interfaceB->getNamespace());
30+
31+
$classA
32+
->addImplement('Foo\\A')
33+
->addImplement('Bar\\C')
34+
->addTrait('Bar\\D');
35+
36+
37+
Assert::matchFile(__DIR__ . '/PhpNamespace.expect', (string) $namespace);

0 commit comments

Comments
 (0)