Skip to content
Merged
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
11 changes: 11 additions & 0 deletions config/sets/packages/faker/faker-10.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use RectorLaravel\Rector\PropertyFetch\ReplaceFakerPropertyFetchWithMethodCallRector;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->import(__DIR__ . '/../../../config.php');
$rectorConfig->rule(ReplaceFakerPropertyFetchWithMethodCallRector::class);
};
16 changes: 15 additions & 1 deletion docs/rector_rules_overview.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# 83 Rules Overview
# 84 Rules Overview

## AbortIfRector

Expand Down Expand Up @@ -593,6 +593,7 @@ Use the event or dispatch helpers instead of the static dispatch method.
-ExampleJob::dispatch($email);
+dispatch(new ExampleJob($email));
```

<br>

```diff
Expand Down Expand Up @@ -1259,6 +1260,19 @@ Replace `$this->faker` with the `fake()` helper function in Factories

<br>

## ReplaceFakerPropertyFetchWithMethodCallRector

Replace deprecated faker property fetch with method call

- class: [`RectorLaravel\Rector\PropertyFetch\ReplaceFakerPropertyFetchWithMethodCallRector`](../src/Rector/PropertyFetch/ReplaceFakerPropertyFetchWithMethodCallRector.php)

```diff
-$faker->name,
+$faker->name(),
```

<br>

## ReplaceServiceContainerCallArgRector

Changes the string or class const used for a service container make call
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

declare(strict_types=1);

namespace RectorLaravel\Rector\PropertyFetch;

use PhpParser\BuilderFactory;
use PhpParser\Node;
use PhpParser\Node\Expr\PropertyFetch;
use PHPStan\Type\ObjectType;
use RectorLaravel\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

/**
* @see \RectorLaravel\Tests\Rector\PropertyFetch\ReplaceFakerInstanceWithHelperRector\ReplaceFakerInstanceWithHelperRectorTest
*/
final class ReplaceFakerPropertyFetchWithMethodCallRector extends AbstractRector
{
public function __construct(
private readonly BuilderFactory $builderFactory,
) {}

public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Replace deprecated faker property fetch with method call',
[
new CodeSample(
<<<'CODE_SAMPLE'
$faker->name,
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
$faker->name(),
CODE_SAMPLE
),
]
);
}

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

/**
* @param PropertyFetch $node
*/
public function refactor(Node $node): ?Node
{
if (! $this->isObjectType($node->var, new ObjectType('Faker\Generator'))) {
return null;
}

return $this->builderFactory->methodCall($node->var, $node->name);
}
}
7 changes: 7 additions & 0 deletions src/Set/LaravelSetProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Rector\Set\Contract\SetProviderInterface;
use Rector\Set\ValueObject\ComposerTriggeredSet;
use Rector\Set\ValueObject\Set;
use RectorLaravel\Set\Packages\Faker\FakerSetList;
use RectorLaravel\Set\Packages\Livewire\LivewireSetList;

final class LaravelSetProvider implements SetProviderInterface
Expand Down Expand Up @@ -102,6 +103,12 @@ public function provide(): array
'3.0',
LivewireSetList::LIVEWIRE_30,
),
new ComposerTriggeredSet(
self::GROUP_NAME,
'fakerphp/faker',
'1.0',
FakerSetList::FAKER_10,
),
...$this->getLaravelVersions(),
];
}
Expand Down
8 changes: 8 additions & 0 deletions src/Set/Packages/Faker/FakerSetList.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace RectorLaravel\Set\Packages\Faker;

final class FakerSetList
{
final public const string FAKER_10 = __DIR__ . '/../../../../config/sets/packages/faker/faker-10.php';
}
9 changes: 9 additions & 0 deletions stubs/Faker/Generator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace Faker;

if (class_exists('Faker\Generator')) {
return;
}

class Generator {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace Rector\Tests\TypeDeclaration\Rector\ReplaceFakerPropertyFetchWithMethodCallRector\Fixture;

use Faker\Generator;

function fake(): Generator
{
return new Generator();
}

/** @var Generator $faker */
$faker->name;
fake()->name;

?>
-----
<?php

namespace Rector\Tests\TypeDeclaration\Rector\ReplaceFakerPropertyFetchWithMethodCallRector\Fixture;

use Faker\Generator;

function fake(): Generator
{
return new Generator();
}

/** @var Generator $faker */
$faker->name();
fake()->name();

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

namespace Rector\Tests\TypeDeclaration\Rector\ReplaceFakerPropertyFetchWithMethodCallRector\Fixture;

use Faker\Generator;

$property = 'name';
/** @var Generator $faker */
$faker->$property;

?>
-----
<?php

namespace Rector\Tests\TypeDeclaration\Rector\ReplaceFakerPropertyFetchWithMethodCallRector\Fixture;

use Faker\Generator;

$property = 'name';
/** @var Generator $faker */
$faker->{$property}();

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

declare(strict_types=1);

namespace RectorLaravel\Tests\Rector\PropertyFetch\ReplaceFakerPropertyFetchWithMethodCallRector;

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

final class ReplaceFakerPropertyFetchWithMethodCallRectorTest extends AbstractRectorTestCase
{
public static function provideData(): Iterator
{
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture');
}

/**
* @test
*/
#[DataProvider('provideData')]
public function test(string $filePath): void
{
$this->doTestFile($filePath);
}

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

declare(strict_types=1);

use Rector\Config\RectorConfig;
use RectorLaravel\Rector\PropertyFetch\ReplaceFakerPropertyFetchWithMethodCallRector;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->rule(ReplaceFakerPropertyFetchWithMethodCallRector::class);
};