Skip to content

Commit 6a76bd2

Browse files
authored
Merge pull request #970 from deguif/fix-clone-void-return
Support void return type for __clone magic method
2 parents 26a2d2a + 0643ca2 commit 6a76bd2

File tree

3 files changed

+34
-2
lines changed

3 files changed

+34
-2
lines changed

lib/Doctrine/Common/Proxy/ProxyGenerator.php

+4-2
Original file line numberDiff line numberDiff line change
@@ -860,15 +860,17 @@ public function __wakeup()$returnTypeHint
860860
*/
861861
private function generateCloneImpl(ClassMetadata $class)
862862
{
863-
$hasParentClone = $class->getReflectionClass()->hasMethod('__clone');
863+
$reflectionClass = $class->getReflectionClass();
864+
$hasParentClone = $reflectionClass->hasMethod('__clone');
865+
$returnTypeHint = $hasParentClone ? $this->getMethodReturnType($reflectionClass->getMethod('__clone')) : '';
864866
$inheritDoc = $hasParentClone ? '{@inheritDoc}' : '';
865867
$callParentClone = $hasParentClone ? "\n parent::__clone();\n" : '';
866868

867869
return <<<EOT
868870
/**
869871
* $inheritDoc
870872
*/
871-
public function __clone()
873+
public function __clone()$returnTypeHint
872874
{
873875
\$this->__cloner__ && \$this->__cloner__->__invoke(\$this, '__clone', []);
874876
$callParentClone }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace Doctrine\Tests\Common\Proxy;
4+
5+
class Php8MagicCloneClass
6+
{
7+
public function __clone(): void
8+
{
9+
}
10+
}

tests/Doctrine/Tests/Common/Proxy/ProxyGeneratorTest.php

+20
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,26 @@ public function testFinalClassThrowsException()
405405
$proxyGenerator->generateProxyClass($this->createClassMetadata(FinalClass::class, []));
406406
}
407407

408+
/**
409+
* @requires PHP >= 8.0.0
410+
*/
411+
public function testPhp8CloneWithVoidReturnType()
412+
{
413+
$className = Php8MagicCloneClass::class;
414+
415+
if ( ! class_exists('Doctrine\Tests\Common\ProxyProxy\__CG__\Php8MagicCloneClass', false)) {
416+
$metadata = $this->createClassMetadata($className, ['id']);
417+
418+
$proxyGenerator = new ProxyGenerator(__DIR__ . '/generated', __NAMESPACE__ . 'Proxy');
419+
$this->generateAndRequire($proxyGenerator, $metadata);
420+
}
421+
422+
self::assertStringContainsString(
423+
'public function __clone(): void',
424+
file_get_contents(__DIR__ . '/generated/__CG__DoctrineTestsCommonProxyPhp8MagicCloneClass.php')
425+
);
426+
}
427+
408428
/**
409429
* @requires PHP >= 8.0.0
410430
*/

0 commit comments

Comments
 (0)