From cbe48f7add8c9117ef076512a1947dfaf97e815f Mon Sep 17 00:00:00 2001 From: Oskar Stark Date: Tue, 13 Dec 2022 10:56:18 +0100 Subject: [PATCH] Enhancement: Add rector config file for easy migration (#542) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Enhancement: Add rector config file for easy migration * Update README.md Co-authored-by: Pierre du Plessis * Update README.md Co-authored-by: Andreas Möller * Update rector-migrate.php Co-authored-by: Andreas Möller * Update README.md Co-authored-by: Andreas Möller * Add workflow * Adjust .gitignore file * Add makre target * Fix * Fix * add all properties * Fix: Use .yaml instead of .yml as extension * Remove not working code * Update rector-migrate.php * Fix: Indentation * Fix: Quote * Fix: Ignore truthy rule for 'on' * Fix: Run 'make cs' * Enhancement: Use array_map() * Fix: Avoid unnecessary imports Co-authored-by: Pierre du Plessis Co-authored-by: Andreas Möller --- .github/dependabot.yml | 12 +++ .github/workflows/rector.yaml | 52 +++++++++++ .gitignore | 1 + Makefile | 4 + README.md | 31 +++++++ rector-migrate.php | 160 ++++++++++++++++++++++++++++++++ vendor-bin/rector/composer.json | 13 +++ vendor-bin/rector/composer.lock | 139 +++++++++++++++++++++++++++ 8 files changed, 412 insertions(+) create mode 100644 .github/workflows/rector.yaml create mode 100644 rector-migrate.php create mode 100644 vendor-bin/rector/composer.json create mode 100644 vendor-bin/rector/composer.lock diff --git a/.github/dependabot.yml b/.github/dependabot.yml index f53d0f2412..baa3fe3b7e 100644 --- a/.github/dependabot.yml +++ b/.github/dependabot.yml @@ -39,6 +39,18 @@ updates: interval: "monthly" versioning-strategy: "increase" + - commit-message: + include: "scope" + prefix: "composer" + directory: "/vendor-bin/rector" + labels: + - "dependency" + open-pull-requests-limit: 10 + package-ecosystem: "composer" + schedule: + interval: "monthly" + versioning-strategy: "increase" + - commit-message: include: "scope" prefix: "github-actions" diff --git a/.github/workflows/rector.yaml b/.github/workflows/rector.yaml new file mode 100644 index 0000000000..f6f338300a --- /dev/null +++ b/.github/workflows/rector.yaml @@ -0,0 +1,52 @@ +on: # yamllint disable-line rule:truthy + pull_request: ~ + push: + branches: + - "main" + - "[0-9].*" + +name: "Rector" + +jobs: + rector: + name: "Rector" + + runs-on: "ubuntu-latest" + + strategy: + matrix: + php-version: + - "8.1" + + steps: + - name: "Checkout code" + uses: "actions/checkout@v3" + + - name: "Set up PHP" + uses: "shivammathur/setup-php@v2" + with: + coverage: "none" + extensions: "intl" + php-version: "${{ matrix.php-version }}" + + - name: "Determine composer cache directory" + id: "composer-cache" + run: "echo \"directory=$(composer config cache-dir)\" >> $GITHUB_OUTPUT" + + - name: "Cache dependencies installed with composer" + uses: "actions/cache@v3" + with: + path: "${{ steps.composer-cache.outputs.directory }}" + key: "composer-${{ runner.os }}-${{ matrix.php-version }}-${{ hashFiles('composer.*') }}" + restore-keys: | + composer-${{ runner.os }}-${{ matrix.php-version }}- + composer-${{ runner.os }}- + composer- + + - name: "Download dependencies" + run: | + composer update --no-interaction --no-progress --optimize-autoloader + composer bin rector install --no-interaction --no-progress --optimize-autoloader + + - name: "Run rector" + run: "vendor/bin/rector process test/ --config=rector-migrate.php --dry-run" diff --git a/.gitignore b/.gitignore index a65c611c9c..1305898a0c 100644 --- a/.gitignore +++ b/.gitignore @@ -3,5 +3,6 @@ /vendor-bin/php-cs-fixer/vendor/ /vendor-bin/phpstan/vendor/ /vendor-bin/psalm/vendor/ +/vendor-bin/rector/vendor/ /.php-cs-fixer.cache /composer.lock diff --git a/Makefile b/Makefile index d935675e04..6765bbcffc 100644 --- a/Makefile +++ b/Makefile @@ -20,6 +20,10 @@ coverage: vendor ## Collects coverage with phpunit test: vendor ## Runs tests with phpunit vendor/bin/phpunit +.PHONY: rector +rector: vendor ## Runs rector + vendor/bin/rector process test/ --config=rector-migrate.php --dry-run + .PHONY: static static: vendor ## Runs static analyzers vendor/bin/phpstan diff --git a/README.md b/README.md index 8958f9847d..c3c199d5c1 100644 --- a/README.md +++ b/README.md @@ -57,6 +57,37 @@ for ($i = 0; $i < 3; $i++) { // 'Orlo Bergstrom' ``` +## Automated refactoring + +If you already used this library with its properties, they are now deprecated and needs to be replaced by their equivalent methods. + +You can use the provided [Rector](https://github.com/rectorphp/rector) config file to automate the work. + +Run + +```bash +composer require --dev rector/rector +``` + +to install `rector/rector`. + +Run + +```bash +vendor/bin/rector process src/ --config vendor/fakerphp/faker/rector-migrate.php +``` + +to run `rector/rector`. + +*Note:* do not forget to replace `src/` with the path to your source directory. + +Another way is to use it in your `rector.php` file: + +```php +$rectorConfig->import('vendor/fakerphp/faker/rector-migrate.php'); +$faker($rectorConfig); +``` + ## License Faker is released under the MIT License. See [`LICENSE`](LICENSE) for details. diff --git a/rector-migrate.php b/rector-migrate.php new file mode 100644 index 0000000000..2839c8b179 --- /dev/null +++ b/rector-migrate.php @@ -0,0 +1,160 @@ +ruleWithConfiguration( + Transform\Rector\Assign\PropertyFetchToMethodCallRector::class, + array_map(static function (string $property): Transform\ValueObject\PropertyFetchToMethodCall { + return new Transform\ValueObject\PropertyFetchToMethodCall( + Generator::class, + $property, + $property, + ); + }, $properties), + ); +}; diff --git a/vendor-bin/rector/composer.json b/vendor-bin/rector/composer.json new file mode 100644 index 0000000000..6cd9a82381 --- /dev/null +++ b/vendor-bin/rector/composer.json @@ -0,0 +1,13 @@ +{ + "require": { + "php": "^8.1", + "rector/rector": "^0.15.0" + }, + "config": { + "platform": { + "php": "8.1.12" + }, + "preferred-install": "dist", + "sort-packages": true + } +} diff --git a/vendor-bin/rector/composer.lock b/vendor-bin/rector/composer.lock new file mode 100644 index 0000000000..ca0fbed694 --- /dev/null +++ b/vendor-bin/rector/composer.lock @@ -0,0 +1,139 @@ +{ + "_readme": [ + "This file locks the dependencies of your project to a known state", + "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", + "This file is @generated automatically" + ], + "content-hash": "be38cafadfc09ca7ff05a317facfa0d5", + "packages": [ + { + "name": "phpstan/phpstan", + "version": "1.9.2", + "source": { + "type": "git", + "url": "https://github.com/phpstan/phpstan.git", + "reference": "d6fdf01c53978b6429f1393ba4afeca39cc68afa" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/phpstan/phpstan/zipball/d6fdf01c53978b6429f1393ba4afeca39cc68afa", + "reference": "d6fdf01c53978b6429f1393ba4afeca39cc68afa", + "shasum": "" + }, + "require": { + "php": "^7.2|^8.0" + }, + "conflict": { + "phpstan/phpstan-shim": "*" + }, + "bin": [ + "phpstan", + "phpstan.phar" + ], + "type": "library", + "autoload": { + "files": [ + "bootstrap.php" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "description": "PHPStan - PHP Static Analysis Tool", + "keywords": [ + "dev", + "static analysis" + ], + "support": { + "issues": "https://github.com/phpstan/phpstan/issues", + "source": "https://github.com/phpstan/phpstan/tree/1.9.2" + }, + "funding": [ + { + "url": "https://github.com/ondrejmirtes", + "type": "github" + }, + { + "url": "https://github.com/phpstan", + "type": "github" + }, + { + "url": "https://tidelift.com/funding/github/packagist/phpstan/phpstan", + "type": "tidelift" + } + ], + "time": "2022-11-10T09:56:11+00:00" + }, + { + "name": "rector/rector", + "version": "0.15.0", + "source": { + "type": "git", + "url": "https://github.com/rectorphp/rector.git", + "reference": "fbfbe499d0fedfac7fe2fed1a55a00bd08f19c91" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/rectorphp/rector/zipball/fbfbe499d0fedfac7fe2fed1a55a00bd08f19c91", + "reference": "fbfbe499d0fedfac7fe2fed1a55a00bd08f19c91", + "shasum": "" + }, + "require": { + "php": "^7.2|^8.0", + "phpstan/phpstan": "^1.9.2" + }, + "conflict": { + "rector/rector-doctrine": "*", + "rector/rector-downgrade-php": "*", + "rector/rector-php-parser": "*", + "rector/rector-phpunit": "*", + "rector/rector-symfony": "*" + }, + "bin": [ + "bin/rector" + ], + "type": "library", + "extra": { + "branch-alias": { + "dev-main": "0.14-dev" + } + }, + "autoload": { + "files": [ + "bootstrap.php" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "description": "Instant Upgrade and Automated Refactoring of any PHP code", + "support": { + "issues": "https://github.com/rectorphp/rector/issues", + "source": "https://github.com/rectorphp/rector/tree/0.15.0" + }, + "funding": [ + { + "url": "https://github.com/tomasvotruba", + "type": "github" + } + ], + "time": "2022-12-04T22:40:18+00:00" + } + ], + "packages-dev": [], + "aliases": [], + "minimum-stability": "stable", + "stability-flags": [], + "prefer-stable": false, + "prefer-lowest": false, + "platform": { + "php": "^8.1" + }, + "platform-dev": [], + "platform-overrides": { + "php": "8.1.12" + }, + "plugin-api-version": "2.3.0" +}