-
-
Notifications
You must be signed in to change notification settings - Fork 359
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Transform] MethodCall to New (#6352)
* working rule * Fixes * Skip first class callable
- Loading branch information
Showing
9 changed files
with
248 additions
and
0 deletions.
There are no files selected for viewing
31 changes: 31 additions & 0 deletions
31
rules-tests/Transform/Rector/MethodCall/MethodCallToNewRector/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,31 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\Transform\Rector\MethodCall\MethodCallToNewRector\Fixture; | ||
|
||
class Fixture | ||
{ | ||
public function run(\Rector\Tests\Transform\Rector\MethodCall\MethodCallToNewRector\Source\ResponseFactory $factory) | ||
{ | ||
$response = $factory->createResponse([ | ||
'a' => 'b' | ||
]); | ||
} | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\Tests\Transform\Rector\MethodCall\MethodCallToNewRector\Fixture; | ||
|
||
class Fixture | ||
{ | ||
public function run(\Rector\Tests\Transform\Rector\MethodCall\MethodCallToNewRector\Source\ResponseFactory $factory) | ||
{ | ||
$response = new \Rector\Tests\Transform\Rector\MethodCall\MethodCallToNewRector\Source\Response([ | ||
'a' => 'b' | ||
]); | ||
} | ||
} | ||
|
||
?> |
15 changes: 15 additions & 0 deletions
15
...Transform/Rector/MethodCall/MethodCallToNewRector/Fixture/skip_non_matching_class.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,15 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\Transform\Rector\MethodCall\MethodCallToNewRector\Fixture; | ||
|
||
class SkipNonMatchingClass | ||
{ | ||
public function run(Response $factory) | ||
{ | ||
$response = $factory->jsonResponse([ | ||
'a' => 'b' | ||
]); | ||
} | ||
} | ||
|
||
?> |
15 changes: 15 additions & 0 deletions
15
...ransform/Rector/MethodCall/MethodCallToNewRector/Fixture/skip_non_matching_method.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,15 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\Transform\Rector\MethodCall\MethodCallToNewRector\Fixture; | ||
|
||
class SkipNonMatchingMethod | ||
{ | ||
public function run(\Rector\Tests\Transform\Rector\MethodCall\MethodCallToNewRector\Source\ResponseFactory $factory) | ||
{ | ||
$response = $factory->jsonResponse([ | ||
'a' => 'b' | ||
]); | ||
} | ||
} | ||
|
||
?> |
28 changes: 28 additions & 0 deletions
28
rules-tests/Transform/Rector/MethodCall/MethodCallToNewRector/MethodCallToNewRectorTest.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\MethodCall\MethodCallToNewRector; | ||
|
||
use Iterator; | ||
use PHPUnit\Framework\Attributes\DataProvider; | ||
use Rector\Testing\PHPUnit\AbstractRectorTestCase; | ||
|
||
final class MethodCallToNewRectorTest 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'; | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
rules-tests/Transform/Rector/MethodCall/MethodCallToNewRector/Source/Response.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,11 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\Transform\Rector\MethodCall\MethodCallToNewRector\Source; | ||
|
||
class Response | ||
{ | ||
public function __construct(public array $params) | ||
{ | ||
|
||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
rules-tests/Transform/Rector/MethodCall/MethodCallToNewRector/Source/ResponseFactory.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,11 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\Transform\Rector\MethodCall\MethodCallToNewRector\Source; | ||
|
||
class ResponseFactory | ||
{ | ||
public function createResponse(array $params): Response | ||
{ | ||
return new Response($params); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
rules-tests/Transform/Rector/MethodCall/MethodCallToNewRector/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,18 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use PHPStan\Type\ObjectType; | ||
use Rector\Config\RectorConfig; | ||
use Rector\Transform\Rector\MethodCall\MethodCallToNewRector; | ||
use Rector\Transform\ValueObject\MethodCallToNew; | ||
|
||
return static function (RectorConfig $rectorConfig): void { | ||
$rectorConfig->ruleWithConfiguration(MethodCallToNewRector::class, [ | ||
new MethodCallToNew( | ||
new ObjectType('Rector\Tests\Transform\Rector\MethodCall\MethodCallToNewRector\Source\ResponseFactory'), | ||
'createResponse', | ||
'Rector\Tests\Transform\Rector\MethodCall\MethodCallToNewRector\Source\Response', | ||
), | ||
]); | ||
}; |
84 changes: 84 additions & 0 deletions
84
rules/Transform/Rector/MethodCall/MethodCallToNewRector.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,84 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Transform\Rector\MethodCall; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Expr\MethodCall; | ||
use PhpParser\Node\Expr\New_; | ||
use PhpParser\Node\Name\FullyQualified; | ||
use PHPStan\Type\ObjectType; | ||
use Rector\Contract\Rector\ConfigurableRectorInterface; | ||
use Rector\Rector\AbstractRector; | ||
use Rector\Transform\ValueObject\MethodCallToNew; | ||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample; | ||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; | ||
use Webmozart\Assert\Assert; | ||
|
||
/** | ||
* @see \Rector\Tests\Transform\Rector\MethodCall\MethodCallToNewRector\MethodCallToNewRectorTest | ||
*/ | ||
class MethodCallToNewRector extends AbstractRector implements ConfigurableRectorInterface | ||
{ | ||
/** | ||
* @var MethodCallToNew[] | ||
*/ | ||
private array $methodCallToNew; | ||
|
||
/** | ||
* @param MethodCallToNew[] $configuration | ||
*/ | ||
public function configure(array $configuration): void | ||
{ | ||
Assert::allIsAOf($configuration, MethodCallToNew::class); | ||
$this->methodCallToNew = $configuration; | ||
} | ||
|
||
public function getRuleDefinition(): RuleDefinition | ||
{ | ||
return new RuleDefinition( | ||
'Change method call to new class', | ||
[new ConfiguredCodeSample( | ||
<<<'CODE_SAMPLE' | ||
$object->createResponse(['a' => 1]); | ||
CODE_SAMPLE | ||
, | ||
<<<'CODE_SAMPLE' | ||
new Response(['a' => 1]); | ||
CODE_SAMPLE | ||
, | ||
[new MethodCallToNew(new ObjectType('ResponseFactory'), 'createResponse', 'Response')] | ||
)] | ||
); | ||
} | ||
|
||
public function getNodeTypes(): array | ||
{ | ||
return [MethodCall::class]; | ||
} | ||
|
||
/** | ||
* @param MethodCall $node | ||
*/ | ||
public function refactor(Node $node): ?New_ | ||
{ | ||
if ($node->isFirstClassCallable()) { | ||
return null; | ||
} | ||
|
||
foreach ($this->methodCallToNew as $methodCallToNew) { | ||
if (! $this->isName($node->name, $methodCallToNew->getMethodName())) { | ||
continue; | ||
} | ||
|
||
if (! $this->isObjectType($node->var, $methodCallToNew->getObject())) { | ||
continue; | ||
} | ||
|
||
return new New_(new FullyQualified($methodCallToNew->getNewClassString()), $node->args); | ||
} | ||
|
||
return null; | ||
} | ||
} |
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,35 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Transform\ValueObject; | ||
|
||
use PHPStan\Type\ObjectType; | ||
|
||
final readonly class MethodCallToNew | ||
{ | ||
/** | ||
* @param class-string $newClassString | ||
*/ | ||
public function __construct( | ||
private ObjectType $objectType, | ||
private string $methodName, | ||
private string $newClassString | ||
) { | ||
} | ||
|
||
public function getObject(): ObjectType | ||
{ | ||
return $this->objectType; | ||
} | ||
|
||
public function getMethodName(): string | ||
{ | ||
return $this->methodName; | ||
} | ||
|
||
public function getNewClassString(): string | ||
{ | ||
return $this->newClassString; | ||
} | ||
} |