Skip to content

Commit

Permalink
support php 8.0 ReflectionUnionType methon parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
Kaleta committed Feb 3, 2022
1 parent 7b5c505 commit 110bf3a
Showing 1 changed file with 14 additions and 8 deletions.
22 changes: 14 additions & 8 deletions Hydrator/ReadOnlyHydrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -333,29 +333,35 @@ protected function getPhpForParameter(\ReflectionParameter $parameter)
}

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

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()) ;
$php = $this->getFullQualifiedClassName($type->getName()) ;
}

if($type->getName() === 'null') {
$hasNull = true;
}

$values[] = $php;
}

$php = implode('|', $values).' ';
if($needsNull && !$hasNull) {
$php = '?'.$php;
}

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

0 comments on commit 110bf3a

Please sign in to comment.