Skip to content

Commit 2349700

Browse files
committed
added support for namespaces in type hints
1 parent ebbc88e commit 2349700

File tree

4 files changed

+20
-2
lines changed

4 files changed

+20
-2
lines changed

src/PhpGenerator/ClassType.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,10 @@ public function __toString()
214214
$traits = (array) $this->traits;
215215
}
216216

217+
foreach ($this->methods as $method) {
218+
$method->setNamespace($this->namespace);
219+
}
220+
217221
return Strings::normalize(
218222
($this->documents ? str_replace("\n", "\n * ", "/**\n" . implode("\n", (array) $this->documents)) . "\n */\n" : '')
219223
. ($this->abstract ? 'abstract ' : '')

src/PhpGenerator/Method.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
* @method Method setDocuments(string[])
3838
* @method string[] getDocuments()
3939
* @method Method addDocument(string)
40+
* @method Method setNamespace(PhpNamespace|NULL)
4041
*/
4142
class Method extends Nette\Object
4243
{
@@ -73,6 +74,9 @@ class Method extends Nette\Object
7374
/** @var array of string */
7475
private $documents = array();
7576

77+
/** @var PhpNamespace */
78+
private $namespace;
79+
7680

7781
/** @return Method */
7882
public static function from($from)
@@ -136,7 +140,11 @@ public function __toString()
136140
$parameters = array();
137141
foreach ($this->parameters as $param) {
138142
$variadic = $this->variadic && $param === end($this->parameters);
139-
$parameters[] = ($param->typeHint ? $param->typeHint . ' ' : '')
143+
$hint = in_array($param->typeHint, array('array', ''))
144+
? $param->typeHint
145+
: ($this->namespace ? $this->namespace->unresolveName($param->typeHint) : $param->typeHint);
146+
147+
$parameters[] = ($hint ? $hint . ' ' : '')
140148
. ($param->reference ? '&' : '')
141149
. ($variadic ? '...' : '')
142150
. '$' . $param->name

tests/PhpGenerator/PhpNamespace.expect

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@ class A implements A, C
77

88
use \Bar\D;
99

10+
public function test(C $test)
11+
{
1012

13+
}
1114

1215
}
1316

tests/PhpGenerator/PhpNamespace.phpt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,10 @@ Assert::exception(function() use ($namespace) {
3636
$classA
3737
->addImplement('Foo\\A')
3838
->addImplement('Bar\\C')
39-
->addTrait('Bar\\D');
39+
->addTrait('Bar\\D')
40+
->addMethod('test')
41+
->addParameter('test')
42+
->setTypeHint('Bar\C');
4043

4144

4245
Assert::matchFile(__DIR__ . '/PhpNamespace.expect', (string) $namespace);

0 commit comments

Comments
 (0)