Skip to content

Commit 365f147

Browse files
Fix support for PHP8 union types
1 parent df68ae9 commit 365f147

File tree

3 files changed

+41
-14
lines changed

3 files changed

+41
-14
lines changed

Compiler/CheckTypeDeclarationsPass.php

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -153,9 +153,26 @@ private function checkTypeDeclarations(Definition $checkedDefinition, \Reflectio
153153
/**
154154
* @throws InvalidParameterTypeException When a parameter is not compatible with the declared type
155155
*/
156-
private function checkType(Definition $checkedDefinition, $value, \ReflectionParameter $parameter, ?string $envPlaceholderUniquePrefix): void
156+
private function checkType(Definition $checkedDefinition, $value, \ReflectionParameter $parameter, ?string $envPlaceholderUniquePrefix, string $type = null): void
157157
{
158-
$type = $parameter->getType()->getName();
158+
if (null === $type) {
159+
$type = $parameter->getType();
160+
161+
if ($type instanceof \ReflectionUnionType) {
162+
foreach ($type->getTypes() as $type) {
163+
try {
164+
$this->checkType($checkedDefinition, $value, $parameter, $envPlaceholderUniquePrefix, $type);
165+
166+
return;
167+
} catch (InvalidParameterTypeException $e) {
168+
}
169+
}
170+
171+
throw new InvalidParameterTypeException($this->currentId, $e->getCode(), $parameter);
172+
}
173+
174+
$type = $type->getName();
175+
}
159176

160177
if ($value instanceof Reference) {
161178
if (!$this->container->has($value = (string) $value)) {
@@ -266,7 +283,7 @@ private function checkType(Definition $checkedDefinition, $value, \ReflectionPar
266283
return;
267284
}
268285

269-
$checkFunction = sprintf('is_%s', $parameter->getType()->getName());
286+
$checkFunction = sprintf('is_%s', $type);
270287

271288
if (!$parameter->getType()->isBuiltin() || !$checkFunction($value)) {
272289
throw new InvalidParameterTypeException($this->currentId, \is_object($value) ? $class : \gettype($value), $parameter);

Dumper/Preloader.php

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public static function preload(array $classes)
4848
}
4949
}
5050

51-
private static function doPreload(string $class, array &$preloaded)
51+
private static function doPreload(string $class, array &$preloaded): void
5252
{
5353
if (isset($preloaded[$class]) || \in_array($class, ['self', 'static', 'parent'], true)) {
5454
return;
@@ -68,9 +68,7 @@ private static function doPreload(string $class, array &$preloaded)
6868

6969
if (\PHP_VERSION_ID >= 70400) {
7070
foreach ($r->getProperties(\ReflectionProperty::IS_PUBLIC) as $p) {
71-
if (($t = $p->getType()) && !$t->isBuiltin()) {
72-
self::doPreload($t->getName(), $preloaded);
73-
}
71+
self::preloadType($p->getType(), $preloaded);
7472
}
7573
}
7674

@@ -84,17 +82,26 @@ private static function doPreload(string $class, array &$preloaded)
8482
}
8583
}
8684

87-
if (($t = $p->getType()) && !$t->isBuiltin()) {
88-
self::doPreload($t->getName(), $preloaded);
89-
}
85+
self::preloadType($p->getType(), $preloaded);
9086
}
9187

92-
if (($t = $m->getReturnType()) && !$t->isBuiltin()) {
93-
self::doPreload($t->getName(), $preloaded);
94-
}
88+
self::preloadType($p->getReturnType(), $preloaded);
9589
}
9690
} catch (\ReflectionException $e) {
9791
// ignore missing classes
9892
}
9993
}
94+
95+
private static function preloadType(?\ReflectionType $t, array &$preloaded): void
96+
{
97+
if (!$t || $t->isBuiltin()) {
98+
return;
99+
}
100+
101+
foreach ($t instanceof \ReflectionUnionType ? $t->getTypes() : [$t] as $t) {
102+
if (!$t->isBuiltin()) {
103+
self::doPreload($t instanceof \ReflectionNamedType ? $t->getName() : $t, $preloaded);
104+
}
105+
}
106+
}
100107
}

Exception/InvalidParameterTypeException.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ class InvalidParameterTypeException extends InvalidArgumentException
2121
{
2222
public function __construct(string $serviceId, string $type, \ReflectionParameter $parameter)
2323
{
24-
parent::__construct(sprintf('Invalid definition for service "%s": argument %d of "%s::%s" accepts "%s", "%s" passed.', $serviceId, 1 + $parameter->getPosition(), $parameter->getDeclaringClass()->getName(), $parameter->getDeclaringFunction()->getName(), $parameter->getType()->getName(), $type));
24+
$acceptedType = $parameter->getType();
25+
$acceptedType = $acceptedType instanceof \ReflectionNamedType ? $acceptedType->getName() : (string) $acceptedType;
26+
27+
parent::__construct(sprintf('Invalid definition for service "%s": argument %d of "%s::%s" accepts "%s", "%s" passed.', $serviceId, 1 + $parameter->getPosition(), $parameter->getDeclaringClass()->getName(), $parameter->getDeclaringFunction()->getName(), $acceptedType, $type), $type);
2528
}
2629
}

0 commit comments

Comments
 (0)