2019-10-13 05:59:52 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
2017-11-18 15:20:10 +00:00
|
|
|
|
2020-08-30 13:47:36 +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;
|
2017-12-27 20:44:10 +00:00
|
|
|
use PhpParser\Node\Expr\Variable;
|
2020-07-29 23:39:41 +00:00
|
|
|
use Rector\Core\Contract\Rector\ConfigurableRectorInterface;
|
2020-08-26 10:54:53 +00:00
|
|
|
use Rector\Core\Exception\ShouldNotHappenException;
|
2020-02-06 21:48:18 +00:00
|
|
|
use Rector\Core\Rector\AbstractRector;
|
2020-09-12 21:19:08 +00:00
|
|
|
use Rector\Transform\ValueObject\PropertyToMethod;
|
2020-11-16 17:50:38 +00:00
|
|
|
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
|
|
|
|
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
2020-08-26 10:54:53 +00:00
|
|
|
use Webmozart\Assert\Assert;
|
2017-11-18 15:20:10 +00:00
|
|
|
|
2019-09-03 09:11:45 +00:00
|
|
|
/**
|
2020-08-30 13:47:36 +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
|
|
|
|
*/
|
2020-08-26 10:54:53 +00:00
|
|
|
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
|
|
|
/**
|
2020-09-12 21:19:08 +00:00
|
|
|
* @var PropertyToMethod[]
|
2017-11-18 15:20:10 +00:00
|
|
|
*/
|
2020-08-26 10:54:53 +00:00
|
|
|
private $propertiesToMethodCalls = [];
|
2017-11-18 15:20:10 +00:00
|
|
|
|
2020-11-16 17:50:38 +00:00
|
|
|
public function getRuleDefinition(): RuleDefinition
|
2018-04-08 11:51:26 +00:00
|
|
|
{
|
2020-12-12 12:08:34 +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']),
|
|
|
|
],
|
|
|
|
];
|
2020-11-16 17:50:38 +00:00
|
|
|
return new RuleDefinition('Replaces properties assign calls be defined methods.', [
|
2018-08-01 19:22:52 +00:00
|
|
|
new ConfiguredCodeSample(
|
2020-09-15 08:23:13 +00:00
|
|
|
<<<'CODE_SAMPLE'
|
2018-04-08 11:51:26 +00:00
|
|
|
$result = $object->property;
|
|
|
|
$object->property = $value;
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2018-04-08 11:51:26 +00:00
|
|
|
,
|
2020-09-15 08:23:13 +00:00
|
|
|
<<<'CODE_SAMPLE'
|
2018-04-08 11:51:26 +00:00
|
|
|
$result = $object->getProperty();
|
|
|
|
$object->setProperty($value);
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2018-08-01 19:22:52 +00:00
|
|
|
,
|
2020-12-12 12:08:34 +00:00
|
|
|
$firstConfiguration
|
2018-09-19 22:45:34 +00:00
|
|
|
),
|
|
|
|
new ConfiguredCodeSample(
|
2020-09-15 08:23:13 +00:00
|
|
|
<<<'CODE_SAMPLE'
|
2018-09-19 22:45:34 +00:00
|
|
|
$result = $object->property;
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2018-09-19 22:45:34 +00:00
|
|
|
,
|
2020-09-15 08:23:13 +00:00
|
|
|
<<<'CODE_SAMPLE'
|
2018-09-19 22:45:34 +00:00
|
|
|
$result = $object->getProperty('someArg');
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2018-09-19 22:45:34 +00:00
|
|
|
,
|
2020-12-12 12:08:34 +00:00
|
|
|
$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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-10-15 04:36:58 +00:00
|
|
|
* @param Assign $node
|
2017-11-18 15:20:10 +00:00
|
|
|
*/
|
2018-10-15 04:36:58 +00:00
|
|
|
public function refactor(Node $node): ?Node
|
2017-11-18 15:20:10 +00:00
|
|
|
{
|
2018-10-15 04:36:58 +00:00
|
|
|
if ($node->var instanceof PropertyFetch) {
|
|
|
|
return $this->processSetter($node);
|
2018-08-14 21:33:39 +00:00
|
|
|
}
|
2018-09-19 22:45:34 +00:00
|
|
|
|
2018-10-15 04:36:58 +00:00
|
|
|
if ($node->expr instanceof PropertyFetch) {
|
|
|
|
return $this->processGetter($node);
|
2018-08-14 21:33:39 +00:00
|
|
|
}
|
2018-08-14 22:12:41 +00:00
|
|
|
|
2018-09-19 22:45:34 +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
|
|
|
|
{
|
2020-08-26 10:54:53 +00:00
|
|
|
$propertiesToMethodCalls = $configuration[self::PROPERTIES_TO_METHOD_CALLS] ?? [];
|
2020-09-12 21:19:08 +00:00
|
|
|
Assert::allIsInstanceOf($propertiesToMethodCalls, PropertyToMethod::class);
|
2020-08-26 10:54:53 +00:00
|
|
|
$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
|
2018-09-19 22:45:34 +00:00
|
|
|
{
|
|
|
|
/** @var PropertyFetch $propertyFetchNode */
|
2019-02-22 17:25:31 +00:00
|
|
|
$propertyFetchNode = $assign->var;
|
2017-12-27 20:44:10 +00:00
|
|
|
|
2020-08-26 10:54:53 +00:00
|
|
|
$propertyToMethodCall = $this->matchPropertyFetchCandidate($propertyFetchNode);
|
2021-01-20 11:41:35 +00:00
|
|
|
if (! $propertyToMethodCall instanceof PropertyToMethod) {
|
2018-09-19 22:45:34 +00:00
|
|
|
return null;
|
2017-11-18 15:48:20 +00:00
|
|
|
}
|
|
|
|
|
2020-08-26 10:54:53 +00:00
|
|
|
if ($propertyToMethodCall->getNewSetMethod() === null) {
|
|
|
|
throw new ShouldNotHappenException();
|
|
|
|
}
|
|
|
|
|
2019-02-22 17:25:31 +00:00
|
|
|
$args = $this->createArgs([$assign->expr]);
|
2018-09-19 22:45:34 +00:00
|
|
|
|
|
|
|
/** @var Variable $variable */
|
|
|
|
$variable = $propertyFetchNode->var;
|
|
|
|
|
2020-08-26 10:54:53 +00:00
|
|
|
return $this->createMethodCall($variable, $propertyToMethodCall->getNewSetMethod(), $args);
|
2018-09-19 22:45:34 +00:00
|
|
|
}
|
|
|
|
|
2019-02-22 17:25:31 +00:00
|
|
|
private function processGetter(Assign $assign): ?Node
|
2018-09-19 22:45:34 +00:00
|
|
|
{
|
|
|
|
/** @var PropertyFetch $propertyFetchNode */
|
2019-02-22 17:25:31 +00:00
|
|
|
$propertyFetchNode = $assign->expr;
|
2018-09-19 22:45:34 +00:00
|
|
|
|
2020-08-26 10:54:53 +00:00
|
|
|
$propertyToMethodCall = $this->matchPropertyFetchCandidate($propertyFetchNode);
|
2021-01-20 11:41:35 +00:00
|
|
|
if (! $propertyToMethodCall instanceof PropertyToMethod) {
|
2018-09-19 22:45:34 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
// simple method name
|
2020-08-26 10:54:53 +00:00
|
|
|
if ($propertyToMethodCall->getNewGetMethod() !== '') {
|
|
|
|
$assign->expr = $this->createMethodCall($propertyFetchNode->var, $propertyToMethodCall->getNewGetMethod());
|
2018-09-19 22:45:34 +00:00
|
|
|
|
2020-08-26 10:54:53 +00:00
|
|
|
if ($propertyToMethodCall->getNewGetArguments() !== []) {
|
|
|
|
$args = $this->createArgs($propertyToMethodCall->getNewGetArguments());
|
|
|
|
$assign->expr->args = $args;
|
|
|
|
}
|
2018-09-19 22:45:34 +00:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-09-12 21:19:08 +00:00
|
|
|
private function matchPropertyFetchCandidate(PropertyFetch $propertyFetch): ?PropertyToMethod
|
2017-11-18 15:48:20 +00:00
|
|
|
{
|
2020-08-26 10:54:53 +00:00
|
|
|
foreach ($this->propertiesToMethodCalls as $propertyToMethodCall) {
|
|
|
|
if (! $this->isObjectType($propertyFetch->var, $propertyToMethodCall->getOldType())) {
|
2018-10-15 04:36:58 +00:00
|
|
|
continue;
|
|
|
|
}
|
2017-12-10 00:13:25 +00:00
|
|
|
|
2020-08-26 10:54:53 +00:00
|
|
|
if (! $this->isName($propertyFetch, $propertyToMethodCall->getOldProperty())) {
|
2018-10-15 04:36:58 +00:00
|
|
|
continue;
|
2017-11-18 15:48:20 +00:00
|
|
|
}
|
2018-10-15 04:36:58 +00:00
|
|
|
|
2020-08-26 10:54:53 +00:00
|
|
|
return $propertyToMethodCall;
|
2017-11-18 15:48:20 +00:00
|
|
|
}
|
|
|
|
|
2018-09-19 22:45:34 +00:00
|
|
|
return null;
|
2017-11-18 15:20:10 +00:00
|
|
|
}
|
|
|
|
}
|