Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Transform] Adds ArrayDimFetchToMethodCallRector rule (#5723)
* Adds ArrayDimFetchToMethodCallRector rule * Swap checks
- Loading branch information
Showing
8 changed files
with
199 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
...tor/ArrayDimFetch/ArrayDimFetchToMethodCallRector/ArrayDimFetchToMethodCallRectorTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Tests\Transform\Rector\ArrayDimFetch\ArrayDimFetchToMethodCallRector; | ||
|
||
use Iterator; | ||
use PHPUnit\Framework\Attributes\DataProvider; | ||
use Rector\Testing\PHPUnit\AbstractRectorTestCase; | ||
|
||
final class ArrayDimFetchToMethodCallRectorTest 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/configured_rule.php'; | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
...ts/Transform/Rector/ArrayDimFetch/ArrayDimFetchToMethodCallRector/Fixture/fixture.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\Transform\Rector\ArrayDimFetch\ArrayDimFetchToMethodCallRector\Fixture; | ||
|
||
/** @var \SomeClass $object */ | ||
$object['key']->get() | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\Tests\Transform\Rector\ArrayDimFetch\ArrayDimFetchToMethodCallRector\Fixture; | ||
|
||
/** @var \SomeClass $object */ | ||
$object->make('key')->get() | ||
|
||
?> |
8 changes: 8 additions & 0 deletions
8
...or/ArrayDimFetch/ArrayDimFetchToMethodCallRector/Fixture/skip_non_matching_object.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\Transform\Rector\ArrayDimFetch\ArrayDimFetchToMethodCallRector\Fixture; | ||
|
||
/** @var \SomeOtherClass $object */ | ||
$object['key']->get() | ||
|
||
?> |
10 changes: 10 additions & 0 deletions
10
...s/Transform/Rector/ArrayDimFetch/ArrayDimFetchToMethodCallRector/Source/ChoiceControl.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Tests\Transform\Rector\ArrayDimFetch\ArrayDimFetchToMethodCallRector\Source; | ||
|
||
final class ChoiceControl | ||
{ | ||
|
||
} |
14 changes: 14 additions & 0 deletions
14
...Transform/Rector/ArrayDimFetch/ArrayDimFetchToMethodCallRector/config/configured_rule.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Rector\Config\RectorConfig; | ||
use Rector\Transform\Rector\ArrayDimFetch\ArrayDimFetchToMethodCallRector; | ||
use Rector\Transform\ValueObject\ArrayDimFetchToMethodCall; | ||
|
||
return static function (RectorConfig $rectorConfig): void { | ||
$rectorConfig | ||
->ruleWithConfiguration(ArrayDimFetchToMethodCallRector::class, [ | ||
new ArrayDimFetchToMethodCall('SomeClass', 'make'), | ||
]); | ||
}; |
82 changes: 82 additions & 0 deletions
82
rules/Transform/Rector/ArrayDimFetch/ArrayDimFetchToMethodCallRector.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Transform\Rector\ArrayDimFetch; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Arg; | ||
use PhpParser\Node\Expr\ArrayDimFetch; | ||
use PhpParser\Node\Expr\MethodCall; | ||
use PhpParser\Node\Expr\Variable; | ||
use PHPStan\Type\ObjectType; | ||
use Rector\Contract\Rector\ConfigurableRectorInterface; | ||
use Rector\Rector\AbstractRector; | ||
use Rector\Transform\ValueObject\ArrayDimFetchToMethodCall; | ||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample; | ||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; | ||
use Webmozart\Assert\Assert; | ||
|
||
/** | ||
* @see \Rector\Tests\Transform\Rector\ArrayDimFetch\ArrayDimFetchToMethodCallRector\ArrayDimFetchToMethodCallRectorTest | ||
*/ | ||
class ArrayDimFetchToMethodCallRector extends AbstractRector implements ConfigurableRectorInterface | ||
{ | ||
/** | ||
* @var ArrayDimFetchToMethodCall[] | ||
*/ | ||
private array $arrayDimFetchToMethodCalls; | ||
|
||
public function getRuleDefinition(): RuleDefinition | ||
{ | ||
return new RuleDefinition('Change array dim fetch to method call', [ | ||
new ConfiguredCodeSample( | ||
<<<'CODE_SAMPLE' | ||
$app['someService']; | ||
CODE_SAMPLE | ||
, | ||
<<<'CODE_SAMPLE' | ||
$app->make('someService'); | ||
CODE_SAMPLE | ||
, | ||
[new ArrayDimFetchToMethodCall('SomeClass', 'make')] | ||
), | ||
]); | ||
} | ||
|
||
public function getNodeTypes(): array | ||
{ | ||
return [ArrayDimFetch::class]; | ||
} | ||
|
||
/** | ||
* @param ArrayDimFetch $node | ||
*/ | ||
public function refactor(Node $node): ?MethodCall | ||
{ | ||
if (! $node->var instanceof Variable) { | ||
return null; | ||
} | ||
|
||
foreach ($this->arrayDimFetchToMethodCalls as $arrayDimFetchToMethodCall) { | ||
if (! $node->dim instanceof Node) { | ||
return null; | ||
} | ||
|
||
if (! $this->isObjectType($node->var, new ObjectType($arrayDimFetchToMethodCall->getClass()))) { | ||
return null; | ||
} | ||
|
||
return new MethodCall($node->var, $arrayDimFetchToMethodCall->getMethod(), [new Arg($node->dim)]); | ||
} | ||
|
||
return null; | ||
} | ||
|
||
public function configure(array $configuration): void | ||
{ | ||
Assert::allIsInstanceOf($configuration, ArrayDimFetchToMethodCall::class); | ||
|
||
$this->arrayDimFetchToMethodCalls = $configuration; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Transform\ValueObject; | ||
|
||
class ArrayDimFetchToMethodCall | ||
{ | ||
public function __construct( | ||
private readonly string $class, | ||
private readonly string $method | ||
) { | ||
} | ||
|
||
public function getClass(): string | ||
{ | ||
return $this->class; | ||
} | ||
|
||
public function getMethod(): string | ||
{ | ||
return $this->method; | ||
} | ||
} |