Skip to content

Commit

Permalink
support php 8.0 ReflectionUnionType parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
Kaleta committed Nov 4, 2021
1 parent b71cfde commit 7e2f15b
Showing 1 changed file with 30 additions and 17 deletions.
47 changes: 30 additions & 17 deletions Hydrator/ReadOnlyHydrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,7 @@ protected function getPhpForMethod(\ReflectionMethod $reflectionMethod, array $p
foreach ($reflectionMethod->getParameters() as $parameter) {
$parameters[] = $this->getPhpForParameter($parameter);
}

$signature .= implode(', ', $parameters) . ')';

$method = $reflectionMethod->name;
Expand Down Expand Up @@ -303,29 +304,41 @@ protected function getPhpForMethod(\ReflectionMethod $reflectionMethod, array $p
/**
* @param \ReflectionParameter $parameter
* @return string
* @throws \ReflectionException
*/
protected function getPhpForParameter(\ReflectionParameter $parameter)
{
$type = $parameter->getType();

$php = null;
if (
version_compare(PHP_VERSION, '7.1.0', '>=')
&& $parameter->hasType()
&& $type->allowsNull()
) {
$php .= '?';
$types = $parameter->getType();
if($types !== null && get_class($types) === "ReflectionUnionType") {
$types = $types->getTypes();
} else {
$types = [$types];
}

if ($type instanceof \ReflectionClass) {
$php .= $this->getFullQualifiedClassName($type->getName()) . ' ';
} elseif (
version_compare(PHP_VERSION, '7.0.0', '>=')
&& $parameter->hasType()
) {
$php .= static::extractNameFromReflexionType($parameter->getType()) . ' ';

$values = [];
/** @var \ReflectionUnionType[]|\ReflectionType|null $type */
foreach ($types as $type) {
$php = null;
if (
version_compare(PHP_VERSION, '7.1.0', '>=')
&& $parameter->hasType()
&& $type->allowsNull()
) {
$php .= '?';
}

if ($type !== null && (class_exists($type->getName()) || interface_exists($type->getName()))) {
$php .= $this->getFullQualifiedClassName($type->getName()) ;
} elseif (
version_compare(PHP_VERSION, '7.0.0', '>=')
&& $parameter->hasType()
) {
$php .= static::extractNameFromReflexionType($parameter->getType()) ;
}
$values[] = $php;
}

$php = implode('|', $values).' ';
if ($parameter->isPassedByReference()) {
$php .= '&';
}
Expand Down

0 comments on commit 7e2f15b

Please sign in to comment.