-
-
Notifications
You must be signed in to change notification settings - Fork 333
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Transform] Add MethodCallToFuncCallRector (#3391)
- Loading branch information
1 parent
9c6f41e
commit 937bf3e
Showing
9 changed files
with
257 additions
and
6 deletions.
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
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
31 changes: 31 additions & 0 deletions
31
...s-tests/Transform/Rector/MethodCall/MethodCallToFuncCallRector/Fixture/some_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,31 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\Transform\Rector\MethodCall\MethodCallToFuncCallRector\Fixture; | ||
|
||
use Rector\Tests\Transform\Rector\MethodCall\MethodCallToFuncCallRector\Source\ParentControllerWithRender; | ||
|
||
final class SomeClass extends ParentControllerWithRender | ||
{ | ||
public function show() | ||
{ | ||
return $this->render('some_template'); | ||
} | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\Tests\Transform\Rector\MethodCall\MethodCallToFuncCallRector\Fixture; | ||
|
||
use Rector\Tests\Transform\Rector\MethodCall\MethodCallToFuncCallRector\Source\ParentControllerWithRender; | ||
|
||
final class SomeClass extends ParentControllerWithRender | ||
{ | ||
public function show() | ||
{ | ||
return \view('some_template'); | ||
} | ||
} | ||
|
||
?> |
28 changes: 28 additions & 0 deletions
28
...Transform/Rector/MethodCall/MethodCallToFuncCallRector/MethodCallToFuncCallRectorTest.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\MethodCallToFuncCallRector; | ||
|
||
use Iterator; | ||
use PHPUnit\Framework\Attributes\DataProvider; | ||
use Rector\Testing\PHPUnit\AbstractRectorTestCase; | ||
|
||
final class MethodCallToFuncCallRectorTest 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'; | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
...nsform/Rector/MethodCall/MethodCallToFuncCallRector/Source/ParentControllerWithRender.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 | ||
|
||
namespace Rector\Tests\Transform\Rector\MethodCall\MethodCallToFuncCallRector\Source; | ||
|
||
class ParentControllerWithRender | ||
{ | ||
public function render() | ||
{ | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
...s-tests/Transform/Rector/MethodCall/MethodCallToFuncCallRector/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 Rector\Config\RectorConfig; | ||
|
||
use Rector\Transform\Rector\MethodCall\MethodCallToFuncCallRector; | ||
use Rector\Transform\ValueObject\MethodCallToFuncCall; | ||
|
||
return static function (RectorConfig $rectorConfig): void { | ||
$rectorConfig->ruleWithConfiguration(MethodCallToFuncCallRector::class, [ | ||
new MethodCallToFuncCall( | ||
'Rector\Tests\Transform\Rector\MethodCall\MethodCallToFuncCallRector\Source\ParentControllerWithRender', | ||
'render', | ||
'view' | ||
), | ||
]); | ||
}; |
95 changes: 95 additions & 0 deletions
95
rules/Transform/Rector/MethodCall/MethodCallToFuncCallRector.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,95 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Transform\Rector\MethodCall; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Expr\FuncCall; | ||
use PhpParser\Node\Expr\MethodCall; | ||
use PhpParser\Node\Name\FullyQualified; | ||
use PHPStan\Type\ObjectType; | ||
use Rector\Core\Contract\Rector\ConfigurableRectorInterface; | ||
use Rector\Core\Rector\AbstractRector; | ||
use Rector\Transform\ValueObject\MethodCallToFuncCall; | ||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample; | ||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; | ||
use Webmozart\Assert\Assert; | ||
|
||
/** | ||
* @see \Rector\Tests\Transform\Rector\MethodCall\MethodCallToFuncCallRector\MethodCallToFuncCallRectorTest | ||
*/ | ||
final class MethodCallToFuncCallRector extends AbstractRector implements ConfigurableRectorInterface | ||
{ | ||
/** | ||
* @var MethodCallToFuncCall[] | ||
*/ | ||
private array $methodCallsToFuncCalls = []; | ||
|
||
public function getRuleDefinition(): RuleDefinition | ||
{ | ||
return new RuleDefinition('Change method call to function call', [ | ||
new ConfiguredCodeSample( | ||
<<<'CODE_SAMPLE' | ||
final class SomeClass | ||
{ | ||
public function show() | ||
{ | ||
return $this->render('some_template'); | ||
} | ||
} | ||
CODE_SAMPLE | ||
|
||
, | ||
<<<'CODE_SAMPLE' | ||
final class SomeClass | ||
{ | ||
public function show() | ||
{ | ||
return view('some_template'); | ||
} | ||
} | ||
CODE_SAMPLE | ||
, | ||
[new MethodCallToFuncCall('SomeClass', 'render', 'view')] | ||
), | ||
]); | ||
} | ||
|
||
/** | ||
* @return array<class-string<Node>> | ||
*/ | ||
public function getNodeTypes(): array | ||
{ | ||
return [MethodCall::class]; | ||
} | ||
|
||
/** | ||
* @param MethodCall $node | ||
*/ | ||
public function refactor(Node $node): ?Node | ||
{ | ||
foreach ($this->methodCallsToFuncCalls as $methodCallToFuncCall) { | ||
if (! $this->isName($node->name, $methodCallToFuncCall->getMethodName())) { | ||
continue; | ||
} | ||
|
||
if (! $this->isObjectType($node->var, new ObjectType($methodCallToFuncCall->getObjectType()))) { | ||
continue; | ||
} | ||
|
||
return new FuncCall(new FullyQualified($methodCallToFuncCall->getFunctionName()), $node->getArgs()); | ||
} | ||
|
||
return null; | ||
} | ||
|
||
/** | ||
* @param mixed[] $configuration | ||
*/ | ||
public function configure(array $configuration): void | ||
{ | ||
Assert::allIsInstanceOf($configuration, MethodCallToFuncCall::class); | ||
$this->methodCallsToFuncCalls = $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,30 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Transform\ValueObject; | ||
|
||
final class MethodCallToFuncCall | ||
{ | ||
public function __construct( | ||
private readonly string $objectType, | ||
private readonly string $methodName, | ||
private readonly string $functionName | ||
) { | ||
} | ||
|
||
public function getObjectType(): string | ||
{ | ||
return $this->objectType; | ||
} | ||
|
||
public function getMethodName(): string | ||
{ | ||
return $this->methodName; | ||
} | ||
|
||
public function getFunctionName(): string | ||
{ | ||
return $this->functionName; | ||
} | ||
} |
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