diff --git a/src/Generator.php b/src/Generator.php index 476b6f365..133a3de1d 100644 --- a/src/Generator.php +++ b/src/Generator.php @@ -173,6 +173,7 @@ private function addOperation(string $targetPath, string $templateName, array $v $variables['relative_path'] = $this->fileManager->relativizePath($targetPath); $variables['use_attributes'] = $this->phpCompatUtil->canUseAttributes(); $variables['use_typed_properties'] = $this->phpCompatUtil->canUseTypedProperties(); + $variables['use_union_types'] = $this->phpCompatUtil->canUseUnionTypes(); $templatePath = $templateName; if (!file_exists($templatePath)) { diff --git a/src/Util/PhpCompatUtil.php b/src/Util/PhpCompatUtil.php index eb34e9f45..b83bbfced 100644 --- a/src/Util/PhpCompatUtil.php +++ b/src/Util/PhpCompatUtil.php @@ -43,6 +43,13 @@ public function canUseTypedProperties(): bool return version_compare($version, '7.4', '>='); } + public function canUseUnionTypes(): bool + { + $version = $this->getPhpVersion(); + + return version_compare($version, '8alpha', '>='); + } + protected function getPhpVersion(): string { $rootDirectory = $this->fileManager->getRootDirectory(); diff --git a/tests/Util/PhpVersionTest.php b/tests/Util/PhpVersionTest.php index 952f2a0a2..c5c1ccbbf 100644 --- a/tests/Util/PhpVersionTest.php +++ b/tests/Util/PhpVersionTest.php @@ -26,28 +26,7 @@ class PhpVersionTest extends TestCase */ public function testUsesPhpPlatformFromComposerJsonFileForCanUseAttributes(string $version, bool $expectedResult): void { - $json = sprintf('{"platform-overrides": {"php": "%s"}}', $version); - - $mockFileManager = $this->createMock(FileManager::class); - $mockFileManager - ->expects(self::once()) - ->method('getRootDirectory') - ->willReturn('/test') - ; - - $mockFileManager - ->expects(self::once()) - ->method('fileExists') - ->with('/test/composer.lock') - ->willReturn(true) - ; - - $mockFileManager - ->expects(self::once()) - ->method('getFileContents') - ->with('/test/composer.lock') - ->willReturn($json) - ; + $mockFileManager = $this->mockFileManager(sprintf('{"platform-overrides": {"php": "%s"}}', $version)); $version = new PhpCompatUtil($mockFileManager); @@ -110,28 +89,7 @@ public function testFallBackToPhpVersionWithoutLockFile(): void public function testWithoutPlatformVersionSet(): void { - $json = '{"platform-overrides": {}}'; - - $mockFileManager = $this->createMock(FileManager::class); - $mockFileManager - ->expects(self::once()) - ->method('getRootDirectory') - ->willReturn('/test') - ; - - $mockFileManager - ->expects(self::once()) - ->method('fileExists') - ->with('/test/composer.lock') - ->willReturn(true) - ; - - $mockFileManager - ->expects(self::once()) - ->method('getFileContents') - ->with('/test/composer.lock') - ->willReturn($json) - ; + $mockFileManager = $this->mockFileManager('{"platform-overrides": {}}'); $util = new PhpCompatUtilTestFixture($mockFileManager); @@ -145,8 +103,58 @@ public function testWithoutPlatformVersionSet(): void */ public function testCanUseTypedProperties(string $version, bool $expectedResult): void { - $json = sprintf('{"platform-overrides": {"php": "%s"}}', $version); + $mockFileManager = $this->mockFileManager(sprintf('{"platform-overrides": {"php": "%s"}}', $version)); + + $version = new PhpCompatUtil($mockFileManager); + + $result = $version->canUseTypedProperties(); + + self::assertSame($expectedResult, $result); + } + + public function phpVersionForTypedPropertiesDataProvider(): \Generator + { + yield ['8', true]; + yield ['8.0.1', true]; + yield ['8RC1', true]; + yield ['7.4', true]; + yield ['7.4.6', true]; + yield ['7', false]; + yield ['7.0', false]; + yield ['5.7', false]; + } + + /** + * @dataProvider phpVersionForUnionTypesDataProvider + */ + public function testCanUseUnionTypes(string $version, bool $expectedResult): void + { + $mockFileManager = $this->mockFileManager(sprintf('{"platform-overrides": {"php": "%s"}}', $version)); + + $version = new PhpCompatUtil($mockFileManager); + + $result = $version->canUseUnionTypes(); + + self::assertSame($expectedResult, $result); + } + + public function phpVersionForUnionTypesDataProvider(): \Generator + { + yield ['8', true]; + yield ['8.0.1', true]; + yield ['8RC1', true]; + yield ['7.4', false]; + yield ['7.4.6', false]; + yield ['7', false]; + yield ['7.0', false]; + yield ['5.7', false]; + } + /** + * @return \PHPUnit\Framework\MockObject\MockObject|FileManager + */ + private function mockFileManager(string $json) + { $mockFileManager = $this->createMock(FileManager::class); $mockFileManager ->expects(self::once()) @@ -168,23 +176,7 @@ public function testCanUseTypedProperties(string $version, bool $expectedResult) ->willReturn($json) ; - $version = new PhpCompatUtil($mockFileManager); - - $result = $version->canUseTypedProperties(); - - self::assertSame($expectedResult, $result); - } - - public function phpVersionForTypedPropertiesDataProvider(): \Generator - { - yield ['8', true]; - yield ['8.0.1', true]; - yield ['8RC1', true]; - yield ['7.4', true]; - yield ['7.4.6', true]; - yield ['7', false]; - yield ['7.0', false]; - yield ['5.7', false]; + return $mockFileManager; } }