rector/src/Rector/Dynamic/ArgumentAdderRector.php

124 lines
3.5 KiB
PHP
Raw Normal View History

<?php declare(strict_types=1);
namespace Rector\Rector\Dynamic;
use PhpParser\BuilderHelpers;
use PhpParser\Node;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Stmt\ClassMethod;
use Rector\Configuration\Rector\ArgumentAdderRecipe;
use Rector\Configuration\Rector\ArgumentAdderRecipeFactory;
use Rector\RectorDefinition\CodeSample;
use Rector\RectorDefinition\RectorDefinition;
final class ArgumentAdderRector extends AbstractArgumentRector
{
/**
* @var ArgumentAdderRecipe[]
*/
private $argumentAdderRecipes = [];
/**
* @var ArgumentAdderRecipe[]
*/
private $activeArgumentAdderRecipes = [];
/**
* @param mixed[] $argumentChangesByMethodAndType
*/
public function __construct(
array $argumentChangesByMethodAndType,
ArgumentAdderRecipeFactory $argumentAdderRecipeFactory
) {
2018-05-02 06:02:55 +00:00
$this->loadArgumentReplacerRecipes($argumentAdderRecipeFactory, $argumentChangesByMethodAndType);
}
public function getDefinition(): RectorDefinition
{
return new RectorDefinition(
2018-05-04 12:52:17 +00:00
'[Dynamic] This Rector adds new default arguments in calls of defined methods and class types.',
[
new CodeSample(
2018-05-04 12:52:17 +00:00
<<<'CODE_SAMPLE'
$someObject = new SomeClass;
$someObject->someMethod();
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
$someObject = new SomeClass;
$someObject->someMethod(true);
CODE_SAMPLE
),
]
);
}
public function isCandidate(Node $node): bool
{
if (! $this->isValidInstance($node)) {
return false;
}
$this->activeArgumentAdderRecipes = $this->matchArgumentChanges($node);
return (bool) $this->activeArgumentAdderRecipes;
}
/**
* @param MethodCall|StaticCall|ClassMethod $node
*/
public function refactor(Node $node): Node
{
$argumentsOrParameters = $this->getNodeArgumentsOrParameters($node);
$argumentsOrParameters = $this->processArgumentNodes($argumentsOrParameters);
$this->setNodeArgumentsOrParameters($node, $argumentsOrParameters);
return $node;
}
/**
* @return ArgumentAdderRecipe[]
*/
private function matchArgumentChanges(Node $node): array
{
$argumentReplacerRecipes = [];
foreach ($this->argumentAdderRecipes as $argumentReplacerRecipe) {
if ($this->isNodeToRecipeMatch($node, $argumentReplacerRecipe)) {
$argumentReplacerRecipes[] = $argumentReplacerRecipe;
}
}
return $argumentReplacerRecipes;
}
/**
* @param mixed[] $configurationArrays
*/
2018-05-02 06:02:55 +00:00
private function loadArgumentReplacerRecipes(
ArgumentAdderRecipeFactory $argumentAdderRecipeFactory,
array $configurationArrays
): void {
foreach ($configurationArrays as $configurationArray) {
2018-05-02 06:02:55 +00:00
$this->argumentAdderRecipes[] = $argumentAdderRecipeFactory->createFromArray($configurationArray);
}
}
/**
* @param mixed[] $argumentNodes
* @return mixed[]
*/
private function processArgumentNodes(array $argumentNodes): array
{
foreach ($this->activeArgumentAdderRecipes as $argumentReplacerRecipe) {
$position = $argumentReplacerRecipe->getPosition();
$argumentNodes[$position] = BuilderHelpers::normalizeValue($argumentReplacerRecipe->getDefaultValue());
}
return $argumentNodes;
}
}