From 676b60c0cc47a3ad2312b5c75a4ebda1bb560bc0 Mon Sep 17 00:00:00 2001 From: David Grudl Date: Tue, 4 Oct 2022 01:50:48 +0200 Subject: [PATCH] FunctionLike changed to abstract class --- src/PhpGenerator/Closure.php | 5 +---- src/PhpGenerator/Extractor.php | 2 +- src/PhpGenerator/Factory.php | 2 +- src/PhpGenerator/{Traits => }/FunctionLike.php | 10 +++++----- src/PhpGenerator/GlobalFunction.php | 5 +---- src/PhpGenerator/Method.php | 11 ++++++----- src/PhpGenerator/Printer.php | 2 +- 7 files changed, 16 insertions(+), 21 deletions(-) rename src/PhpGenerator/{Traits => }/FunctionLike.php (95%) diff --git a/src/PhpGenerator/Closure.php b/src/PhpGenerator/Closure.php index 398e1491..d019fbd6 100644 --- a/src/PhpGenerator/Closure.php +++ b/src/PhpGenerator/Closure.php @@ -14,13 +14,10 @@ /** * Closure. - * - * @property-deprecated string $body */ -final class Closure +final class Closure extends FunctionLike { use Nette\SmartObject; - use Traits\FunctionLike; use Traits\AttributeAware; /** @var Parameter[] */ diff --git a/src/PhpGenerator/Extractor.php b/src/PhpGenerator/Extractor.php index f2054fcd..148bf3a4 100644 --- a/src/PhpGenerator/Extractor.php +++ b/src/PhpGenerator/Extractor.php @@ -385,7 +385,7 @@ private function addCommentAndAttributes($element, Node $node): void } - private function setupFunction(GlobalFunction|Method $function, Node\FunctionLike $node): void + private function setupFunction(FunctionLike $function, Node\FunctionLike $node): void { $function->setReturnReference($node->returnsByRef()); $function->setReturnType($node->getReturnType() ? $this->toPhp($node->getReturnType()) : null); diff --git a/src/PhpGenerator/Factory.php b/src/PhpGenerator/Factory.php index 33c6f115..c8193a59 100644 --- a/src/PhpGenerator/Factory.php +++ b/src/PhpGenerator/Factory.php @@ -194,7 +194,7 @@ public function fromFunctionReflection(\ReflectionFunction $from, bool $withBody } - public function fromCallable(callable $from): Method|GlobalFunction|Closure + public function fromCallable(callable $from): FunctionLike { $ref = Nette\Utils\Callback::toReflection($from); return $ref instanceof \ReflectionMethod diff --git a/src/PhpGenerator/Traits/FunctionLike.php b/src/PhpGenerator/FunctionLike.php similarity index 95% rename from src/PhpGenerator/Traits/FunctionLike.php rename to src/PhpGenerator/FunctionLike.php index 15bb8e57..ace264ce 100644 --- a/src/PhpGenerator/Traits/FunctionLike.php +++ b/src/PhpGenerator/FunctionLike.php @@ -7,18 +7,18 @@ declare(strict_types=1); -namespace Nette\PhpGenerator\Traits; +namespace Nette\PhpGenerator; use Nette; -use Nette\PhpGenerator\Dumper; -use Nette\PhpGenerator\Parameter; use Nette\Utils\Type; /** - * @internal + * GlobalFunction/Closure/Method description. + * + * @property-deprecated string $body */ -trait FunctionLike +abstract class FunctionLike { private string $body = ''; diff --git a/src/PhpGenerator/GlobalFunction.php b/src/PhpGenerator/GlobalFunction.php index ae1e4f88..26665332 100644 --- a/src/PhpGenerator/GlobalFunction.php +++ b/src/PhpGenerator/GlobalFunction.php @@ -14,13 +14,10 @@ /** * Global function. - * - * @property-deprecated string $body */ -final class GlobalFunction +final class GlobalFunction extends FunctionLike { use Nette\SmartObject; - use Traits\FunctionLike; use Traits\NameAware; use Traits\CommentAware; use Traits\AttributeAware; diff --git a/src/PhpGenerator/Method.php b/src/PhpGenerator/Method.php index 0596658c..fbffab1f 100644 --- a/src/PhpGenerator/Method.php +++ b/src/PhpGenerator/Method.php @@ -14,13 +14,10 @@ /** * Class method. - * - * @property-deprecated string|null $body */ -final class Method +final class Method extends FunctionLike { use Nette\SmartObject; - use Traits\FunctionLike; use Traits\NameAware; use Traits\VisibilityAware; use Traits\CommentAware; @@ -92,7 +89,11 @@ public function addPromotedParameter(string $name, mixed $defaultValue = null): $param->setDefaultValue($defaultValue); } - return $this->parameters[$name] = $param; + $params = $this->getParameters(); + $params[$name] = $param; + $this->setParameters($params); + + return $param; } diff --git a/src/PhpGenerator/Printer.php b/src/PhpGenerator/Printer.php index c7005fe1..4e0f3ee3 100644 --- a/src/PhpGenerator/Printer.php +++ b/src/PhpGenerator/Printer.php @@ -385,7 +385,7 @@ protected function printDocComment(/*Traits\CommentAware*/ $commentable): string } - private function printReturnType(Closure|GlobalFunction|Method $function): string + private function printReturnType(FunctionLike $function): string { return ($tmp = $this->printType($function->getReturnType(), $function->isReturnNullable())) ? $this->returnTypeColon . $tmp