Skip to content

Commit

Permalink
Merge pull request #970 from deguif/fix-clone-void-return
Browse files Browse the repository at this point in the history
Support void return type for __clone magic method
  • Loading branch information
malarzm authored Aug 20, 2022
2 parents 26a2d2a + 0643ca2 commit 6a76bd2
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 2 deletions.
6 changes: 4 additions & 2 deletions lib/Doctrine/Common/Proxy/ProxyGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -860,15 +860,17 @@ 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" : '';

return <<<EOT
/**
* $inheritDoc
*/
public function __clone()
public function __clone()$returnTypeHint
{
\$this->__cloner__ && \$this->__cloner__->__invoke(\$this, '__clone', []);
$callParentClone }
Expand Down
10 changes: 10 additions & 0 deletions tests/Doctrine/Tests/Common/Proxy/Php8MagicCloneClass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Doctrine\Tests\Common\Proxy;

class Php8MagicCloneClass
{
public function __clone(): void
{
}
}
20 changes: 20 additions & 0 deletions tests/Doctrine/Tests/Common/Proxy/ProxyGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,26 @@ public function testFinalClassThrowsException()
$proxyGenerator->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
*/
Expand Down

0 comments on commit 6a76bd2

Please sign in to comment.