Skip to content

Commit 0f3b66a

Browse files
committed
ComponentReflection: changed way how it detects built-in types [Closes #241]
1 parent 79615df commit 0f3b66a

File tree

2 files changed

+15
-12
lines changed

2 files changed

+15
-12
lines changed

src/Application/UI/ComponentReflection.php

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -154,10 +154,10 @@ public static function combineArgs(\ReflectionFunctionAbstract $method, array $a
154154
$res = [];
155155
foreach ($method->getParameters() as $i => $param) {
156156
$name = $param->getName();
157-
[$type, $isClass] = self::getParameterType($param);
157+
$type = self::getParameterType($param);
158158
if (isset($args[$name])) {
159159
$res[$i] = $args[$name];
160-
if (!self::convertType($res[$i], $type, $isClass)) {
160+
if (!self::convertType($res[$i], $type)) {
161161
throw new Nette\InvalidArgumentException(sprintf(
162162
'Argument $%s passed to %s() must be %s, %s given.',
163163
$name,
@@ -187,9 +187,15 @@ public static function combineArgs(\ReflectionFunctionAbstract $method, array $a
187187
/**
188188
* Non data-loss type conversion.
189189
*/
190-
public static function convertType(&$val, string $type, bool $isClass = false): bool
190+
public static function convertType(&$val, string $type): bool
191191
{
192-
if ($isClass) {
192+
static $builtin = [
193+
'string' => 1, 'int' => 1, 'float' => 1, 'bool' => 1, 'array' => 1, 'object' => 1,
194+
'callable' => 1, 'iterable' => 1, 'void' => 1, 'null' => 1,
195+
'boolean' => 1, 'integer' => 1, 'double' => 1, 'NULL' => 1,
196+
];
197+
198+
if (empty($builtin[$type])) {
193199
return $val instanceof $type;
194200

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

242248

243-
/**
244-
* @return array [string|null, bool]
245-
*/
246-
public static function getParameterType(\ReflectionParameter $param): array
249+
public static function getParameterType(\ReflectionParameter $param): string
247250
{
248251
return $param->hasType()
249-
? [$param->getType()->getName(), !$param->getType()->isBuiltin()]
250-
: [gettype($param->isDefaultValueAvailable() ? $param->getDefaultValue() : null), false];
252+
? $param->getType()->getName()
253+
: gettype($param->isDefaultValueAvailable() ? $param->getDefaultValue() : null);
251254
}
252255

253256

src/Application/UI/Presenter.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -968,7 +968,7 @@ public static function argsToParams(string $class, string $method, array &$args,
968968
$i = 0;
969969
$rm = new \ReflectionMethod($class, $method);
970970
foreach ($rm->getParameters() as $param) {
971-
[$type, $isClass] = ComponentReflection::getParameterType($param);
971+
$type = ComponentReflection::getParameterType($param);
972972
$name = $param->getName();
973973

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

994-
if (!ComponentReflection::convertType($args[$name], $type, $isClass)) {
994+
if (!ComponentReflection::convertType($args[$name], $type)) {
995995
throw new InvalidLinkException(sprintf(
996996
'Argument $%s passed to %s() must be %s, %s given.',
997997
$name,

0 commit comments

Comments
 (0)