Skip to content

Commit 5e6b374

Browse files
committed
Transform source based on target PHP version
1 parent 31842bd commit 5e6b374

File tree

7 files changed

+47
-17
lines changed

7 files changed

+47
-17
lines changed

.github/workflows/lint.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ jobs:
5353

5454
- name: "Transform source code"
5555
if: matrix.php-version != '7.4' && matrix.php-version != '8.0' && matrix.php-version != '8.1'
56-
run: build/transform-source
56+
run: "build/transform-source ${{ matrix.php-version }}"
5757

5858
- name: "Reinstall matrix PHP version"
5959
if: matrix.php-version != '7.4' && matrix.php-version != '8.0' && matrix.php-version != '8.1'

.github/workflows/static-analysis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ jobs:
5858
- name: "Transform source code"
5959
if: matrix.php-version != '7.4' && matrix.php-version != '8.0' && matrix.php-version != '8.1'
6060
shell: bash
61-
run: build/transform-source
61+
run: "build/transform-source ${{ matrix.php-version }}"
6262

6363
- name: "Reinstall matrix PHP version"
6464
if: matrix.php-version != '7.4' && matrix.php-version != '8.0' && matrix.php-version != '8.1'

.github/workflows/tests.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ jobs:
5858
- name: "Transform source code"
5959
if: matrix.php-version != '7.4' && matrix.php-version != '8.0' && matrix.php-version != '8.1'
6060
shell: bash
61-
run: build/transform-source
61+
run: "build/transform-source ${{ matrix.php-version }}"
6262

6363
- name: "Reinstall matrix PHP version"
6464
if: matrix.php-version != '7.4' && matrix.php-version != '8.0' && matrix.php-version != '8.1'
@@ -114,7 +114,7 @@ jobs:
114114

115115
- name: "Transform source code"
116116
shell: bash
117-
run: build/transform-source
117+
run: "build/transform-source ${{ matrix.php-version }}"
118118

119119
- name: "Reinstall matrix PHP version"
120120
uses: "shivammathur/setup-php@v2"

build/rector-downgrade-vendor.php

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,28 @@
11
<?php declare(strict_types=1);
22

33
use Rector\Core\Configuration\Option;
4-
use Rector\Core\ValueObject\PhpVersion;
54
use Rector\DowngradePhp72\Rector\FuncCall\DowngradePregUnmatchedAsNullConstantRector;
65
use Rector\DowngradePhp72\Rector\FuncCall\DowngradeStreamIsattyRector;
76
use Rector\DowngradePhp72\Rector\FunctionLike\DowngradeObjectTypeDeclarationRector;
87
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
98

109
return static function (ContainerConfigurator $containerConfigurator): void {
10+
$parsePhpVersion = static function (string $version, int $defaultPatch = 0): int {
11+
$parts = array_map('intval', explode('.', $version));
12+
13+
return $parts[0] * 10000 + $parts[1] * 100 + ($parts[2] ?? $defaultPatch);
14+
};
15+
$targetPhpVersion = getenv('TARGET_PHP_VERSION');
16+
$targetPhpVersionId = $parsePhpVersion($targetPhpVersion);
17+
1118
$parameters = $containerConfigurator->parameters();
12-
$parameters->set(Option::PHP_VERSION_FEATURES, PhpVersion::PHP_71);
19+
$parameters->set(Option::PHP_VERSION_FEATURES, $targetPhpVersionId);
1320

1421
$services = $containerConfigurator->services();
15-
$services->set(DowngradeObjectTypeDeclarationRector::class);
16-
$services->set(DowngradePregUnmatchedAsNullConstantRector::class);
17-
$services->set(DowngradeStreamIsattyRector::class);
22+
23+
if ($targetPhpVersionId < 70200) {
24+
$services->set(DowngradeObjectTypeDeclarationRector::class);
25+
$services->set(DowngradePregUnmatchedAsNullConstantRector::class);
26+
$services->set(DowngradeStreamIsattyRector::class);
27+
}
1828
};

build/rector-downgrade.php

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,27 @@
11
<?php declare(strict_types=1);
22

33
use Rector\Core\Configuration\Option;
4-
use Rector\Core\ValueObject\PhpVersion;
54
use Rector\DowngradePhp72\Rector\FunctionLike\DowngradeObjectTypeDeclarationRector;
65
use Rector\DowngradePhp73\Rector\FuncCall\DowngradeTrailingCommasInFunctionCallsRector;
76
use Rector\DowngradePhp74\Rector\Coalesce\DowngradeNullCoalescingOperatorRector;
87
use Rector\DowngradePhp74\Rector\ArrowFunction\ArrowFunctionToAnonymousFunctionRector;
98
use Rector\DowngradePhp74\Rector\Property\DowngradeTypedPropertyRector;
109
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
1110

11+
12+
1213
return static function (ContainerConfigurator $containerConfigurator): void {
14+
$parsePhpVersion = static function (string $version, int $defaultPatch = 0): int {
15+
$parts = array_map('intval', explode('.', $version));
16+
17+
return $parts[0] * 10000 + $parts[1] * 100 + ($parts[2] ?? $defaultPatch);
18+
};
19+
$targetPhpVersion = getenv('TARGET_PHP_VERSION');
20+
$targetPhpVersionId = $parsePhpVersion($targetPhpVersion);
21+
1322
$parameters = $containerConfigurator->parameters();
1423

15-
$parameters->set(Option::PHP_VERSION_FEATURES, PhpVersion::PHP_71);
24+
$parameters->set(Option::PHP_VERSION_FEATURES, $targetPhpVersionId);
1625
$parameters->set(Option::SKIP, [
1726
'tests/*/data/*',
1827
'tests/PHPStan/Analyser/traits/*',
@@ -23,9 +32,18 @@
2332
]);
2433

2534
$services = $containerConfigurator->services();
26-
$services->set(DowngradeTypedPropertyRector::class);
27-
$services->set(DowngradeTrailingCommasInFunctionCallsRector::class);
28-
$services->set(DowngradeObjectTypeDeclarationRector::class);
29-
$services->set(DowngradeNullCoalescingOperatorRector::class);
30-
$services->set(ArrowFunctionToAnonymousFunctionRector::class);
35+
36+
if ($targetPhpVersionId < 70400) {
37+
$services->set(DowngradeTypedPropertyRector::class);
38+
$services->set(DowngradeNullCoalescingOperatorRector::class);
39+
$services->set(ArrowFunctionToAnonymousFunctionRector::class);
40+
}
41+
42+
if ($targetPhpVersionId < 70300) {
43+
$services->set(DowngradeTrailingCommasInFunctionCallsRector::class);
44+
}
45+
46+
if ($targetPhpVersionId < 70200) {
47+
$services->set(DowngradeObjectTypeDeclarationRector::class);
48+
}
3149
};

build/transform-source

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ set -o errexit
44
set -o pipefail
55
set -o nounset
66

7+
export TARGET_PHP_VERSION=$1
8+
79
vendor/bin/rector process src tests/PHPStan tests/e2e -c build/rector-downgrade.php
810

911
vendor/bin/rector process vendor/symfony vendor/nette -c build/rector-downgrade-vendor.php || true

compiler/src/Console/CompileCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ private function patchFile(OutputInterface $output, string $originalFile, string
258258
private function transformSource(): void
259259
{
260260
chdir(__DIR__ . '/../../..');
261-
exec(escapeshellarg(__DIR__ . '/../../../build/transform-source'), $outputLines, $exitCode);
261+
exec(escapeshellarg(__DIR__ . '/../../../build/transform-source') . ' 7.1', $outputLines, $exitCode);
262262
if ($exitCode === 0) {
263263
return;
264264
}

0 commit comments

Comments
 (0)