2018-05-01 17:31:41 +00:00
|
|
|
<?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);
|
2018-05-01 17:31:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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.',
|
2018-05-01 17:31:41 +00:00
|
|
|
[
|
|
|
|
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
|
2018-05-01 17:31:41 +00:00
|
|
|
),
|
|
|
|
]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2018-05-01 17:31:41 +00:00
|
|
|
foreach ($configurationArrays as $configurationArray) {
|
2018-05-02 06:02:55 +00:00
|
|
|
$this->argumentAdderRecipes[] = $argumentAdderRecipeFactory->createFromArray($configurationArray);
|
2018-05-01 17:31:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param mixed[] $argumentNodes
|
|
|
|
* @return mixed[]
|
|
|
|
*/
|
|
|
|
private function processArgumentNodes(array $argumentNodes): array
|
|
|
|
{
|
|
|
|
foreach ($this->activeArgumentAdderRecipes as $argumentReplacerRecipe) {
|
|
|
|
$position = $argumentReplacerRecipe->getPosition();
|
|
|
|
|
2018-05-02 16:55:07 +00:00
|
|
|
$argumentNodes[$position] = BuilderHelpers::normalizeValue($argumentReplacerRecipe->getDefaultValue());
|
2018-05-01 17:31:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $argumentNodes;
|
|
|
|
}
|
|
|
|
}
|