Skip to content

Commit 14dce1c

Browse files
committed
Method, Parameter: added support for build-in types callable, self, parent
1 parent 0dcfe23 commit 14dce1c

File tree

7 files changed

+28
-18
lines changed

7 files changed

+28
-18
lines changed

src/PhpGenerator/Method.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,12 +80,13 @@ public static function from($from)
8080
*/
8181
public function __toString()
8282
{
83+
static $builtinTypes = array('array', 'self', 'parent', 'callable', '');
8384
$parameters = array();
8485
foreach ($this->parameters as $param) {
8586
$variadic = $this->variadic && $param === end($this->parameters);
86-
$hint = in_array($param->getTypeHint(), array('array', ''))
87+
$hint = !$this->namespace || in_array($param->getTypeHint(), $builtinTypes, TRUE)
8788
? $param->getTypeHint()
88-
: ($this->namespace ? $this->namespace->unresolveName($param->getTypeHint()) : $param->getTypeHint());
89+
: $this->namespace->unresolveName($param->getTypeHint());
8990

9091
$parameters[] = ($hint ? $hint . ' ' : '')
9192
. ($param->isReference() ? '&' : '')

src/PhpGenerator/Parameter.php

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,19 @@ public static function from(\ReflectionParameter $from)
3939
$param = new static;
4040
$param->name = $from->getName();
4141
$param->reference = $from->isPassedByReference();
42-
try {
43-
$param->typeHint = $from->isArray() ? 'array' : ($from->getClass() ? '\\' . $from->getClass()->getName() : '');
44-
} catch (\ReflectionException $e) {
45-
if (preg_match('#Class (.+) does not exist#', $e->getMessage(), $m)) {
46-
$param->typeHint = '\\' . $m[1];
47-
} else {
48-
throw $e;
42+
if ($from->isArray()) {
43+
$param->typeHint = 'array';
44+
} elseif (PHP_VERSION_ID >= 50400 && $from->isCallable()) {
45+
$param->typeHint = 'callable';
46+
} else {
47+
try {
48+
$param->typeHint = $from->getClass() ? '\\' . $from->getClass()->getName() : '';
49+
} catch (\ReflectionException $e) {
50+
if (preg_match('#Class (.+) does not exist#', $e->getMessage(), $m)) {
51+
$param->typeHint = '\\' . $m[1];
52+
} else {
53+
throw $e;
54+
}
4955
}
5056
}
5157
$param->optional = PHP_VERSION_ID < 50407 ? $from->isOptional() || ($param->typeHint && $from->allowsNull()) : $from->isDefaultValueAvailable();

tests/PhpGenerator/ClassType.from.expect

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ class Class2 extends Class1 implements Interface2
4848
* Func3
4949
* @return Class1
5050
*/
51-
private function & func3(array $a%a?%, Class2 $b = NULL, Unknown $c, \Xyz\Unknown $d, $e)
51+
private function & func3(array $a%a?%, Class2 $b = NULL, Unknown $c, \Xyz\Unknown $d, callable $e, $f)
5252
{
5353
}
5454

tests/PhpGenerator/ClassType.from.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ class Class2 extends Class1 implements Interface2
5555
* Func3
5656
* @return Class1
5757
*/
58-
private function & func3(array $a = array(), Class2 $b = NULL, \Abc\Unknown $c, \Xyz\Unknown $d, $e)
58+
private function & func3(array $a = array(), Class2 $b = NULL, \Abc\Unknown $c, \Xyz\Unknown $d, callable $e, $f)
5959
{}
6060

6161
final function func2()

tests/PhpGenerator/Method.variadics.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ $method = (new Method)
6969
->setBody('return 42;');
7070
$method->addParameter('foo');
7171
$method->addParameter('bar');
72-
$method->addParameter('baz', []);
72+
$method->addParameter('baz', array());
7373

7474
Assert::match(
7575
'function variadic($foo, $bar, ...$baz)

tests/PhpGenerator/PhpNamespace.expect

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ class A implements A, C
77
use \Bar\D;
88

99

10-
public function test(C $test)
10+
public function test(C $a, self $b, parent $c, array $d, callable $e)
1111
{
1212
}
1313

tests/PhpGenerator/PhpNamespace.phpt

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,13 @@ Assert::exception(function () use ($namespace) {
3636
$classA
3737
->addImplement('Foo\\A')
3838
->addImplement('Bar\\C')
39-
->addTrait('Bar\\D')
40-
->addMethod('test')
41-
->addParameter('test')
42-
->setTypeHint('Bar\C');
43-
39+
->addTrait('Bar\\D');
40+
41+
$method = $classA->addMethod('test');
42+
$method->addParameter('a')->setTypeHint('Bar\C');
43+
$method->addParameter('b')->setTypeHint('self');
44+
$method->addParameter('c')->setTypeHint('parent');
45+
$method->addParameter('d')->setTypeHint('array');
46+
$method->addParameter('e')->setTypeHint('callable');
4447

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

0 commit comments

Comments
 (0)