Skip to content

Commit

Permalink
ComponentReflection: detects built-in types [Closes #241]
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Dec 10, 2019
1 parent 79615df commit 42435c8
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 12 deletions.
23 changes: 13 additions & 10 deletions src/Application/UI/ComponentReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,10 +154,10 @@ public static function combineArgs(\ReflectionFunctionAbstract $method, array $a
$res = [];
foreach ($method->getParameters() as $i => $param) {
$name = $param->getName();
[$type, $isClass] = self::getParameterType($param);
$type = self::getParameterType($param);
if (isset($args[$name])) {
$res[$i] = $args[$name];
if (!self::convertType($res[$i], $type, $isClass)) {
if (!self::convertType($res[$i], $type)) {
throw new Nette\InvalidArgumentException(sprintf(
'Argument $%s passed to %s() must be %s, %s given.',
$name,
Expand Down Expand Up @@ -187,9 +187,15 @@ public static function combineArgs(\ReflectionFunctionAbstract $method, array $a
/**
* Non data-loss type conversion.
*/
public static function convertType(&$val, string $type, bool $isClass = false): bool
public static function convertType(&$val, string $type): bool
{
if ($isClass) {
static $builtin = [
'string' => 1, 'int' => 1, 'float' => 1, 'bool' => 1, 'array' => 1, 'object' => 1,
'callable' => 1, 'iterable' => 1, 'void' => 1, 'null' => 1,
'boolean' => 1, 'integer' => 1, 'double' => 1, 'NULL' => 1,
];

if (empty($builtin[$type])) {
return $val instanceof $type;

} elseif ($type === 'callable') {
Expand Down Expand Up @@ -240,14 +246,11 @@ public static function parseAnnotation(\Reflector $ref, string $name): ?array
}


/**
* @return array [string|null, bool]
*/
public static function getParameterType(\ReflectionParameter $param): array
public static function getParameterType(\ReflectionParameter $param): string
{
return $param->hasType()
? [$param->getType()->getName(), !$param->getType()->isBuiltin()]
: [gettype($param->isDefaultValueAvailable() ? $param->getDefaultValue() : null), false];
? $param->getType()->getName()
: gettype($param->isDefaultValueAvailable() ? $param->getDefaultValue() : null);
}


Expand Down
4 changes: 2 additions & 2 deletions src/Application/UI/Presenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -968,7 +968,7 @@ public static function argsToParams(string $class, string $method, array &$args,
$i = 0;
$rm = new \ReflectionMethod($class, $method);
foreach ($rm->getParameters() as $param) {
[$type, $isClass] = ComponentReflection::getParameterType($param);
$type = ComponentReflection::getParameterType($param);
$name = $param->getName();

if (array_key_exists($i, $args)) {
Expand All @@ -991,7 +991,7 @@ public static function argsToParams(string $class, string $method, array &$args,
continue;
}

if (!ComponentReflection::convertType($args[$name], $type, $isClass)) {
if (!ComponentReflection::convertType($args[$name], $type)) {
throw new InvalidLinkException(sprintf(
'Argument $%s passed to %s() must be %s, %s given.',
$name,
Expand Down

0 comments on commit 42435c8

Please sign in to comment.