Skip to content

Commit 8a295a3

Browse files
committed
expose var use_typed_properties to all templates
1 parent 203699d commit 8a295a3

File tree

3 files changed

+62
-62
lines changed

3 files changed

+62
-62
lines changed

src/Generator.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ private function addOperation(string $targetPath, string $templateName, array $v
173173
$variables['relative_path'] = $this->fileManager->relativizePath($targetPath);
174174
$variables['use_attributes'] = $this->phpCompatUtil->canUseAttributes();
175175
$variables['use_typed_properties'] = $this->phpCompatUtil->canUseTypedProperties();
176+
$variables['use_union_types'] = $this->phpCompatUtil->canUseUnionTypes();
176177

177178
$templatePath = $templateName;
178179
if (!file_exists($templatePath)) {

src/Util/PhpCompatUtil.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,13 @@ public function canUseTypedProperties(): bool
4343
return version_compare($version, '7.4', '>=');
4444
}
4545

46+
public function canUseUnionTypes(): bool
47+
{
48+
$version = $this->getPhpVersion();
49+
50+
return version_compare($version, '8alpha', '>=');
51+
}
52+
4653
protected function getPhpVersion(): string
4754
{
4855
$rootDirectory = $this->fileManager->getRootDirectory();

tests/Util/PhpVersionTest.php

Lines changed: 54 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -26,28 +26,7 @@ class PhpVersionTest extends TestCase
2626
*/
2727
public function testUsesPhpPlatformFromComposerJsonFileForCanUseAttributes(string $version, bool $expectedResult): void
2828
{
29-
$json = sprintf('{"platform-overrides": {"php": "%s"}}', $version);
30-
31-
$mockFileManager = $this->createMock(FileManager::class);
32-
$mockFileManager
33-
->expects(self::once())
34-
->method('getRootDirectory')
35-
->willReturn('/test')
36-
;
37-
38-
$mockFileManager
39-
->expects(self::once())
40-
->method('fileExists')
41-
->with('/test/composer.lock')
42-
->willReturn(true)
43-
;
44-
45-
$mockFileManager
46-
->expects(self::once())
47-
->method('getFileContents')
48-
->with('/test/composer.lock')
49-
->willReturn($json)
50-
;
29+
$mockFileManager = $this->mockFileManager(sprintf('{"platform-overrides": {"php": "%s"}}', $version));
5130

5231
$version = new PhpCompatUtil($mockFileManager);
5332

@@ -110,28 +89,7 @@ public function testFallBackToPhpVersionWithoutLockFile(): void
11089

11190
public function testWithoutPlatformVersionSet(): void
11291
{
113-
$json = '{"platform-overrides": {}}';
114-
115-
$mockFileManager = $this->createMock(FileManager::class);
116-
$mockFileManager
117-
->expects(self::once())
118-
->method('getRootDirectory')
119-
->willReturn('/test')
120-
;
121-
122-
$mockFileManager
123-
->expects(self::once())
124-
->method('fileExists')
125-
->with('/test/composer.lock')
126-
->willReturn(true)
127-
;
128-
129-
$mockFileManager
130-
->expects(self::once())
131-
->method('getFileContents')
132-
->with('/test/composer.lock')
133-
->willReturn($json)
134-
;
92+
$mockFileManager = $this->mockFileManager('{"platform-overrides": {}}');
13593

13694
$util = new PhpCompatUtilTestFixture($mockFileManager);
13795

@@ -145,8 +103,58 @@ public function testWithoutPlatformVersionSet(): void
145103
*/
146104
public function testCanUseTypedProperties(string $version, bool $expectedResult): void
147105
{
148-
$json = sprintf('{"platform-overrides": {"php": "%s"}}', $version);
106+
$mockFileManager = $this->mockFileManager(sprintf('{"platform-overrides": {"php": "%s"}}', $version));
107+
108+
$version = new PhpCompatUtil($mockFileManager);
109+
110+
$result = $version->canUseTypedProperties();
111+
112+
self::assertSame($expectedResult, $result);
113+
}
114+
115+
public function phpVersionForTypedPropertiesDataProvider(): \Generator
116+
{
117+
yield ['8', true];
118+
yield ['8.0.1', true];
119+
yield ['8RC1', true];
120+
yield ['7.4', true];
121+
yield ['7.4.6', true];
122+
yield ['7', false];
123+
yield ['7.0', false];
124+
yield ['5.7', false];
125+
}
126+
127+
/**
128+
* @dataProvider phpVersionForUnionTypesDataProvider
129+
*/
130+
public function testCanUseUnionTypes(string $version, bool $expectedResult): void
131+
{
132+
$mockFileManager = $this->mockFileManager(sprintf('{"platform-overrides": {"php": "%s"}}', $version));
133+
134+
$version = new PhpCompatUtil($mockFileManager);
135+
136+
$result = $version->canUseUnionTypes();
137+
138+
self::assertSame($expectedResult, $result);
139+
}
140+
141+
public function phpVersionForUnionTypesDataProvider(): \Generator
142+
{
143+
yield ['8', true];
144+
yield ['8.0.1', true];
145+
yield ['8RC1', true];
146+
yield ['7.4', false];
147+
yield ['7.4.6', false];
148+
yield ['7', false];
149+
yield ['7.0', false];
150+
yield ['5.7', false];
151+
}
149152

153+
/**
154+
* @return \PHPUnit\Framework\MockObject\MockObject|FileManager
155+
*/
156+
private function mockFileManager(string $json)
157+
{
150158
$mockFileManager = $this->createMock(FileManager::class);
151159
$mockFileManager
152160
->expects(self::once())
@@ -168,23 +176,7 @@ public function testCanUseTypedProperties(string $version, bool $expectedResult)
168176
->willReturn($json)
169177
;
170178

171-
$version = new PhpCompatUtil($mockFileManager);
172-
173-
$result = $version->canUseTypedProperties();
174-
175-
self::assertSame($expectedResult, $result);
176-
}
177-
178-
public function phpVersionForTypedPropertiesDataProvider(): \Generator
179-
{
180-
yield ['8', true];
181-
yield ['8.0.1', true];
182-
yield ['8RC1', true];
183-
yield ['7.4', true];
184-
yield ['7.4.6', true];
185-
yield ['7', false];
186-
yield ['7.0', false];
187-
yield ['5.7', false];
179+
return $mockFileManager;
188180
}
189181
}
190182

0 commit comments

Comments
 (0)