From fcc20c4eff1c90fb78f10fbee6af269631377f9c Mon Sep 17 00:00:00 2001 From: David Grudl Date: Tue, 16 Apr 2024 19:10:15 +0200 Subject: [PATCH] refactoring --- src/Application/LinkGenerator.php | 41 ++++++++++------------ src/Application/UI/ComponentReflection.php | 26 +++++--------- src/Application/UI/Presenter.php | 2 +- 3 files changed, 29 insertions(+), 40 deletions(-) diff --git a/src/Application/LinkGenerator.php b/src/Application/LinkGenerator.php index c96e53b18..e07de7c77 100644 --- a/src/Application/LinkGenerator.php +++ b/src/Application/LinkGenerator.php @@ -13,6 +13,7 @@ use Nette\Application; use Nette\Http\UrlScript; use Nette\Routing\Router; +use Nette\Utils\Reflection; /** @@ -135,15 +136,12 @@ public function createRequest( $method = $reflection->getSignalMethod($signal); if (!$method) { throw new UI\InvalidLinkException("Unknown signal '$signal', missing handler {$reflection->getName()}::{$component::formatSignalMethod($signal)}()"); - } elseif ( - $refPresenter?->invalidLinkMode - && (UI\ComponentReflection::parseAnnotation($method, 'deprecated') || $method->getAttributes(Attributes\Deprecated::class)) - ) { + } elseif ($this->isDeprecated($refPresenter, $method)) { trigger_error("Link to deprecated signal '$signal'" . ($component === $refPresenter ? '' : ' in ' . $component::class) . " from '{$refPresenter->getName()}:{$refPresenter->getAction()}'.", E_USER_DEPRECATED); } // convert indexed parameters to named - static::argsToParams($component::class, $method->getName(), $args, [], $missing); + static::argsToParams($method, $args, [], $missing); } // counterpart of StatePersistent @@ -169,22 +167,16 @@ public function createRequest( $current = $refPresenter && ($action === '*' || strcasecmp($action, $refPresenter->getAction()) === 0) && $presenterClass === $refPresenter::class; $reflection = new UI\ComponentReflection($presenterClass); - if ($refPresenter?->invalidLinkMode - && (UI\ComponentReflection::parseAnnotation($reflection, 'deprecated') || $reflection->getAttributes(Attributes\Deprecated::class)) - ) { + if ($this->isDeprecated($refPresenter, $reflection)) { trigger_error("Link to deprecated presenter '$presenter' from '{$refPresenter->getName()}:{$refPresenter->getAction()}'.", E_USER_DEPRECATED); } // counterpart of run() & tryCall() if ($method = $reflection->getActionRenderMethod($action)) { - if ( - $refPresenter?->invalidLinkMode - && (UI\ComponentReflection::parseAnnotation($method, 'deprecated') || $method->getAttributes(Attributes\Deprecated::class)) - ) { + if ($this->isDeprecated($refPresenter, $method)) { trigger_error("Link to deprecated action '$presenter:$action' from '{$refPresenter->getName()}:{$refPresenter->getAction()}'.", E_USER_DEPRECATED); } - - static::argsToParams($presenterClass, $method->getName(), $args, $path === 'this' ? $refPresenter->getParameters() : [], $missing); + static::argsToParams($method, $args, $path === 'this' ? $refPresenter->getParameters() : [], $missing); } elseif (array_key_exists(0, $args)) { throw new UI\InvalidLinkException("Unable to pass parameters to action '$presenter:$action', missing corresponding method."); @@ -214,7 +206,7 @@ public function createRequest( if ($mode !== 'test' && !empty($missing)) { foreach ($missing as $rp) { if (!array_key_exists($rp->getName(), $args)) { - throw new UI\InvalidLinkException("Missing parameter \${$rp->getName()} required by {$rp->getDeclaringClass()->getName()}::{$rp->getDeclaringFunction()->getName()}()"); + throw new UI\InvalidLinkException("Missing parameter \${$rp->getName()} required by " . Reflection::toString($rp->getDeclaringFunction())); } } } @@ -270,16 +262,14 @@ public static function parseDestination(string $destination): array * @internal */ public static function argsToParams( - string $class, - string $method, + \ReflectionMethod $method, array &$args, array $supplemental = [], ?array &$missing = null, ): void { $i = 0; - $rm = new \ReflectionMethod($class, $method); - foreach ($rm->getParameters() as $param) { + foreach ($method->getParameters() as $param) { $type = UI\ComponentReflection::getType($param); $name = $param->getName(); @@ -312,9 +302,9 @@ public static function argsToParams( if (!UI\ComponentReflection::convertType($args[$name], $type)) { throw new UI\InvalidLinkException(sprintf( - 'Argument $%s passed to %s() must be %s, %s given.', + 'Argument $%s passed to %s must be %s, %s given.', $name, - $rm->getDeclaringClass()->getName() . '::' . $rm->getName(), + Reflection::toString($method), $type, get_debug_type($args[$name]), )); @@ -329,7 +319,7 @@ public static function argsToParams( } if (array_key_exists($i, $args)) { - throw new UI\InvalidLinkException("Passed more parameters than method $class::{$rm->getName()}() expects."); + throw new UI\InvalidLinkException('Passed more parameters than method ' . Reflection::toString($method) . ' expects.'); } } @@ -366,4 +356,11 @@ public function withReferenceUrl(string $url): static $this->presenterFactory, ); } + + + private function isDeprecated(?UI\Presenter $presenter, \ReflectionClass|\ReflectionMethod $reflection): bool + { + return $presenter?->invalidLinkMode + && (UI\ComponentReflection::parseAnnotation($reflection, 'deprecated') || $reflection->getAttributes(Attributes\Deprecated::class)); + } } diff --git a/src/Application/UI/ComponentReflection.php b/src/Application/UI/ComponentReflection.php index 50c6fc911..05cccfb5e 100644 --- a/src/Application/UI/ComponentReflection.php +++ b/src/Application/UI/ComponentReflection.php @@ -10,6 +10,7 @@ namespace Nette\Application\UI; use Nette; +use Nette\Utils\Reflection; /** @@ -22,7 +23,6 @@ final class ComponentReflection extends \ReflectionClass { private static array $ppCache = []; private static array $pcCache = []; - private static array $mcCache = []; private static array $armCache = []; @@ -148,18 +148,10 @@ public function saveState(Component $component, array &$params): void */ public function hasCallableMethod(string $method): bool { - $class = $this->getName(); - $cache = &self::$mcCache[strtolower($class . ':' . $method)]; - if ($cache === null) { - try { - $cache = false; - $rm = new \ReflectionMethod($class, $method); - $cache = $this->isInstantiable() && $rm->isPublic() && !$rm->isAbstract() && !$rm->isStatic(); - } catch (\ReflectionException) { - } - } - - return $cache; + return $this->isInstantiable() + && $this->hasMethod($method) + && ($rm = $this->getMethod($method)) + && $rm->isPublic() && !$rm->isAbstract() && !$rm->isStatic(); } @@ -173,9 +165,9 @@ public static function combineArgs(\ReflectionFunctionAbstract $method, array $a $res[$i] = $args[$name]; if (!self::convertType($res[$i], $type)) { throw new Nette\InvalidArgumentException(sprintf( - 'Argument $%s passed to %s() must be %s, %s given.', + 'Argument $%s passed to %s must be %s, %s given.', $name, - ($method instanceof \ReflectionMethod ? $method->getDeclaringClass()->getName() . '::' : '') . $method->getName(), + Reflection::toString($method), $type, get_debug_type($args[$name]), )); @@ -188,9 +180,9 @@ public static function combineArgs(\ReflectionFunctionAbstract $method, array $a $res[$i] = []; } else { throw new Nette\InvalidArgumentException(sprintf( - 'Missing parameter $%s required by %s()', + 'Missing parameter $%s required by %s', $name, - ($method instanceof \ReflectionMethod ? $method->getDeclaringClass()->getName() . '::' : '') . $method->getName(), + Reflection::toString($method), )); } } diff --git a/src/Application/UI/Presenter.php b/src/Application/UI/Presenter.php index 9474f0a9a..7f76a725d 100644 --- a/src/Application/UI/Presenter.php +++ b/src/Application/UI/Presenter.php @@ -862,7 +862,7 @@ public static function argsToParams( ?array &$missing = null, ): void { - LinkGenerator::argsToParams($class, $method, $args, $supplemental, $missing); + LinkGenerator::argsToParams(new \ReflectionMethod($class, $method), $args, $supplemental, $missing); }