diff --git a/src/Framework/MockObject/Api/MockedCloneMethodWithVoidReturnType.php b/src/Framework/MockObject/Api/MockedCloneMethodWithVoidReturnType.php deleted file mode 100644 index adbf82f3949..00000000000 --- a/src/Framework/MockObject/Api/MockedCloneMethodWithVoidReturnType.php +++ /dev/null @@ -1,21 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ -namespace PHPUnit\Framework\MockObject; - -/** - * @internal This trait is not covered by the backward compatibility promise for PHPUnit - */ -trait MockedCloneMethodWithVoidReturnType -{ - public function __clone(): void - { - $this->__phpunit_invocationMocker = clone $this->__phpunit_getInvocationHandler(); - } -} diff --git a/src/Framework/MockObject/Api/MockedCloneMethodWithoutReturnType.php b/src/Framework/MockObject/Api/MockedCloneMethodWithoutReturnType.php deleted file mode 100644 index 598d5006c6b..00000000000 --- a/src/Framework/MockObject/Api/MockedCloneMethodWithoutReturnType.php +++ /dev/null @@ -1,21 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ -namespace PHPUnit\Framework\MockObject; - -/** - * @internal This trait is not covered by the backward compatibility promise for PHPUnit - */ -trait MockedCloneMethodWithoutReturnType -{ - public function __clone() - { - $this->__phpunit_invocationMocker = clone $this->__phpunit_getInvocationHandler(); - } -} diff --git a/src/Framework/MockObject/Api/UnmockedCloneMethodWithVoidReturnType.php b/src/Framework/MockObject/Api/UnmockedCloneMethodWithVoidReturnType.php deleted file mode 100644 index af90e4fe9e9..00000000000 --- a/src/Framework/MockObject/Api/UnmockedCloneMethodWithVoidReturnType.php +++ /dev/null @@ -1,23 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ -namespace PHPUnit\Framework\MockObject; - -/** - * @internal This trait is not covered by the backward compatibility promise for PHPUnit - */ -trait UnmockedCloneMethodWithVoidReturnType -{ - public function __clone(): void - { - $this->__phpunit_invocationMocker = clone $this->__phpunit_getInvocationHandler(); - - parent::__clone(); - } -} diff --git a/src/Framework/MockObject/Api/UnmockedCloneMethodWithoutReturnType.php b/src/Framework/MockObject/Api/UnmockedCloneMethodWithoutReturnType.php deleted file mode 100644 index c9980c62f04..00000000000 --- a/src/Framework/MockObject/Api/UnmockedCloneMethodWithoutReturnType.php +++ /dev/null @@ -1,23 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ -namespace PHPUnit\Framework\MockObject; - -/** - * @internal This trait is not covered by the backward compatibility promise for PHPUnit - */ -trait UnmockedCloneMethodWithoutReturnType -{ - public function __clone() - { - $this->__phpunit_invocationMocker = clone $this->__phpunit_getInvocationHandler(); - - parent::__clone(); - } -} diff --git a/src/Framework/MockObject/Generator.php b/src/Framework/MockObject/Generator.php index 1ae58cf640b..0d1417de6ff 100644 --- a/src/Framework/MockObject/Generator.php +++ b/src/Framework/MockObject/Generator.php @@ -63,6 +63,58 @@ */ final class Generator { + private const MOCKED_CLONE_METHOD_WITH_VOID_RETURN_TYPE_TRAIT = <<<'EOT' +namespace PHPUnit\Framework\MockObject; + +trait MockedCloneMethodWithVoidReturnType +{ + public function __clone(): void + { + $this->__phpunit_invocationMocker = clone $this->__phpunit_getInvocationHandler(); + } +} +EOT; + + private const MOCKED_CLONE_METHOD_WITHOUT_RETURN_TYPE_TRAIT = <<<'EOT' +namespace PHPUnit\Framework\MockObject; + +trait MockedCloneMethodWithoutReturnType +{ + public function __clone() + { + $this->__phpunit_invocationMocker = clone $this->__phpunit_getInvocationHandler(); + } +} +EOT; + + private const UNMOCKED_CLONE_METHOD_WITH_VOID_RETURN_TYPE_TRAIT = <<<'EOT' +namespace PHPUnit\Framework\MockObject; + +trait UnmockedCloneMethodWithVoidReturnType +{ + public function __clone(): void + { + $this->__phpunit_invocationMocker = clone $this->__phpunit_getInvocationHandler(); + + parent::__clone(); + } +} +EOT; + + private const UNMOCKED_CLONE_METHOD_WITHOUT_RETURN_TYPE_TRAIT = <<<'EOT' +namespace PHPUnit\Framework\MockObject; + +trait UnmockedCloneMethodWithoutReturnType +{ + public function __clone() + { + $this->__phpunit_invocationMocker = clone $this->__phpunit_getInvocationHandler(); + + parent::__clone(); + } +} +EOT; + /** * @var array */ @@ -954,19 +1006,11 @@ private function generateMock($type, ?array $explicitMethods, string $mockClassN $cloneTrait = ''; if ($mockedCloneMethod) { - if (PHP_MAJOR_VERSION >= 8) { - $cloneTrait = PHP_EOL . ' use \PHPUnit\Framework\MockObject\MockedCloneMethodWithVoidReturnType;'; - } else { - $cloneTrait = PHP_EOL . ' use \PHPUnit\Framework\MockObject\MockedCloneMethodWithoutReturnType;'; - } + $cloneTrait = $this->mockedCloneMethod(); } if ($unmockedCloneMethod) { - if (PHP_MAJOR_VERSION >= 8) { - $cloneTrait = PHP_EOL . ' use \PHPUnit\Framework\MockObject\UnmockedCloneMethodWithVoidReturnType;'; - } else { - $cloneTrait = PHP_EOL . ' use \PHPUnit\Framework\MockObject\UnmockedCloneMethodWithoutReturnType;'; - } + $cloneTrait = $this->unmockedCloneMethod(); } $classTemplate->setVar( @@ -1107,4 +1151,38 @@ private function isConstructor(ReflectionMethod $method): bool return $methodName === $className; } + + private function mockedCloneMethod(): string + { + if (PHP_MAJOR_VERSION >= 8) { + if (!trait_exists('\PHPUnit\Framework\MockObject\MockedCloneMethodWithVoidReturnType')) { + eval(self::MOCKED_CLONE_METHOD_WITH_VOID_RETURN_TYPE_TRAIT); + } + + return PHP_EOL . ' use \PHPUnit\Framework\MockObject\MockedCloneMethodWithVoidReturnType;'; + } + + if (!trait_exists('\PHPUnit\Framework\MockObject\MockedCloneMethodWithoutReturnType')) { + eval(self::MOCKED_CLONE_METHOD_WITHOUT_RETURN_TYPE_TRAIT); + } + + return PHP_EOL . ' use \PHPUnit\Framework\MockObject\MockedCloneMethodWithoutReturnType;'; + } + + private function unmockedCloneMethod(): string + { + if (PHP_MAJOR_VERSION >= 8) { + if (!trait_exists('\PHPUnit\Framework\MockObject\UnmockedCloneMethodWithVoidReturnType')) { + eval(self::UNMOCKED_CLONE_METHOD_WITH_VOID_RETURN_TYPE_TRAIT); + } + + return PHP_EOL . ' use \PHPUnit\Framework\MockObject\UnmockedCloneMethodWithVoidReturnType;'; + } + + if (!trait_exists('\PHPUnit\Framework\MockObject\UnmockedCloneMethodWithoutReturnType')) { + eval(self::UNMOCKED_CLONE_METHOD_WITHOUT_RETURN_TYPE_TRAIT); + } + + return PHP_EOL . ' use \PHPUnit\Framework\MockObject\UnmockedCloneMethodWithoutReturnType;'; + } }