Skip to content

Commit ef45bd5

Browse files
committed
added Factory::fromCallable()
1 parent caa5b44 commit ef45bd5

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

src/PhpGenerator/Factory.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,16 @@ public function fromFunctionReflection(\ReflectionFunction $from)
101101
}
102102

103103

104+
/** @return Method|GlobalFunction|Closure */
105+
public function fromCallable(callable $from)
106+
{
107+
$ref = Nette\Utils\Callback::toReflection($from);
108+
return $ref instanceof \ReflectionMethod
109+
? self::fromMethodReflection($ref)
110+
: self::fromFunctionReflection($ref);
111+
}
112+
113+
104114
public function fromParameterReflection(\ReflectionParameter $from): Parameter
105115
{
106116
$param = new Parameter($from->getName());

tests/PhpGenerator/Factory.phpt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,13 @@ Assert::same('trim', $res->getName());
3838

3939
$res = $factory->fromFunctionReflection(new \ReflectionFunction(function () {}));
4040
Assert::type(Nette\PhpGenerator\Closure::class, $res);
41+
42+
43+
$res = $factory->fromCallable('trim');
44+
Assert::type(Nette\PhpGenerator\GlobalFunction::class, $res);
45+
Assert::same('trim', $res->getName());
46+
47+
48+
$res = $factory->fromCallable([new ReflectionClass(stdClass::class), 'getName']);
49+
Assert::type(Nette\PhpGenerator\Method::class, $res);
50+
Assert::same('getName', $res->getName());

0 commit comments

Comments
 (0)