diff --git a/lib/Doctrine/Common/Proxy/ProxyGenerator.php b/lib/Doctrine/Common/Proxy/ProxyGenerator.php index ec904312f..1401bcbf7 100644 --- a/lib/Doctrine/Common/Proxy/ProxyGenerator.php +++ b/lib/Doctrine/Common/Proxy/ProxyGenerator.php @@ -860,7 +860,9 @@ public function __wakeup()$returnTypeHint */ private function generateCloneImpl(ClassMetadata $class) { - $hasParentClone = $class->getReflectionClass()->hasMethod('__clone'); + $reflectionClass = $class->getReflectionClass(); + $hasParentClone = $reflectionClass->hasMethod('__clone'); + $returnTypeHint = $hasParentClone ? $this->getMethodReturnType($reflectionClass->getMethod('__clone')) : ''; $inheritDoc = $hasParentClone ? '{@inheritDoc}' : ''; $callParentClone = $hasParentClone ? "\n parent::__clone();\n" : ''; @@ -868,7 +870,7 @@ private function generateCloneImpl(ClassMetadata $class) /** * $inheritDoc */ - public function __clone() + public function __clone()$returnTypeHint { \$this->__cloner__ && \$this->__cloner__->__invoke(\$this, '__clone', []); $callParentClone } diff --git a/tests/Doctrine/Tests/Common/Proxy/Php8MagicCloneClass.php b/tests/Doctrine/Tests/Common/Proxy/Php8MagicCloneClass.php new file mode 100644 index 000000000..68445c38a --- /dev/null +++ b/tests/Doctrine/Tests/Common/Proxy/Php8MagicCloneClass.php @@ -0,0 +1,10 @@ +generateProxyClass($this->createClassMetadata(FinalClass::class, [])); } + /** + * @requires PHP >= 8.0.0 + */ + public function testPhp8CloneWithVoidReturnType() + { + $className = Php8MagicCloneClass::class; + + if ( ! class_exists('Doctrine\Tests\Common\ProxyProxy\__CG__\Php8MagicCloneClass', false)) { + $metadata = $this->createClassMetadata($className, ['id']); + + $proxyGenerator = new ProxyGenerator(__DIR__ . '/generated', __NAMESPACE__ . 'Proxy'); + $this->generateAndRequire($proxyGenerator, $metadata); + } + + self::assertStringContainsString( + 'public function __clone(): void', + file_get_contents(__DIR__ . '/generated/__CG__DoctrineTestsCommonProxyPhp8MagicCloneClass.php') + ); + } + /** * @requires PHP >= 8.0.0 */