From 2e68470012f4d017b8aa7aee69994e3b153457af Mon Sep 17 00:00:00 2001 From: Artem Onyshchenko Date: Wed, 28 Apr 2021 09:19:57 +0300 Subject: [PATCH] feat(PhpUnit) - add php 8.0 compatibility part 2 - Update CodeHelper add support ReflectionUnionType return type - Update Mockery add support ReflectionUnionType return type --- src/Generator/Helper/CodeHelper.php | 10 +++++++++- src/Generator/Mock/Mockery.php | 10 +++++++++- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/src/Generator/Helper/CodeHelper.php b/src/Generator/Helper/CodeHelper.php index 64464e9..789f2d6 100644 --- a/src/Generator/Helper/CodeHelper.php +++ b/src/Generator/Helper/CodeHelper.php @@ -532,7 +532,15 @@ protected function getMethodReturnType(ReflectionMethod $reflectionMethod): stri } else { $returnType = $reflectionMethod->getReturnType(); - if (null !== $returnType) { + if ( + class_exists('ReflectionUnionType') && + $returnType instanceof \ReflectionUnionType + ) { + // use only first return type + // @TODO return all return types + $returnType = array_shift($returnType->getTypes()); + } + if ($returnType instanceof \ReflectionNamedType) { $returnType = $returnType->getName(); } } diff --git a/src/Generator/Mock/Mockery.php b/src/Generator/Mock/Mockery.php index f1bde5f..3dfca12 100644 --- a/src/Generator/Mock/Mockery.php +++ b/src/Generator/Mock/Mockery.php @@ -115,7 +115,15 @@ public function generateMockCode( } else { $returnType = $refMethod->getReturnType(); - if (null !== $returnType) { + if ( + class_exists('ReflectionUnionType') && + $returnType instanceof \ReflectionUnionType + ) { + // use only first return type + // @TODO return all return types + $returnType = array_shift($returnType->getTypes()); + } + if ($returnType instanceof \ReflectionNamedType) { $returnType = $returnType->getName(); } }