Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhancement: Add rector config file for easy migration #542

Merged
merged 21 commits into from Dec 13, 2022
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 12 additions & 0 deletions .github/dependabot.yml
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
@@ -0,0 +1,52 @@
on:
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
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
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
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
165 changes: 165 additions & 0 deletions rector-migrate.php
@@ -0,0 +1,165 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\Transform\Rector\Assign\PropertyFetchToMethodCallRector;
use Rector\Transform\ValueObject\PropertyFetchToMethodCall;

// This file configures rector/rector to replace all deprecated property usages with their equivalent functions.
return static function (RectorConfig $rectorConfig): void {
localheinz marked this conversation as resolved.
Show resolved Hide resolved
$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',
];

$methodCalls = [];

foreach ($properties as $property) {
if (!property_exists(\Faker\Generator::class, $property)) {
throw new \InvalidArgumentException(sprintf('Property "%s" does not exist in class "%s"', $property, \Faker\Generator::class));
}
localheinz marked this conversation as resolved.
Show resolved Hide resolved
OskarStark marked this conversation as resolved.
Show resolved Hide resolved

OskarStark marked this conversation as resolved.
Show resolved Hide resolved
$methodCalls[] = new PropertyFetchToMethodCall(Generator::class, $property, $property);
}

$rectorConfig->ruleWithConfiguration(
PropertyFetchToMethodCallRector::class,
$methodCalls
);
localheinz marked this conversation as resolved.
Show resolved Hide resolved
};
13 changes: 13 additions & 0 deletions 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
}
}