2020-08-12 09:44:34 +00:00
|
|
|
<?php
|
|
|
|
|
2021-05-09 20:15:43 +00:00
|
|
|
declare (strict_types=1);
|
2022-06-06 17:12:56 +00:00
|
|
|
namespace Rector\Transform\Rector\MethodCall;
|
2020-08-12 09:44:34 +00:00
|
|
|
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node;
|
|
|
|
use PhpParser\Node\Expr\MethodCall;
|
2024-01-02 02:40:38 +00:00
|
|
|
use Rector\Contract\Rector\ConfigurableRectorInterface;
|
|
|
|
use Rector\Rector\AbstractRector;
|
2022-06-06 17:12:56 +00:00
|
|
|
use Rector\Transform\ValueObject\MethodCallToPropertyFetch;
|
2022-06-07 09:18:30 +00:00
|
|
|
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
|
|
|
|
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
2024-05-02 02:21:56 +00:00
|
|
|
use RectorPrefix202405\Webmozart\Assert\Assert;
|
2020-08-12 09:44:34 +00:00
|
|
|
/**
|
2021-03-12 22:20:25 +00:00
|
|
|
* @see \Rector\Tests\Transform\Rector\MethodCall\MethodCallToPropertyFetchRector\MethodCallToPropertyFetchRectorTest
|
2020-08-12 09:44:34 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
final class MethodCallToPropertyFetchRector extends AbstractRector implements ConfigurableRectorInterface
|
2020-08-12 09:44:34 +00:00
|
|
|
{
|
|
|
|
/**
|
2022-03-05 11:00:01 +00:00
|
|
|
* @var MethodCallToPropertyFetch[]
|
2020-08-12 09:44:34 +00:00
|
|
|
*/
|
2022-03-05 11:00:01 +00:00
|
|
|
private $methodCallsToPropertyFetches = [];
|
2022-06-07 08:22:29 +00:00
|
|
|
public function getRuleDefinition() : RuleDefinition
|
2020-08-12 09:44:34 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return new RuleDefinition('Turns method call "$this->something()" to property fetch "$this->something"', [new ConfiguredCodeSample(<<<'CODE_SAMPLE'
|
2020-08-12 09:44:34 +00:00
|
|
|
class SomeClass
|
|
|
|
{
|
|
|
|
public function run()
|
|
|
|
{
|
|
|
|
$this->someMethod();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
CODE_SAMPLE
|
2021-05-09 20:15:43 +00:00
|
|
|
, <<<'CODE_SAMPLE'
|
2020-08-12 09:44:34 +00:00
|
|
|
class SomeClass
|
|
|
|
{
|
|
|
|
public function run()
|
|
|
|
{
|
|
|
|
$this->someProperty;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
CODE_SAMPLE
|
2021-12-06 21:12:03 +00:00
|
|
|
, ['someMethod' => 'someProperty'])]);
|
2020-08-12 09:44:34 +00:00
|
|
|
}
|
|
|
|
/**
|
2021-02-27 00:06:15 +00:00
|
|
|
* @return array<class-string<Node>>
|
2020-08-12 09:44:34 +00:00
|
|
|
*/
|
2021-05-09 20:15:43 +00:00
|
|
|
public function getNodeTypes() : array
|
2020-08-12 09:44:34 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return [MethodCall::class];
|
2020-08-12 09:44:34 +00:00
|
|
|
}
|
|
|
|
/**
|
2021-12-10 10:22:23 +00:00
|
|
|
* @param MethodCall $node
|
2020-08-12 09:44:34 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
public function refactor(Node $node) : ?Node
|
2020-08-12 09:44:34 +00:00
|
|
|
{
|
2022-03-05 11:00:01 +00:00
|
|
|
foreach ($this->methodCallsToPropertyFetches as $methodCallToPropertyFetch) {
|
|
|
|
if (!$this->isName($node->name, $methodCallToPropertyFetch->getOldMethod())) {
|
2020-08-12 09:44:34 +00:00
|
|
|
continue;
|
|
|
|
}
|
2022-03-05 11:00:01 +00:00
|
|
|
if (!$this->isObjectType($node->var, $methodCallToPropertyFetch->getOldObjectType())) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
return $this->nodeFactory->createPropertyFetch($node->var, $methodCallToPropertyFetch->getNewProperty());
|
2020-08-12 09:44:34 +00:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
2021-05-02 10:46:55 +00:00
|
|
|
/**
|
2021-11-27 13:43:28 +00:00
|
|
|
* @param mixed[] $configuration
|
2021-05-02 10:46:55 +00:00
|
|
|
*/
|
2021-12-10 10:22:23 +00:00
|
|
|
public function configure(array $configuration) : void
|
2020-08-12 09:44:34 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
Assert::allIsAOf($configuration, MethodCallToPropertyFetch::class);
|
2022-03-05 11:00:01 +00:00
|
|
|
$this->methodCallsToPropertyFetches = $configuration;
|
2020-08-12 09:44:34 +00:00
|
|
|
}
|
|
|
|
}
|