Skip to content

Commit 5501c46

Browse files
pavelkourildg
authored andcommitted
Method, Parameter: added support for PHP 7 type hints
1 parent 9dac8e0 commit 5501c46

File tree

4 files changed

+127
-1
lines changed

4 files changed

+127
-1
lines changed

src/PhpGenerator/Method.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ class Method extends Nette\Object
5151
/** @var PhpNamespace|NULL */
5252
private $namespace;
5353

54+
/** @var string|NULL */
55+
private $returnType;
56+
5457

5558
/**
5659
* @return self
@@ -79,6 +82,9 @@ public static function from($from)
7982
$method->returnReference = $from->returnsReference();
8083
$method->variadic = PHP_VERSION_ID >= 50600 && $from->isVariadic();
8184
$method->documents = $from->getDocComment() ? array(preg_replace('#^\s*\* ?#m', '', trim($from->getDocComment(), "/* \r\n\t"))) : array();
85+
if (PHP_VERSION_ID >= 70000 && $from->hasReturnType()) {
86+
$method->returnType = (string) $from->getReturnType();
87+
}
8288
return $method;
8389
}
8490

@@ -123,6 +129,7 @@ public function __toString()
123129
. ' ' . $this->name
124130
. '(' . implode(', ', $parameters) . ')'
125131
. ($this->uses ? ' use (' . implode(', ', $uses) . ')' : '')
132+
. ($this->returnType ? ': ' . $namespace->unresolveName($this->returnType) : '')
126133
. ($this->abstract || $this->body === FALSE ? ';'
127134
: ($this->name ? "\n" : ' ') . "{\n" . Nette\Utils\Strings::indent(ltrim(rtrim($this->body) . "\n"), 1) . '}');
128135
}
@@ -408,4 +415,24 @@ public function setNamespace(PhpNamespace $val = NULL)
408415
return $this;
409416
}
410417

418+
419+
/**
420+
* @param string|NULL
421+
* @return self
422+
*/
423+
public function setReturnType($val)
424+
{
425+
$this->returnType = $val ? (string) $val : NULL;
426+
return $this;
427+
}
428+
429+
430+
/**
431+
* @return string|NULL
432+
*/
433+
public function getReturnType()
434+
{
435+
return $this->returnType;
436+
}
437+
411438
}

src/PhpGenerator/Parameter.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@ public static function from(\ReflectionParameter $from)
3838
{
3939
$param = new static($from->getName());
4040
$param->reference = $from->isPassedByReference();
41-
if ($from->isArray()) {
41+
if (PHP_VERSION_ID >= 70000) {
42+
$param->typeHint = $from->hasType() ? (string) $from->getType() : NULL;
43+
} elseif ($from->isArray()) {
4244
$param->typeHint = 'array';
4345
} elseif (PHP_VERSION_ID >= 50400 && $from->isCallable()) {
4446
$param->typeHint = 'callable';
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
/**
4+
* Test: Nette\PhpGenerator - PHP7 return type declarations
5+
* @phpversion 7.0
6+
*/
7+
8+
namespace A
9+
{
10+
class Foo {}
11+
}
12+
13+
namespace
14+
{
15+
use Nette\PhpGenerator\Method;
16+
use Tester\Assert;
17+
18+
19+
require __DIR__ . '/../bootstrap.php';
20+
21+
// test from
22+
23+
interface A
24+
{
25+
function testClass(): \A\Foo;
26+
function testScalar(): string;
27+
}
28+
29+
$method = Method::from(A::class .'::testClass');
30+
Assert::same('A\Foo', $method->getReturnType());
31+
32+
$method = Method::from(A::class .'::testScalar');
33+
Assert::same('string', $method->getReturnType());
34+
35+
// generating methods with return type declarations
36+
37+
$method = (new Method('create'))
38+
->setReturnType('Foo')
39+
->setBody('return new Foo();');
40+
41+
Assert::match(
42+
'function create(): Foo
43+
{
44+
return new Foo();
45+
}
46+
', (string) $method);
47+
48+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
/**
4+
* Test: Nette\PhpGenerator - PHP7 scalar type hints
5+
* @phpversion 7.0
6+
*/
7+
8+
9+
use Nette\PhpGenerator\Method;
10+
use Tester\Assert;
11+
12+
13+
require __DIR__ . '/../bootstrap.php';
14+
15+
// test from
16+
17+
18+
interface Foo
19+
{
20+
function scalars(string $a, bool $b, int $c, float $d);
21+
}
22+
23+
$method = Method::from(Foo::class . '::scalars');
24+
Assert::same('string', $method->getParameters()['a']->getTypeHint());
25+
26+
$method = Method::from(Foo::class . '::scalars');
27+
Assert::same('bool', $method->getParameters()['b']->getTypeHint());
28+
29+
$method = Method::from(Foo::class . '::scalars');
30+
Assert::same('int', $method->getParameters()['c']->getTypeHint());
31+
32+
$method = Method::from(Foo::class . '::scalars');
33+
Assert::same('float', $method->getParameters()['d']->getTypeHint());
34+
35+
36+
// generating methods with scalar type hints
37+
38+
$method = (new Method)
39+
->setName('create')
40+
->setBody('return null;');
41+
$method->addParameter('a')->setTypeHint('string');
42+
$method->addParameter('b')->setTypeHint('bool');
43+
44+
Assert::match(
45+
'function create(string $a, bool $b)
46+
{
47+
return null;
48+
}
49+
', (string) $method);

0 commit comments

Comments
 (0)