diff --git a/src/PhpGenerator/Closure.php b/src/PhpGenerator/Closure.php index 398e1491..43f26e1f 100644 --- a/src/PhpGenerator/Closure.php +++ b/src/PhpGenerator/Closure.php @@ -9,18 +9,12 @@ namespace Nette\PhpGenerator; -use Nette; - /** * 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 0be2d2d8..836853c3 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 94% rename from src/PhpGenerator/Traits/FunctionLike.php rename to src/PhpGenerator/FunctionLike.php index c0b33f99..7d6d0879 100644 --- a/src/PhpGenerator/Traits/FunctionLike.php +++ b/src/PhpGenerator/FunctionLike.php @@ -7,20 +7,22 @@ declare(strict_types=1); -namespace Nette\PhpGenerator\Traits; +namespace Nette\PhpGenerator; use JetBrains\PhpStorm\Language; 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 { + use Nette\SmartObject; + private string $body = ''; /** @var Parameter[] */ diff --git a/src/PhpGenerator/GlobalFunction.php b/src/PhpGenerator/GlobalFunction.php index ae1e4f88..76393715 100644 --- a/src/PhpGenerator/GlobalFunction.php +++ b/src/PhpGenerator/GlobalFunction.php @@ -9,18 +9,12 @@ namespace Nette\PhpGenerator; -use Nette; - /** * 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..2c636a48 100644 --- a/src/PhpGenerator/Method.php +++ b/src/PhpGenerator/Method.php @@ -14,13 +14,9 @@ /** * 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 +88,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