rector/rules/transform/src/Rector/Assign/PropertyToMethodRector.php

170 lines
5.0 KiB
PHP
Raw Normal View History

2019-10-13 05:59:52 +00:00
<?php
declare(strict_types=1);
2017-11-18 15:20:10 +00:00
namespace Rector\Transform\Rector\Assign;
2017-11-18 15:20:10 +00:00
use PhpParser\Node;
2017-11-18 15:48:20 +00:00
use PhpParser\Node\Expr\Assign;
2017-11-18 15:20:10 +00:00
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Expr\Variable;
2020-07-29 23:39:41 +00:00
use Rector\Core\Contract\Rector\ConfigurableRectorInterface;
use Rector\Core\Exception\ShouldNotHappenException;
use Rector\Core\Rector\AbstractRector;
use Rector\Transform\ValueObject\PropertyToMethod;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
use Webmozart\Assert\Assert;
2017-11-18 15:20:10 +00:00
2019-09-03 09:11:45 +00:00
/**
* @see \Rector\Transform\Tests\Rector\Assign\PropertyToMethodRector\PropertyToMethodRectorTest
2019-09-03 09:11:45 +00:00
*/
2020-07-29 23:39:41 +00:00
final class PropertyToMethodRector extends AbstractRector implements ConfigurableRectorInterface
2017-11-18 15:20:10 +00:00
{
2020-07-29 23:39:41 +00:00
/**
* @var string
*/
public const PROPERTIES_TO_METHOD_CALLS = 'properties_to_method_calls';
2020-07-29 23:39:41 +00:00
2020-05-06 21:39:33 +00:00
/**
* @var PropertyToMethod[]
2017-11-18 15:20:10 +00:00
*/
private $propertiesToMethodCalls = [];
2017-11-18 15:20:10 +00:00
public function getRuleDefinition(): RuleDefinition
2018-04-08 11:51:26 +00:00
{
$firstConfiguration = [
self::PROPERTIES_TO_METHOD_CALLS => [
new PropertyToMethod('SomeObject', 'property', 'getProperty', 'setProperty'),
],
];
$secondConfiguration = [
self::PROPERTIES_TO_METHOD_CALLS => [
new PropertyToMethod('SomeObject', 'property', 'getConfig', null, ['someArg']),
],
];
return new RuleDefinition('Replaces properties assign calls be defined methods.', [
new ConfiguredCodeSample(
<<<'CODE_SAMPLE'
2018-04-08 11:51:26 +00:00
$result = $object->property;
$object->property = $value;
CODE_SAMPLE
2018-04-08 11:51:26 +00:00
,
<<<'CODE_SAMPLE'
2018-04-08 11:51:26 +00:00
$result = $object->getProperty();
$object->setProperty($value);
CODE_SAMPLE
,
$firstConfiguration
),
new ConfiguredCodeSample(
<<<'CODE_SAMPLE'
$result = $object->property;
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
$result = $object->getProperty('someArg');
CODE_SAMPLE
,
$secondConfiguration
2018-04-08 14:05:11 +00:00
),
2018-04-08 11:51:26 +00:00
]);
}
2018-08-14 22:12:41 +00:00
/**
* @return string[]
*/
public function getNodeTypes(): array
2017-11-18 15:20:10 +00:00
{
2018-08-14 22:12:41 +00:00
return [Assign::class];
2017-11-18 15:20:10 +00:00
}
/**
* @param Assign $node
2017-11-18 15:20:10 +00:00
*/
public function refactor(Node $node): ?Node
2017-11-18 15:20:10 +00:00
{
if ($node->var instanceof PropertyFetch) {
return $this->processSetter($node);
}
if ($node->expr instanceof PropertyFetch) {
return $this->processGetter($node);
}
2018-08-14 22:12:41 +00:00
return null;
}
2017-11-18 15:48:20 +00:00
2020-07-29 23:39:41 +00:00
public function configure(array $configuration): void
{
$propertiesToMethodCalls = $configuration[self::PROPERTIES_TO_METHOD_CALLS] ?? [];
Assert::allIsInstanceOf($propertiesToMethodCalls, PropertyToMethod::class);
$this->propertiesToMethodCalls = $propertiesToMethodCalls;
2020-07-29 23:39:41 +00:00
}
2019-02-22 17:25:31 +00:00
private function processSetter(Assign $assign): ?Node
{
/** @var PropertyFetch $propertyFetchNode */
2019-02-22 17:25:31 +00:00
$propertyFetchNode = $assign->var;
$propertyToMethodCall = $this->matchPropertyFetchCandidate($propertyFetchNode);
if (! $propertyToMethodCall instanceof PropertyToMethod) {
return null;
2017-11-18 15:48:20 +00:00
}
if ($propertyToMethodCall->getNewSetMethod() === null) {
throw new ShouldNotHappenException();
}
2019-02-22 17:25:31 +00:00
$args = $this->createArgs([$assign->expr]);
/** @var Variable $variable */
$variable = $propertyFetchNode->var;
return $this->createMethodCall($variable, $propertyToMethodCall->getNewSetMethod(), $args);
}
2019-02-22 17:25:31 +00:00
private function processGetter(Assign $assign): ?Node
{
/** @var PropertyFetch $propertyFetchNode */
2019-02-22 17:25:31 +00:00
$propertyFetchNode = $assign->expr;
$propertyToMethodCall = $this->matchPropertyFetchCandidate($propertyFetchNode);
if (! $propertyToMethodCall instanceof PropertyToMethod) {
return null;
}
// simple method name
if ($propertyToMethodCall->getNewGetMethod() !== '') {
$assign->expr = $this->createMethodCall($propertyFetchNode->var, $propertyToMethodCall->getNewGetMethod());
if ($propertyToMethodCall->getNewGetArguments() !== []) {
$args = $this->createArgs($propertyToMethodCall->getNewGetArguments());
$assign->expr->args = $args;
}
2019-02-22 17:25:31 +00:00
return $assign;
2017-11-18 15:48:20 +00:00
}
2019-02-22 17:25:31 +00:00
return $assign;
2017-11-18 15:48:20 +00:00
}
private function matchPropertyFetchCandidate(PropertyFetch $propertyFetch): ?PropertyToMethod
2017-11-18 15:48:20 +00:00
{
foreach ($this->propertiesToMethodCalls as $propertyToMethodCall) {
if (! $this->isObjectType($propertyFetch->var, $propertyToMethodCall->getOldType())) {
continue;
}
2017-12-10 00:13:25 +00:00
if (! $this->isName($propertyFetch, $propertyToMethodCall->getOldProperty())) {
continue;
2017-11-18 15:48:20 +00:00
}
return $propertyToMethodCall;
2017-11-18 15:48:20 +00:00
}
return null;
2017-11-18 15:20:10 +00:00
}
}