Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Apr 16, 2024
1 parent 0b89f40 commit fcc20c4
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 40 deletions.
41 changes: 19 additions & 22 deletions src/Application/LinkGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Nette\Application;
use Nette\Http\UrlScript;
use Nette\Routing\Router;
use Nette\Utils\Reflection;


/**
Expand Down Expand Up @@ -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
Expand All @@ -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.");
Expand Down Expand Up @@ -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()));
}
}
}
Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -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]),
));
Expand All @@ -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.');
}
}

Expand Down Expand Up @@ -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));
}
}
26 changes: 9 additions & 17 deletions src/Application/UI/ComponentReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
namespace Nette\Application\UI;

use Nette;
use Nette\Utils\Reflection;


/**
Expand All @@ -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 = [];


Expand Down Expand Up @@ -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();
}


Expand All @@ -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]),
));
Expand All @@ -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),
));
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Application/UI/Presenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}


Expand Down

0 comments on commit fcc20c4

Please sign in to comment.