Skip to content

Commit 2679627

Browse files
committed
added Factory::fromMethodReflection()
1 parent f5ead28 commit 2679627

File tree

3 files changed

+38
-21
lines changed

3 files changed

+38
-21
lines changed

src/PhpGenerator/Factory.php

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public function fromClassReflection(\ReflectionClass $from)
4545
$class->setProperties($props);
4646
foreach ($from->getMethods() as $method) {
4747
if ($method->getDeclaringClass()->getName() === $from->getName()) {
48-
$methods[] = $this->fromFunctionReflection($method)->setNamespace($class->getNamespace());
48+
$methods[] = $this->fromMethodReflection($method)->setNamespace($class->getNamespace());
4949
}
5050
}
5151
$class->setMethods($methods);
@@ -56,25 +56,19 @@ public function fromClassReflection(\ReflectionClass $from)
5656
/**
5757
* @return Method
5858
*/
59-
public function fromFunctionReflection(\ReflectionFunctionAbstract $from)
59+
public function fromMethodReflection(\ReflectionMethod $from)
6060
{
61-
$method = $from instanceof \ReflectionMethod
62-
? new Method($from->getName())
63-
: ($from->isClosure() ? new Closure : new GlobalFunction($from->getName()));
61+
$method = new Method($from->getName());
6462
$method->setParameters(array_map([$this, 'fromParameterReflection'], $from->getParameters()));
65-
if ($from instanceof \ReflectionMethod) {
66-
$isInterface = $from->getDeclaringClass()->isInterface();
67-
$method->setStatic($from->isStatic());
68-
$method->setVisibility($from->isPrivate() ? 'private' : ($from->isProtected() ? 'protected' : ($isInterface ? NULL : 'public')));
69-
$method->setFinal($from->isFinal());
70-
$method->setAbstract($from->isAbstract() && !$isInterface);
71-
$method->setBody($from->isAbstract() ? FALSE : '');
72-
}
63+
$method->setStatic($from->isStatic());
64+
$isInterface = $from->getDeclaringClass()->isInterface();
65+
$method->setVisibility($from->isPrivate() ? 'private' : ($from->isProtected() ? 'protected' : ($isInterface ? NULL : 'public')));
66+
$method->setFinal($from->isFinal());
67+
$method->setAbstract($from->isAbstract() && !$isInterface);
68+
$method->setBody($from->isAbstract() ? FALSE : '');
7369
$method->setReturnReference($from->returnsReference());
7470
$method->setVariadic($from->isVariadic());
75-
if (!$from->isClosure()) {
76-
$method->setComment(Helpers::unformatDocComment($from->getDocComment()));
77-
}
71+
$method->setComment(Helpers::unformatDocComment($from->getDocComment()));
7872
if (PHP_VERSION_ID >= 70000 && $from->hasReturnType()) {
7973
$method->setReturnType((string) $from->getReturnType());
8074
$method->setReturnNullable($from->getReturnType()->allowsNull());
@@ -83,6 +77,26 @@ public function fromFunctionReflection(\ReflectionFunctionAbstract $from)
8377
}
8478

8579

80+
/**
81+
* @return GlobalFunction|Closure
82+
*/
83+
public function fromFunctionReflection(\ReflectionFunction $from)
84+
{
85+
$function = $from->isClosure() ? new Closure : new GlobalFunction($from->getName());
86+
$function->setParameters(array_map([$this, 'fromParameterReflection'], $from->getParameters()));
87+
$function->setReturnReference($from->returnsReference());
88+
$function->setVariadic($from->isVariadic());
89+
if (!$from->isClosure()) {
90+
$function->setComment(Helpers::unformatDocComment($from->getDocComment()));
91+
}
92+
if (PHP_VERSION_ID >= 70000 && $from->hasReturnType()) {
93+
$function->setReturnType((string) $from->getReturnType());
94+
$function->setReturnNullable($from->getReturnType()->allowsNull());
95+
}
96+
return $function;
97+
}
98+
99+
86100
/**
87101
* @return Parameter
88102
*/

src/PhpGenerator/Method.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,12 @@ class Method
3939
*/
4040
public static function from($method)
4141
{
42-
return (new Factory)->fromFunctionReflection(
43-
$method instanceof \ReflectionFunctionAbstract ? $method : Nette\Utils\Callback::toReflection($method)
44-
);
42+
$method = $method instanceof \ReflectionFunctionAbstract ? $method : Nette\Utils\Callback::toReflection($method);
43+
if ($method instanceof \ReflectionFunction) {
44+
trigger_error('For global functions or closures use Nette\PhpGenerator\GlobalFunction or Nette\PhpGenerator\Closure.', E_USER_DEPRECATED);
45+
return (new Factory)->fromFunctionReflection($method);
46+
}
47+
return (new Factory)->fromMethodReflection($method);
4548
}
4649

4750

@@ -51,7 +54,7 @@ public static function from($method)
5154
public function __construct($name = NULL)
5255
{
5356
if ($name === NULL) {
54-
throw new Nette\DeprecatedException('For closures use Nette\PhpGenerator\GlobalFunction instead of Nette\PhpGenerator\Method.');
57+
throw new Nette\DeprecatedException('For closures use Nette\PhpGenerator\Closure instead of Nette\PhpGenerator\Method.');
5558
}
5659
$this->setName($name);
5760
}

tests/PhpGenerator/Factory.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ Assert::type(Nette\PhpGenerator\ClassType::class, $res);
1818
Assert::same('stdClass', $res->getName());
1919

2020

21-
$res = $factory->fromFunctionReflection(new \ReflectionMethod(ReflectionClass::class, 'getName'));
21+
$res = $factory->fromMethodReflection(new \ReflectionMethod(ReflectionClass::class, 'getName'));
2222
Assert::type(Nette\PhpGenerator\Method::class, $res);
2323
Assert::same('getName', $res->getName());
2424

0 commit comments

Comments
 (0)