From 137f9b81b1ac995bdf289276ff1f7b39fab41731 Mon Sep 17 00:00:00 2001 From: Ondrej Mirtes Date: Tue, 27 Sep 2022 18:02:32 +0200 Subject: [PATCH] Use readonly properties --- .github/workflows/e2e-tests.yml | 6 ++--- .github/workflows/lint.yml | 14 +++++------ .github/workflows/phar.yml | 4 +-- .github/workflows/static-analysis.yml | 14 +++++------ .github/workflows/tests.yml | 20 +++++++-------- build/rector-downgrade.php | 5 ++++ src/Analyser/NodeScopeResolver.php | 36 +++++++++++++-------------- 7 files changed, 51 insertions(+), 48 deletions(-) diff --git a/.github/workflows/e2e-tests.yml b/.github/workflows/e2e-tests.yml index 5d7afa2fa50..741654b4149 100644 --- a/.github/workflows/e2e-tests.yml +++ b/.github/workflows/e2e-tests.yml @@ -33,8 +33,6 @@ jobs: strategy: fail-fast: false matrix: - php-version: - - "8.0" operating-system: [ubuntu-latest, windows-latest] steps: @@ -45,7 +43,7 @@ jobs: uses: "shivammathur/setup-php@v2" with: coverage: "none" - php-version: "${{ matrix.php-version }}" + php-version: "8.1" extensions: mbstring ini-values: memory_limit=256M @@ -82,7 +80,7 @@ jobs: uses: "shivammathur/setup-php@v2" with: coverage: "none" - php-version: "8.0" + php-version: "8.1" tools: ${{ matrix.tools }} extensions: ${{ matrix.extensions }} diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index 1a029d2dee4..4b857810f04 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -49,7 +49,7 @@ jobs: run: "composer install --no-interaction --no-progress" - name: "Install PHP for code transform" - if: matrix.php-version != '8.0' && matrix.php-version != '8.1' && matrix.php-version != '8.2' + if: matrix.php-version != '8.1' && matrix.php-version != '8.2' uses: "shivammathur/setup-php@v2" with: coverage: "none" @@ -58,25 +58,25 @@ jobs: - name: "Rector downgrade cache key" id: rector-cache-key - if: matrix.php-version != '8.0' && matrix.php-version != '8.1' && matrix.php-version != '8.2' + if: matrix.php-version != '8.1' && matrix.php-version != '8.2' run: | echo "::set-output name=sha::$(php build/rector-cache-files-hash.php)" - name: "Rector downgrade cache" - if: matrix.php-version != '8.0' && matrix.php-version != '8.1' && matrix.php-version != '8.2' + if: matrix.php-version != '8.1' && matrix.php-version != '8.2' uses: actions/cache@v3 with: path: ./tmp/rectorCache.php - key: "rector-v1-lint-${{ hashFiles('composer.lock', 'build/rector-downgrade.php') }}-${{ matrix.php-version }}-${{ steps.rector-cache-key.outputs.sha }}" + key: "rector-v2-lint-${{ hashFiles('composer.lock', 'build/rector-downgrade.php') }}-${{ matrix.php-version }}-${{ steps.rector-cache-key.outputs.sha }}" restore-keys: | - rector-v1-lint-${{ hashFiles('composer.lock', 'build/rector-downgrade.php') }}-${{ matrix.php-version }}- + rector-v2-lint-${{ hashFiles('composer.lock', 'build/rector-downgrade.php') }}-${{ matrix.php-version }}- - name: "Transform source code" - if: matrix.php-version != '8.0' && matrix.php-version != '8.1' && matrix.php-version != '8.2' + if: matrix.php-version != '8.1' && matrix.php-version != '8.2' run: "build/transform-source ${{ matrix.php-version }}" - name: "Reinstall matrix PHP version" - if: matrix.php-version != '8.0' && matrix.php-version != '8.1' && matrix.php-version != '8.2' + if: matrix.php-version != '8.1' && matrix.php-version != '8.2' uses: "shivammathur/setup-php@v2" with: coverage: "none" diff --git a/.github/workflows/phar.yml b/.github/workflows/phar.yml index 2277f2f0723..dfcafa819cd 100644 --- a/.github/workflows/phar.yml +++ b/.github/workflows/phar.yml @@ -60,9 +60,9 @@ jobs: uses: actions/cache@v3 with: path: ./tmp/rectorCache.php - key: "rector-v1-phar-${{ hashFiles('composer.lock', 'build/rector-downgrade.php') }}-${{ steps.rector-cache-key.outputs.sha }}" + key: "rector-v2-phar-${{ hashFiles('composer.lock', 'build/rector-downgrade.php') }}-${{ steps.rector-cache-key.outputs.sha }}" restore-keys: | - rector-v1-phar-${{ hashFiles('composer.lock', 'build/rector-downgrade.php') }}- + rector-v2-phar-${{ hashFiles('composer.lock', 'build/rector-downgrade.php') }}- - name: "Prepare for PHAR compilation" working-directory: "compiler" diff --git a/.github/workflows/static-analysis.yml b/.github/workflows/static-analysis.yml index 85fb3ca0a3c..5c79bb3ccb0 100644 --- a/.github/workflows/static-analysis.yml +++ b/.github/workflows/static-analysis.yml @@ -56,7 +56,7 @@ jobs: run: "composer install --no-interaction --no-progress" - name: "Install PHP for code transform" - if: matrix.php-version != '8.0' && matrix.php-version != '8.1' && matrix.php-version != '8.2' + if: matrix.php-version != '8.1' && matrix.php-version != '8.2' uses: "shivammathur/setup-php@v2" with: coverage: "none" @@ -65,26 +65,26 @@ jobs: - name: "Rector downgrade cache key" id: rector-cache-key - if: matrix.php-version != '8.0' && matrix.php-version != '8.1' && matrix.php-version != '8.2' + if: matrix.php-version != '8.1' && matrix.php-version != '8.2' run: | echo "::set-output name=sha::$(php build/rector-cache-files-hash.php)" - name: "Rector downgrade cache" - if: matrix.php-version != '8.0' && matrix.php-version != '8.1' && matrix.php-version != '8.2' + if: matrix.php-version != '8.1' && matrix.php-version != '8.2' uses: actions/cache@v3 with: path: ./tmp/rectorCache.php - key: "rector-v1-sa-${{ matrix.script }}-${{ matrix.operating-system }}-${{ hashFiles('composer.lock', 'build/rector-downgrade.php') }}-${{ matrix.php-version }}-${{ steps.rector-cache-key.outputs.sha }}" + key: "rector-v2-sa-${{ matrix.script }}-${{ matrix.operating-system }}-${{ hashFiles('composer.lock', 'build/rector-downgrade.php') }}-${{ matrix.php-version }}-${{ steps.rector-cache-key.outputs.sha }}" restore-keys: | - rector-v1-sa-${{ matrix.script }}-${{ matrix.operating-system }}-${{ hashFiles('composer.lock', 'build/rector-downgrade.php') }}-${{ matrix.php-version }}- + rector-v2-sa-${{ matrix.script }}-${{ matrix.operating-system }}-${{ hashFiles('composer.lock', 'build/rector-downgrade.php') }}-${{ matrix.php-version }}- - name: "Transform source code" - if: matrix.php-version != '8.0' && matrix.php-version != '8.1' && matrix.php-version != '8.2' + if: matrix.php-version != '8.1' && matrix.php-version != '8.2' shell: bash run: "build/transform-source ${{ matrix.php-version }}" - name: "Reinstall matrix PHP version" - if: matrix.php-version != '8.0' && matrix.php-version != '8.1' && matrix.php-version != '8.2' + if: matrix.php-version != '8.1' && matrix.php-version != '8.2' uses: "shivammathur/setup-php@v2" with: coverage: "none" diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index b464ec28fb2..6d532bb9b9e 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -57,7 +57,7 @@ jobs: run: "composer install --no-interaction --no-progress" - name: "Install PHP for code transform" - if: matrix.php-version != '8.0' && matrix.php-version != '8.1' && matrix.php-version != '8.2' + if: matrix.php-version != '8.1' && matrix.php-version != '8.2' uses: "shivammathur/setup-php@v2" with: coverage: "none" @@ -66,26 +66,26 @@ jobs: - name: "Rector downgrade cache key" id: rector-cache-key - if: matrix.php-version != '8.0' && matrix.php-version != '8.1' && matrix.php-version != '8.2' + if: matrix.php-version != '8.1' && matrix.php-version != '8.2' run: | echo "::set-output name=sha::$(php build/rector-cache-files-hash.php)" - name: "Rector downgrade cache" - if: matrix.php-version != '8.0' && matrix.php-version != '8.1' && matrix.php-version != '8.2' + if: matrix.php-version != '8.1' && matrix.php-version != '8.2' uses: actions/cache@v3 with: path: ./tmp/rectorCache.php - key: "rector-v1-tests-${{ matrix.script }}-${{ matrix.operating-system }}-${{ hashFiles('composer.lock', 'build/rector-downgrade.php') }}-${{ matrix.php-version }}-${{ steps.rector-cache-key.outputs.sha }}" + key: "rector-v2-tests-${{ matrix.script }}-${{ matrix.operating-system }}-${{ hashFiles('composer.lock', 'build/rector-downgrade.php') }}-${{ matrix.php-version }}-${{ steps.rector-cache-key.outputs.sha }}" restore-keys: | - rector-v1-tests-${{ matrix.script }}-${{ matrix.operating-system }}-${{ hashFiles('composer.lock', 'build/rector-downgrade.php') }}-${{ matrix.php-version }}- + rector-v2-tests-${{ matrix.script }}-${{ matrix.operating-system }}-${{ hashFiles('composer.lock', 'build/rector-downgrade.php') }}-${{ matrix.php-version }}- - name: "Transform source code" - if: matrix.php-version != '8.0' && matrix.php-version != '8.1' && matrix.php-version != '8.2' + if: matrix.php-version != '8.1' && matrix.php-version != '8.2' shell: bash run: "build/transform-source ${{ matrix.php-version }}" - name: "Reinstall matrix PHP version" - if: matrix.php-version != '8.0' && matrix.php-version != '8.1' && matrix.php-version != '8.2' + if: matrix.php-version != '8.1' && matrix.php-version != '8.2' uses: "shivammathur/setup-php@v2" with: coverage: "none" @@ -187,7 +187,7 @@ jobs: - name: "Rector downgrade cache key" id: rector-cache-key - if: matrix.php-version != '8.0' && matrix.php-version != '8.1' && matrix.php-version != '8.2' + if: matrix.php-version != '8.1' && matrix.php-version != '8.2' run: | echo "::set-output name=sha::$(php build/rector-cache-files-hash.php)" @@ -195,9 +195,9 @@ jobs: uses: actions/cache@v3 with: path: ./tmp/rectorCache.php - key: "rector-v1-tests-old-${{ matrix.script }}-${{ matrix.operating-system }}-${{ hashFiles('composer.lock', 'build/rector-downgrade.php') }}-${{ matrix.php-version }}-${{ steps.rector-cache-key.outputs.sha }}" + key: "rector-v2-tests-old-${{ matrix.script }}-${{ matrix.operating-system }}-${{ hashFiles('composer.lock', 'build/rector-downgrade.php') }}-${{ matrix.php-version }}-${{ steps.rector-cache-key.outputs.sha }}" restore-keys: | - rector-v1-tests-old-${{ matrix.script }}-${{ matrix.operating-system }}-${{ hashFiles('composer.lock', 'build/rector-downgrade.php') }}-${{ matrix.php-version }}- + rector-v2-tests-old-${{ matrix.script }}-${{ matrix.operating-system }}-${{ hashFiles('composer.lock', 'build/rector-downgrade.php') }}-${{ matrix.php-version }}- - name: "Transform source code" shell: bash diff --git a/build/rector-downgrade.php b/build/rector-downgrade.php index e4f65721dc6..cd460fb679c 100644 --- a/build/rector-downgrade.php +++ b/build/rector-downgrade.php @@ -12,6 +12,7 @@ use Rector\DowngradePhp80\Rector\FunctionLike\DowngradeMixedTypeDeclarationRector; use Rector\DowngradePhp80\Rector\FunctionLike\DowngradeUnionTypeDeclarationRector; use Rector\DowngradePhp80\Rector\Property\DowngradeUnionTypeTypedPropertyRector; +use Rector\DowngradePhp81\Rector\Property\DowngradeReadonlyPropertyRector; return static function (RectorConfig $config): void { $parsePhpVersion = static function (string $version, int $defaultPatch = 0): int { @@ -27,6 +28,10 @@ $config->phpVersion($targetPhpVersionId); $config->skip(RectorCache::SKIP_PATHS); + if ($targetPhpVersionId < 80100) { + $config->rule(DowngradeReadonlyPropertyRector::class); + } + if ($targetPhpVersionId < 80000) { $config->rule(DowngradeTrailingCommasInParamUseRector::class); $config->rule(DowngradeNonCapturingCatchesRector::class); diff --git a/src/Analyser/NodeScopeResolver.php b/src/Analyser/NodeScopeResolver.php index 94a3453cc92..f8cbcb4ff0e 100644 --- a/src/Analyser/NodeScopeResolver.php +++ b/src/Analyser/NodeScopeResolver.php @@ -188,24 +188,24 @@ class NodeScopeResolver * @param array $earlyTerminatingFunctionCalls */ public function __construct( - private ReflectionProvider $reflectionProvider, - private InitializerExprTypeResolver $initializerExprTypeResolver, - private Reflector $reflector, - private ClassReflectionExtensionRegistryProvider $classReflectionExtensionRegistryProvider, - private Parser $parser, - private FileTypeMapper $fileTypeMapper, - private StubPhpDocProvider $stubPhpDocProvider, - private PhpVersion $phpVersion, - private PhpDocInheritanceResolver $phpDocInheritanceResolver, - private FileHelper $fileHelper, - private TypeSpecifier $typeSpecifier, - private DynamicThrowTypeExtensionProvider $dynamicThrowTypeExtensionProvider, - private ReadWritePropertiesExtensionProvider $readWritePropertiesExtensionProvider, - private bool $polluteScopeWithLoopInitialAssignments, - private bool $polluteScopeWithAlwaysIterableForeach, - private array $earlyTerminatingMethodCalls, - private array $earlyTerminatingFunctionCalls, - private bool $implicitThrows, + private readonly ReflectionProvider $reflectionProvider, + private readonly InitializerExprTypeResolver $initializerExprTypeResolver, + private readonly Reflector $reflector, + private readonly ClassReflectionExtensionRegistryProvider $classReflectionExtensionRegistryProvider, + private readonly Parser $parser, + private readonly FileTypeMapper $fileTypeMapper, + private readonly StubPhpDocProvider $stubPhpDocProvider, + private readonly PhpVersion $phpVersion, + private readonly PhpDocInheritanceResolver $phpDocInheritanceResolver, + private readonly FileHelper $fileHelper, + private readonly TypeSpecifier $typeSpecifier, + private readonly DynamicThrowTypeExtensionProvider $dynamicThrowTypeExtensionProvider, + private readonly ReadWritePropertiesExtensionProvider $readWritePropertiesExtensionProvider, + private readonly bool $polluteScopeWithLoopInitialAssignments, + private readonly bool $polluteScopeWithAlwaysIterableForeach, + private readonly array $earlyTerminatingMethodCalls, + private readonly array $earlyTerminatingFunctionCalls, + private readonly bool $implicitThrows, ) { $earlyTerminatingMethodNames = [];