mirror of
https://github.com/rectorphp/rector.git
synced 2025-02-24 03:35:01 +01:00
fixup
This commit is contained in:
parent
351be9264c
commit
b641aa8039
@ -3,11 +3,22 @@
|
|||||||
namespace Rector\Rector\Dynamic;
|
namespace Rector\Rector\Dynamic;
|
||||||
|
|
||||||
use PhpParser\Node;
|
use PhpParser\Node;
|
||||||
|
use PhpParser\Node\Arg;
|
||||||
|
use PhpParser\Node\Expr\Assign;
|
||||||
use PhpParser\Node\Expr\MethodCall;
|
use PhpParser\Node\Expr\MethodCall;
|
||||||
use PhpParser\Node\Expr\PropertyFetch;
|
use PhpParser\Node\Expr\PropertyFetch;
|
||||||
use Rector\NodeAnalyzer\PropertyFetchAnalyzer;
|
use Rector\NodeAnalyzer\PropertyFetchAnalyzer;
|
||||||
use Rector\Rector\AbstractRector;
|
use Rector\Rector\AbstractRector;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Example - from:
|
||||||
|
* - $result = $object->property;
|
||||||
|
* - $object->property = $value;
|
||||||
|
*
|
||||||
|
* To
|
||||||
|
* - $result = $object->getProperty();
|
||||||
|
* - $object->setProperty($value);
|
||||||
|
*/
|
||||||
final class PropertyToMethodRector extends AbstractRector
|
final class PropertyToMethodRector extends AbstractRector
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
@ -20,9 +31,9 @@ final class PropertyToMethodRector extends AbstractRector
|
|||||||
private $perClassPropertyToMethods = [];
|
private $perClassPropertyToMethods = [];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var string[]
|
* @var string
|
||||||
*/
|
*/
|
||||||
private $activeTypes = [];
|
private $activeMethod;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var PropertyFetchAnalyzer
|
* @var PropertyFetchAnalyzer
|
||||||
@ -40,13 +51,49 @@ final class PropertyToMethodRector extends AbstractRector
|
|||||||
|
|
||||||
public function isCandidate(Node $node): bool
|
public function isCandidate(Node $node): bool
|
||||||
{
|
{
|
||||||
if (! $node instanceof PropertyFetch) {
|
if (! $node instanceof Assign) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// setter
|
||||||
|
if ($node->var instanceof PropertyFetch) {
|
||||||
|
return $this->processPropertyFetchCandidate($node->var, 'set');
|
||||||
|
}
|
||||||
|
|
||||||
|
// getter
|
||||||
|
if ($node->expr instanceof PropertyFetch) {
|
||||||
|
return $this->processPropertyFetchCandidate($node->expr, 'get');
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param Assign $assignNode
|
||||||
|
*/
|
||||||
|
public function refactor(Node $assignNode): ?Node
|
||||||
|
{
|
||||||
|
// setter
|
||||||
|
if ($assignNode->var instanceof PropertyFetch) {
|
||||||
|
$args = [new Arg($assignNode->expr)];
|
||||||
|
|
||||||
|
return new MethodCall($assignNode->var->var, $this->activeMethod, $args);
|
||||||
|
}
|
||||||
|
|
||||||
|
// getter
|
||||||
|
if ($assignNode->expr instanceof PropertyFetch) {
|
||||||
|
$assignNode->expr = new MethodCall($assignNode->expr->var, $this->activeMethod);
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
private function processPropertyFetchCandidate(PropertyFetch $propertyFetchNode, string $type): bool
|
||||||
|
{
|
||||||
foreach ($this->perClassPropertyToMethods as $class => $propertyToMethods) {
|
foreach ($this->perClassPropertyToMethods as $class => $propertyToMethods) {
|
||||||
if ($this->propertyFetchAnalyzer->isTypeAndProperties($node, $class, array_keys($propertyToMethods))) {
|
$properties = array_keys($propertyToMethods);
|
||||||
$this->activeTypes = $propertyToMethods[$node->name->toString()];
|
if ($this->propertyFetchAnalyzer->isTypeAndProperties($propertyFetchNode, $class, $properties)) {
|
||||||
|
$this->activeMethod = $propertyToMethods[$propertyFetchNode->name->toString()][$type];
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@ -54,15 +101,4 @@ final class PropertyToMethodRector extends AbstractRector
|
|||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @param PropertyFetch $propertyFetchNode
|
|
||||||
*/
|
|
||||||
public function refactor(Node $propertyFetchNode): ?Node
|
|
||||||
{
|
|
||||||
return new MethodCall(
|
|
||||||
$propertyFetchNode->var,
|
|
||||||
$this->activeTypes[0]
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,6 @@
|
|||||||
rectors:
|
rectors:
|
||||||
Rector\Rector\Dynamic\PropertyToMethodRector:
|
Rector\Rector\Dynamic\PropertyToMethodRector:
|
||||||
'Translator':
|
'Translator':
|
||||||
'locale': ['getLocale', 'setLocale']
|
'locale':
|
||||||
|
'get': 'getLocale'
|
||||||
|
'set': 'setLocale'
|
||||||
|
Loading…
x
Reference in New Issue
Block a user