Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/workflows/rector.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,6 @@ jobs:

- name: "Run rector"
run: "vendor/bin/rector process test/ --ansi --config=rector-migrate.php --dry-run || true"

- name: "Run rector tests"
run: "vendor-bin/rector/vendor/bin/phpunit --configuration vendor-bin/rector/phpunit.xml"
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@
/vendor-bin/psalm/vendor/
/vendor-bin/rector/vendor/
/.php-cs-fixer.cache
/.phpunit.cache/
/composer.lock
/vendor-bin/rector/.phpunit.cache/
19 changes: 7 additions & 12 deletions rector-migrate.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@

declare(strict_types=1);

use Faker\Generator;
use Rector\Config;
use Rector\Transform;
require_once __DIR__ . '/rector/FakerPropertyToMethodCallRector.php';

use Faker\Rector\FakerPropertyToMethodCallRector;
use Rector\Config\RectorConfig;

// This file configures rector/rector to replace all deprecated property usages with their equivalent functions.
return static function (Config\RectorConfig $rectorConfig): void {
return static function (RectorConfig $rectorConfig): void {
$properties = [
'address',
'amPm',
Expand Down Expand Up @@ -149,13 +150,7 @@
];

$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),
FakerPropertyToMethodCallRector::class,
$properties,
);
};
90 changes: 90 additions & 0 deletions rector/FakerPropertyToMethodCallRector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Faker package.
*
* (c) Oskar Stark <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Faker\Rector;

use Faker\Generator;
use PhpParser\Node;
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Identifier;
use PHPStan\Type\ObjectType;
use Rector\Contract\Rector\ConfigurableRectorInterface;
use Rector\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

final class FakerPropertyToMethodCallRector extends AbstractRector implements ConfigurableRectorInterface
{
/**
* @var list<string>
*/
private array $propertyNames = [];

/**
* @param array<mixed> $configuration
*/
public function configure(array $configuration): void
{
$this->propertyNames = array_values(array_filter($configuration, 'is_string'));
}

public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Replaces deprecated Faker property access with method calls',
[
new ConfiguredCodeSample(
<<<'CODE_SAMPLE'
$faker->name;
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
$faker->name();
CODE_SAMPLE
,
['name', 'email', 'address'],
),
],
);
}

/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [PropertyFetch::class];
}

/**
* @param PropertyFetch $node
*/
public function refactor(Node $node): ?Node
{
if (!$node->name instanceof Identifier) {
return null;
}

$propertyName = $node->name->toString();

if (!\in_array($propertyName, $this->propertyNames, true)) {
return null;
}

if (!$this->isObjectType($node->var, new ObjectType(Generator::class))) {
return null;
}

return $this->nodeFactory->createMethodCall($node->var, $propertyName);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Faker package.
*
* (c) Oskar Stark <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Faker\Rector\Tests\FakerPropertyToMethodCallRector;

use Iterator;
use PHPUnit\Framework\Attributes\DataProvider;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;

final class FakerPropertyToMethodCallRectorTest extends AbstractRectorTestCase
{
#[DataProvider('provideData')]
public function test(string $filePath): void
{
$this->doTestFile($filePath);
}

public static function provideData(): Iterator
{
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture');
}

public function provideConfigFilePath(): string
{
return __DIR__ . '/config/config.php';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

use Faker\Generator;

/** @var Generator $faker */
$faker = new Generator();

$name = $faker->name;
$email = $faker->email;
$address = $faker->address;

?>
-----
<?php

use Faker\Generator;

/** @var Generator $faker */
$faker = new Generator();

$name = $faker->name();
$email = $faker->email();
$address = $faker->address();

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

use Faker\Generator;

/** @var Generator $faker */
$faker = new Generator();

// Already method calls - should not change
$name = $faker->name();
$email = $faker->email();
$address = $faker->address();
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

class SomeOtherClass
{
public string $name;
public string $email;
}

$other = new SomeOtherClass();

// Should not change - not a Faker\Generator instance
$name = $other->name;
$email = $other->email;
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

use Faker\Generator;

/** @var Generator $faker */
$faker = new Generator();

// Should not change - 'firstName' is not in the configured list
$firstName = $faker->firstName;
23 changes: 23 additions & 0 deletions rector/tests/FakerPropertyToMethodCallRector/config/config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Faker package.
*
* (c) Oskar Stark <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

use Faker\Rector\FakerPropertyToMethodCallRector;
use Rector\Config\RectorConfig;

return RectorConfig::configure()
->withRules([FakerPropertyToMethodCallRector::class])
->withConfiguredRule(FakerPropertyToMethodCallRector::class, [
'name',
'email',
'address',
]);
13 changes: 13 additions & 0 deletions vendor-bin/rector/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,19 @@
"php": "^8.1",
"rector/rector": "^2.2.6"
},
"require-dev": {
"phpunit/phpunit": "^10.5 || ^11.0"
},
"autoload": {
"psr-4": {
"Faker\\Rector\\": "../../rector/"
}
},
"autoload-dev": {
"psr-4": {
"Faker\\Rector\\Tests\\": "../../rector/tests/"
}
},
"config": {
"platform": {
"php": "8.1.12"
Expand Down
Loading
Loading