From b2d773bf608b9fcada913e653a621dad65fe5cda Mon Sep 17 00:00:00 2001 From: Tyson Andre Date: Sat, 27 Jul 2019 13:47:47 -0400 Subject: [PATCH] Fix deprecation notice for ReflectionType::__toString() in php 7.4 Add a method that does the exact same thing, as a simple workaround. --- lib/Dispatcher.php | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/lib/Dispatcher.php b/lib/Dispatcher.php index afc6a3d..82e7600 100644 --- a/lib/Dispatcher.php +++ b/lib/Dispatcher.php @@ -124,7 +124,7 @@ public function dispatch($msg) // Does the parameter have a type hint? $param = $parameters[$position]; if ($param->hasType()) { - $class = (string)$param->getType(); + $class = self::stringFromReflectionType($param->getType()); $value = $this->mapper->map($value, new $class()); } } else if (is_array($value) && isset($docBlock)) { @@ -160,4 +160,26 @@ public function dispatch($msg) $result = $obj->$fn(...$args); return $result; } + + /** + * Converts the reflection type to a string representing that type. + * This avoids a deprecation notice in php 7.4, while behaving identically to __toString(). + */ + private static function stringFromReflectionType( + \ReflectionType $reflection_type = null + ) : string { + if (!$reflection_type) { + return ''; + } + if ($reflection_type instanceof \ReflectionNamedType) { + $reflection_type_string = $reflection_type->getName(); + if ($reflection_type->allowsNull()) { + return "?" . $reflection_type_string; + } + return $reflection_type_string; + } + // Unreachable in php 7.1+. Note that ReflectionType::__toString() is deprecated in php 7.4. + return (string)$reflection_type; + } + }