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 1 commit
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
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
OskarStark marked this conversation as resolved.
Show resolved Hide resolved
```

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.
OskarStark marked this conversation as resolved.
Show resolved Hide resolved

Another way is to use it in your `rector.php` file:

```php
$faker = require 'vendor/fakerphp/faker/rector-migrate.php';
OskarStark marked this conversation as resolved.
Show resolved Hide resolved
$faker($rectorConfig);
```

## License

Faker is released under the MIT License. See [`LICENSE`](LICENSE) for details.
Expand Down
41 changes: 41 additions & 0 deletions rector-migrate.php
@@ -0,0 +1,41 @@
<?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 = [
'boolean',
'city',
'dateTime',
'dateTimeThisYear',
'email',
'firstName',
'firstNameFemale',
'lastName',
'name',
'password',
'phoneNumber',
'sentence',
'sha256',
'slug',
'text',
'url',
'word',
'words',
];

$methodCalls = [];
foreach ($properties as $property) {
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
};