Skip to content

Commit

Permalink
Fix deprecation notice for ReflectionType::__toString() in php 7.4
Browse files Browse the repository at this point in the history
Add a method that does the exact same thing, as a simple workaround.

Fixes #23
  • Loading branch information
TysonAndre committed Jul 27, 2019
1 parent aaaf9b6 commit 802d75a
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion lib/Dispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down Expand Up @@ -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;
}

}

0 comments on commit 802d75a

Please sign in to comment.