Skip to content

Commit 5f5524d

Browse files
committed
PhpNamespace::unresolveName() supports for build-in types
1 parent 38e8fbb commit 5f5524d

File tree

3 files changed

+13
-7
lines changed

3 files changed

+13
-7
lines changed

src/PhpGenerator/Method.php

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -96,13 +96,12 @@ public static function from($from)
9696
*/
9797
public function __toString()
9898
{
99-
static $builtinTypes = ['array', 'self', 'parent', 'callable', 'string', 'bool', 'float', 'int', NULL];
10099
$parameters = [];
101100
foreach ($this->parameters as $param) {
102101
$variadic = $this->variadic && $param === end($this->parameters);
103-
$hint = !$this->namespace || in_array($param->getTypeHint(), $builtinTypes, TRUE)
104-
? $param->getTypeHint()
105-
: $this->namespace->unresolveName($param->getTypeHint());
102+
$hint = $this->namespace
103+
? $this->namespace->unresolveName((string) $param->getTypeHint())
104+
: $param->getTypeHint();
106105

107106
$parameters[] = ($hint ? $hint . ' ' : '')
108107
. ($param->isReference() ? '&' : '')
@@ -114,9 +113,9 @@ public function __toString()
114113
foreach ($this->uses as $param) {
115114
$uses[] = ($param->isReference() ? '&' : '') . '$' . $param->getName();
116115
}
117-
$returnType = !$this->namespace || in_array($this->returnType, $builtinTypes, TRUE)
118-
? $this->returnType
119-
: $this->namespace->unresolveName($this->returnType);
116+
$returnType = $this->namespace
117+
? $this->namespace->unresolveName((string) $this->returnType)
118+
: $this->returnType;
120119

121120
return ($this->comment ? str_replace("\n", "\n * ", "/**\n" . $this->comment) . "\n */\n" : '')
122121
. ($this->abstract ? 'abstract ' : '')

src/PhpGenerator/PhpNamespace.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,9 @@ public function addUse($name, $alias = NULL, &$aliasOut = NULL)
134134
*/
135135
public function unresolveName($name)
136136
{
137+
if (in_array(strtolower($name), ['self', 'parent', 'array', 'callable', 'string', 'bool', 'float', 'int', ''], TRUE)) {
138+
return $name;
139+
}
137140
$name = ltrim($name, '\\');
138141
$res = NULL;
139142
$lower = strtolower($name);

tests/PhpGenerator/PhpNamespace.phpt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ Assert::same('\Bar', $namespace->unresolveName('Bar'));
2121
Assert::same('C', $namespace->unresolveName('bar\C'));
2222
Assert::same('C\D', $namespace->unresolveName('Bar\C\D'));
2323

24+
foreach (['String', 'string', 'int', 'float', 'bool', 'array', 'callable', 'self', 'parent', ''] as $type) {
25+
Assert::same($type, $namespace->unresolveName($type));
26+
}
27+
2428

2529
$classA = $namespace->addClass('A');
2630
Assert::same($namespace, $classA->getNamespace());

0 commit comments

Comments
 (0)