Skip to content

Commit 3689d92

Browse files
committed
replaced use of Reflection::getReturnTypes() etc
1 parent 5b9152c commit 3689d92

5 files changed

+27
-32
lines changed

src/DI/Definitions/AccessorDefinition.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
use Nette;
1313
use Nette\DI\ServiceCreationException;
14-
use Nette\Utils\Reflection;
14+
use Nette\Utils\Type;
1515

1616

1717
/**
@@ -134,7 +134,7 @@ public function generateMethod(Nette\PhpGenerator\Method $method, Nette\DI\PhpGe
134134

135135
$class->addMethod(self::METHOD_GET)
136136
->setBody('return $this->container->getService(?);', [$this->reference->getValue()])
137-
->setReturnType(Reflection::getReturnType($rm));
137+
->setReturnType((string) Type::fromReflection($rm));
138138

139139
$method->setBody('return new class ($this) ' . $class . ';');
140140
}

src/DI/Definitions/FactoryDefinition.php

+6-10
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Nette;
1313
use Nette\DI\ServiceCreationException;
1414
use Nette\Utils\Reflection;
15+
use Nette\Utils\Type;
1516

1617

1718
/**
@@ -242,13 +243,11 @@ private function completeParameters(Nette\DI\Resolver $resolver): void
242243
}
243244

244245
foreach ($method->getParameters() as $param) {
245-
$methodHint = Reflection::getParameterTypes($param);
246+
$methodType = Type::fromReflection($param);
246247
if (isset($ctorParams[$param->name])) {
247248
$ctorParam = $ctorParams[$param->name];
248-
$ctorHint = Reflection::getParameterTypes($ctorParam);
249-
if ($methodHint !== $ctorHint
250-
&& !is_a((string) reset($methodHint), (string) reset($ctorHint), true)
251-
) {
249+
$ctorType = Type::fromReflection($ctorParam);
250+
if ($ctorType && !$ctorType->allows((string) $methodType)) {
252251
throw new ServiceCreationException(sprintf(
253252
"Type of \$%s in %s::create() doesn't match type in %s constructor.",
254253
$param->name,
@@ -267,10 +266,7 @@ private function completeParameters(Nette\DI\Resolver $resolver): void
267266
) . ($hint ? ", did you mean \${$hint}?" : '.'));
268267
}
269268

270-
$paramDef = PHP_VERSION_ID < 80000
271-
? ($methodHint && $param->allowsNull() ? '?' : '') . reset($methodHint)
272-
: implode('|', $methodHint);
273-
$paramDef .= ' ' . $param->name;
269+
$paramDef = $methodType . ' ' . $param->name;
274270
if ($param->isDefaultValueAvailable()) {
275271
$this->parameters[$paramDef] = Reflection::getParameterDefaultValue($param);
276272
} else {
@@ -302,7 +298,7 @@ public function generateMethod(Nette\PhpGenerator\Method $method, Nette\DI\PhpGe
302298
$rm = new \ReflectionMethod($this->getType(), self::METHOD_CREATE);
303299
$methodCreate
304300
->setParameters($generator->convertParameters($this->parameters))
305-
->setReturnType(Reflection::getReturnType($rm) ?: $this->getResultType())
301+
->setReturnType((string) (Type::fromReflection($rm) ?? $this->getResultType()))
306302
->setBody($body);
307303

308304
$method->setBody('return new class ($this) ' . $class . ';');

src/DI/Definitions/LocatorDefinition.php

+1-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
namespace Nette\DI\Definitions;
1111

1212
use Nette;
13-
use Nette\Utils\Reflection;
1413

1514

1615
/**
@@ -140,7 +139,7 @@ public function generateMethod(Nette\PhpGenerator\Method $method, Nette\DI\PhpGe
140139
$nullable = $rm->getReturnType()->allowsNull();
141140

142141
$methodInner = $class->addMethod($rm->name)
143-
->setReturnType(Reflection::getReturnType($rm))
142+
->setReturnType((string) Nette\Utils\Type::fromReflection($rm))
144143
->setReturnNullable($nullable);
145144

146145
if (!$name) {

src/DI/DependencyChecker.php

+5-4
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
use Nette;
1313
use Nette\Utils\Reflection;
14+
use Nette\Utils\Type;
1415
use ReflectionClass;
1516
use ReflectionMethod;
1617

@@ -121,7 +122,7 @@ class_uses($name),
121122
$name,
122123
$prop->name,
123124
$prop->getDocComment(),
124-
Reflection::getPropertyTypes($prop),
125+
(string) Type::fromReflection($prop),
125126
PHP_VERSION_ID >= 80000 ? count($prop->getAttributes(Attributes\Inject::class)) : null,
126127
];
127128
}
@@ -133,7 +134,7 @@ class_uses($name),
133134
$method->name,
134135
$method->getDocComment(),
135136
self::hashParameters($method),
136-
Reflection::getReturnTypes($method),
137+
(string) Type::fromReflection($method),
137138
];
138139
}
139140
}
@@ -157,7 +158,7 @@ class_uses($name),
157158
$uses,
158159
$method->getDocComment(),
159160
self::hashParameters($method),
160-
Reflection::getReturnTypes($method),
161+
(string) Type::fromReflection($method),
161162
];
162163
}
163164

@@ -171,7 +172,7 @@ private static function hashParameters(\ReflectionFunctionAbstract $method): arr
171172
foreach ($method->getParameters() as $param) {
172173
$res[] = [
173174
$param->name,
174-
Reflection::getParameterTypes($param),
175+
(string) Type::fromReflection($param),
175176
$param->isVariadic(),
176177
$param->isDefaultValueAvailable()
177178
? [Reflection::getParameterDefaultValue($param)]

src/DI/Resolver.php

+13-14
Original file line numberDiff line numberDiff line change
@@ -561,53 +561,52 @@ public static function autowireArguments(
561561
*/
562562
private static function autowireArgument(\ReflectionParameter $parameter, callable $getter)
563563
{
564+
$method = $parameter->getDeclaringFunction();
564565
$desc = Reflection::toString($parameter);
565-
if ($parameter->getType() instanceof \ReflectionIntersectionType) {
566+
$type = Nette\Utils\Type::fromReflection($parameter);
567+
568+
if ($type && $type->isIntersection()) {
566569
throw new ServiceCreationException(sprintf(
567570
'Parameter %s has intersection type, so its value must be specified.',
568571
$desc
569572
));
570-
}
571-
572-
$types = array_diff(Reflection::getParameterTypes($parameter), ['null']);
573-
$type = count($types) === 1 ? reset($types) : null;
574-
$method = $parameter->getDeclaringFunction();
575573

576-
if ($type && !Reflection::isBuiltinType($type)) {
574+
} elseif ($type && $type->isClass()) {
575+
$class = $type->getSingleName();
577576
try {
578-
$res = $getter($type, true);
577+
$res = $getter($class, true);
579578
} catch (MissingServiceException $e) {
580579
$res = null;
581580
} catch (ServiceCreationException $e) {
582581
throw new ServiceCreationException("{$e->getMessage()} (required by $desc)", 0, $e);
583582
}
584583
if ($res !== null || $parameter->allowsNull()) {
585584
return $res;
586-
} elseif (class_exists($type) || interface_exists($type)) {
585+
} elseif (class_exists($class) || interface_exists($class)) {
587586
throw new ServiceCreationException(sprintf(
588587
'Service of type %s required by %s not found. Did you add it to configuration file?',
589-
$type,
588+
$class,
590589
$desc
591590
));
592591
} else {
593592
throw new ServiceCreationException(sprintf(
594593
"Class '%s' required by %s not found. Check the parameter type and 'use' statements.",
595-
$type,
594+
$class,
596595
$desc
597596
));
598597
}
599598

600599
} elseif (
601600
$method instanceof \ReflectionMethod
602-
&& $type === 'array'
601+
&& $type && $type->getSingleName() === 'array'
603602
&& preg_match('#@param[ \t]+([\w\\\\]+)\[\][ \t]+\$' . $parameter->name . '#', (string) $method->getDocComment(), $m)
604603
&& ($itemType = Reflection::expandClassName($m[1], $method->getDeclaringClass()))
605604
&& (class_exists($itemType) || interface_exists($itemType))
606605
) {
607606
return $getter($itemType, false);
608607

609608
} elseif (
610-
($types && $parameter->allowsNull())
609+
($type && $parameter->allowsNull())
611610
|| $parameter->isOptional()
612611
|| $parameter->isDefaultValueAvailable()
613612
) {
@@ -621,7 +620,7 @@ private static function autowireArgument(\ReflectionParameter $parameter, callab
621620
throw new ServiceCreationException(sprintf(
622621
'Parameter %s has %s, so its value must be specified.',
623622
$desc,
624-
count($types) > 1 ? 'union type and no default value' : 'no class type or default value'
623+
$type && $type->isUnion() ? 'union type and no default value' : 'no class type or default value'
625624
));
626625
}
627626
}

0 commit comments

Comments
 (0)