2019-10-13 05:59:52 +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\Assign;
|
2017-10-20 12:33:55 +00:00
|
|
|
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node;
|
|
|
|
use PhpParser\Node\Expr\Assign;
|
|
|
|
use PhpParser\Node\Expr\PropertyFetch;
|
|
|
|
use PhpParser\Node\Expr\Variable;
|
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\PropertyAssignToMethodCall;
|
2022-06-07 09:18:30 +00:00
|
|
|
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
|
|
|
|
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
2024-03-01 20:02:28 +00:00
|
|
|
use RectorPrefix202403\Webmozart\Assert\Assert;
|
2019-09-03 09:11:45 +00:00
|
|
|
/**
|
2021-03-12 22:20:25 +00:00
|
|
|
* @see \Rector\Tests\Transform\Rector\Assign\PropertyAssignToMethodCallRector\PropertyAssignToMethodCallRectorTest
|
2019-09-03 09:11:45 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
final class PropertyAssignToMethodCallRector extends AbstractRector implements ConfigurableRectorInterface
|
2017-10-20 12:33:55 +00:00
|
|
|
{
|
2018-06-06 13:12:07 +00:00
|
|
|
/**
|
2020-08-31 08:48:14 +00:00
|
|
|
* @var PropertyAssignToMethodCall[]
|
2018-06-06 13:12:07 +00:00
|
|
|
*/
|
2020-08-31 08:48:14 +00:00
|
|
|
private $propertyAssignsToMethodCalls = [];
|
2022-06-07 08:22:29 +00:00
|
|
|
public function getRuleDefinition() : RuleDefinition
|
2018-04-07 23:35:59 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return new RuleDefinition('Turns property assign of specific type and property name to method call', [new ConfiguredCodeSample(<<<'CODE_SAMPLE'
|
2020-02-06 21:48:18 +00:00
|
|
|
$someObject = new SomeClass;
|
2018-08-01 16:15:06 +00:00
|
|
|
$someObject->oldProperty = false;
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2021-05-09 20:15:43 +00:00
|
|
|
, <<<'CODE_SAMPLE'
|
2018-08-01 16:15:06 +00:00
|
|
|
$someObject = new SomeClass;
|
|
|
|
$someObject->newMethodCall(false);
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2022-06-07 08:22:29 +00:00
|
|
|
, [new PropertyAssignToMethodCall('SomeClass', 'oldProperty', 'newMethodCall')])]);
|
2018-04-07 23:35:59 +00:00
|
|
|
}
|
2018-08-14 22:12:41 +00:00
|
|
|
/**
|
2021-02-27 00:06:15 +00:00
|
|
|
* @return array<class-string<Node>>
|
2018-08-14 22:12:41 +00:00
|
|
|
*/
|
2021-05-09 20:15:43 +00:00
|
|
|
public function getNodeTypes() : array
|
2017-10-20 12:33:55 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return [Assign::class];
|
2017-10-20 12:33:55 +00:00
|
|
|
}
|
2017-10-20 12:46:59 +00:00
|
|
|
/**
|
2021-12-10 10:22:23 +00:00
|
|
|
* @param Assign $node
|
2017-10-20 12:46:59 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
public function refactor(Node $node) : ?Node
|
2017-10-20 12:33:55 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
if (!$node->var instanceof PropertyFetch) {
|
2018-10-15 04:36:58 +00:00
|
|
|
return null;
|
2018-08-14 21:33:39 +00:00
|
|
|
}
|
2018-10-15 04:36:58 +00:00
|
|
|
$propertyFetchNode = $node->var;
|
2017-11-06 13:15:35 +00:00
|
|
|
/** @var Variable $propertyNode */
|
2017-11-06 11:48:43 +00:00
|
|
|
$propertyNode = $propertyFetchNode->var;
|
2020-08-31 08:48:14 +00:00
|
|
|
foreach ($this->propertyAssignsToMethodCalls as $propertyAssignToMethodCall) {
|
2023-03-22 14:20:22 +00:00
|
|
|
if (!$this->isName($propertyFetchNode, $propertyAssignToMethodCall->getOldPropertyName())) {
|
2018-10-15 04:36:58 +00:00
|
|
|
continue;
|
|
|
|
}
|
2023-03-22 14:20:22 +00:00
|
|
|
if (!$this->isObjectType($propertyFetchNode->var, $propertyAssignToMethodCall->getObjectType())) {
|
2020-08-31 08:48:14 +00:00
|
|
|
continue;
|
2018-10-14 09:55:10 +00:00
|
|
|
}
|
2021-05-09 20:15:43 +00:00
|
|
|
return $this->nodeFactory->createMethodCall($propertyNode, $propertyAssignToMethodCall->getNewMethodName(), [$node->expr]);
|
2018-10-14 09:55:10 +00:00
|
|
|
}
|
2022-02-28 06:43:27 +00:00
|
|
|
return null;
|
2017-10-20 12:33:55 +00:00
|
|
|
}
|
2021-05-02 10:46:55 +00:00
|
|
|
/**
|
2021-11-28 17:01:20 +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-07-29 23:39:41 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
Assert::allIsAOf($configuration, PropertyAssignToMethodCall::class);
|
2022-02-18 01:46:17 +00:00
|
|
|
$this->propertyAssignsToMethodCalls = $configuration;
|
2020-07-29 23:39:41 +00:00
|
|
|
}
|
2017-10-20 12:33:55 +00:00
|
|
|
}
|