Skip to content

Commit

Permalink
Enhancement: Add rector config file for easy migration (#542)
Browse files Browse the repository at this point in the history
* Enhancement: Add rector config file for easy migration

* Update README.md

Co-authored-by: Pierre du Plessis <[email protected]>

* Update README.md

Co-authored-by: Andreas Möller <[email protected]>

* Update rector-migrate.php

Co-authored-by: Andreas Möller <[email protected]>

* Update README.md

Co-authored-by: Andreas Möller <[email protected]>

* 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 <[email protected]>
Co-authored-by: Andreas Möller <[email protected]>
  • Loading branch information
3 people authored Dec 13, 2022
1 parent 7ae22eb commit cbe48f7
Show file tree
Hide file tree
Showing 8 changed files with 412 additions and 0 deletions.
12 changes: 12 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
52 changes: 52 additions & 0 deletions .github/workflows/rector.yaml
Original file line number Diff line number Diff line change
@@ -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"
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
31 changes: 31 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
160 changes: 160 additions & 0 deletions rector-migrate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
<?php

declare(strict_types=1);

use Rector\Config;
use Rector\Transform;

// This file configures rector/rector to replace all deprecated property usages with their equivalent functions.
return static function (Config\RectorConfig $rectorConfig): void {
$properties = [
'address',
'amPm',
'asciify',
'biasedNumberBetween',
'boolean',
'bothify',
'buildingNumber',
'century',
'chrome',
'city',
'citySuffix',
'colorName',
'company',
'companyEmail',
'companySuffix',
'country',
'countryCode',
'countryISOAlpha3',
'creditCardDetails',
'creditCardExpirationDate',
'creditCardExpirationDateString',
'creditCardNumber',
'creditCardType',
'currencyCode',
'date',
'dateTime',
'dateTimeAD',
'dateTimeBetween',
'dateTimeInInterval',
'dateTimeThisCentury',
'dateTimeThisDecade',
'dateTimeThisMonth',
'dateTimeThisYear',
'dayOfMonth',
'dayOfWeek',
'domainName',
'domainWord',
'e164PhoneNumber',
'email',
'emoji',
'file',
'firefox',
'firstName',
'firstNameFemale',
'firstNameMale',
'freeEmail',
'freeEmailDomain',
'getDefaultTimezone',
'hexColor',
'hslColor',
'hslColorAsArray',
'iban',
'image',
'imageUrl',
'imei',
'internetExplorer',
'iosMobileToken',
'ipv4',
'ipv6',
'iso8601',
'jobTitle',
'languageCode',
'lastName',
'latitude',
'lexify',
'linuxPlatformToken',
'linuxProcessor',
'localCoordinates',
'localIpv4',
'locale',
'longitude',
'macAddress',
'macPlatformToken',
'macProcessor',
'md5',
'month',
'monthName',
'msedge',
'name',
'numerify',
'opera',
'paragraph',
'paragraphs',
'passthrough',
'password',
'phoneNumber',
'postcode',
'randomAscii',
'randomDigitNotNull',
'randomElement',
'randomElements',
'randomHtml',
'randomKey',
'randomLetter',
'realText',
'realTextBetween',
'regexify',
'rgbColor',
'rgbColorAsArray',
'rgbCssColor',
'rgbaCssColor',
'safari',
'safeColorName',
'safeEmail',
'safeEmailDomain',
'safeHexColor',
'sentence',
'sentences',
'setDefaultTimezone',
'sha1',
'sha256',
'shuffle',
'shuffleArray',
'shuffleString',
'slug',
'streetAddress',
'streetName',
'streetSuffix',
'swiftBicNumber',
'text',
'time',
'timezone',
'title',
'titleFemale',
'titleMale',
'tld',
'toLower',
'toUpper',
'unixTime',
'url',
'userAgent',
'userName',
'uuid',
'windowsPlatformToken',
'word',
'words',
'year',
];

$rectorConfig->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),
);
};
13 changes: 13 additions & 0 deletions vendor-bin/rector/composer.json
Original file line number Diff line number Diff line change
@@ -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
}
}
Loading

0 comments on commit cbe48f7

Please sign in to comment.