2018-05-01 17:31:41 +00:00
|
|
|
<?php declare(strict_types=1);
|
|
|
|
|
2018-08-01 18:29:40 +00:00
|
|
|
namespace Rector\Rector\Argument;
|
2018-05-01 17:31:41 +00:00
|
|
|
|
|
|
|
use PhpParser\BuilderHelpers;
|
|
|
|
use PhpParser\Node;
|
2018-10-21 19:43:47 +00:00
|
|
|
use PhpParser\Node\Arg;
|
2018-05-01 17:31:41 +00:00
|
|
|
use PhpParser\Node\Expr\MethodCall;
|
|
|
|
use PhpParser\Node\Expr\StaticCall;
|
2018-10-21 19:43:47 +00:00
|
|
|
use PhpParser\Node\Expr\Variable;
|
2019-03-08 20:56:34 +00:00
|
|
|
use PhpParser\Node\Identifier;
|
|
|
|
use PhpParser\Node\Name\FullyQualified;
|
2018-10-21 19:43:47 +00:00
|
|
|
use PhpParser\Node\Param;
|
2018-05-01 17:31:41 +00:00
|
|
|
use PhpParser\Node\Stmt\ClassMethod;
|
2018-10-21 19:43:47 +00:00
|
|
|
use Rector\Rector\AbstractRector;
|
2018-08-01 19:52:44 +00:00
|
|
|
use Rector\RectorDefinition\ConfiguredCodeSample;
|
2018-05-01 17:31:41 +00:00
|
|
|
use Rector\RectorDefinition\RectorDefinition;
|
|
|
|
|
2018-10-21 19:43:47 +00:00
|
|
|
final class ArgumentAdderRector extends AbstractRector
|
2018-05-01 17:31:41 +00:00
|
|
|
{
|
|
|
|
/**
|
2018-10-21 19:43:47 +00:00
|
|
|
* @var mixed[]
|
2018-05-01 17:31:41 +00:00
|
|
|
*/
|
2018-10-21 19:43:47 +00:00
|
|
|
private $positionWithDefaultValueByMethodNamesByClassTypes = [];
|
2018-05-01 17:31:41 +00:00
|
|
|
|
|
|
|
/**
|
2018-10-21 19:43:47 +00:00
|
|
|
* @param mixed[] $positionWithDefaultValueByMethodNamesByClassTypes
|
2018-05-01 17:31:41 +00:00
|
|
|
*/
|
2019-04-26 13:32:24 +00:00
|
|
|
public function __construct(array $positionWithDefaultValueByMethodNamesByClassTypes = [])
|
2018-07-18 12:56:04 +00:00
|
|
|
{
|
2018-10-21 19:43:47 +00:00
|
|
|
$this->positionWithDefaultValueByMethodNamesByClassTypes = $positionWithDefaultValueByMethodNamesByClassTypes;
|
2018-05-01 17:31:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getDefinition(): RectorDefinition
|
|
|
|
{
|
2018-10-21 19:43:47 +00:00
|
|
|
$configuration = [
|
|
|
|
'SomeExampleClass' => [
|
|
|
|
'someMethod' => [
|
|
|
|
0 => [
|
2019-03-08 20:56:34 +00:00
|
|
|
'name' => 'someArgument',
|
2018-10-21 19:43:47 +00:00
|
|
|
'default_value' => 'true',
|
2019-03-08 20:56:34 +00:00
|
|
|
'type' => 'SomeType',
|
2018-10-21 19:43:47 +00:00
|
|
|
],
|
|
|
|
],
|
|
|
|
],
|
|
|
|
];
|
|
|
|
|
2018-05-01 17:31:41 +00:00
|
|
|
return new RectorDefinition(
|
2018-08-01 13:34:55 +00:00
|
|
|
'This Rector adds new default arguments in calls of defined methods and class types.',
|
2018-05-01 17:31:41 +00:00
|
|
|
[
|
2018-08-01 19:52:44 +00:00
|
|
|
new ConfiguredCodeSample(
|
2018-05-04 12:52:17 +00:00
|
|
|
<<<'CODE_SAMPLE'
|
2018-10-21 19:43:47 +00:00
|
|
|
$someObject = new SomeExampleClass;
|
2018-05-04 12:52:17 +00:00
|
|
|
$someObject->someMethod();
|
|
|
|
CODE_SAMPLE
|
|
|
|
,
|
|
|
|
<<<'CODE_SAMPLE'
|
2018-10-21 19:43:47 +00:00
|
|
|
$someObject = new SomeExampleClass;
|
2018-05-04 12:52:17 +00:00
|
|
|
$someObject->someMethod(true);
|
2018-05-04 13:01:35 +00:00
|
|
|
CODE_SAMPLE
|
2018-08-01 19:52:44 +00:00
|
|
|
,
|
2018-10-21 19:43:47 +00:00
|
|
|
$configuration
|
2018-05-04 13:01:35 +00:00
|
|
|
),
|
2018-08-01 19:52:44 +00:00
|
|
|
new ConfiguredCodeSample(
|
2018-05-04 13:01:35 +00:00
|
|
|
<<<'CODE_SAMPLE'
|
2018-10-21 19:43:47 +00:00
|
|
|
class MyCustomClass extends SomeExampleClass
|
2018-05-04 13:01:35 +00:00
|
|
|
{
|
|
|
|
public function someMethod()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
}
|
|
|
|
CODE_SAMPLE
|
|
|
|
,
|
|
|
|
<<<'CODE_SAMPLE'
|
2018-10-21 19:43:47 +00:00
|
|
|
class MyCustomClass extends SomeExampleClass
|
2018-05-04 13:01:35 +00:00
|
|
|
{
|
|
|
|
public function someMethod($value = true)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
}
|
2018-05-04 12:52:17 +00:00
|
|
|
CODE_SAMPLE
|
2018-08-01 19:52:44 +00:00
|
|
|
,
|
2018-10-21 19:43:47 +00:00
|
|
|
$configuration
|
2018-05-01 17:31:41 +00:00
|
|
|
),
|
|
|
|
]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-08-14 22:12:41 +00:00
|
|
|
/**
|
|
|
|
* @return string[]
|
|
|
|
*/
|
|
|
|
public function getNodeTypes(): array
|
2018-05-01 17:31:41 +00:00
|
|
|
{
|
2018-08-14 21:33:39 +00:00
|
|
|
return [MethodCall::class, StaticCall::class, ClassMethod::class];
|
2018-05-01 17:31:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param MethodCall|StaticCall|ClassMethod $node
|
|
|
|
*/
|
2018-08-14 22:12:41 +00:00
|
|
|
public function refactor(Node $node): ?Node
|
2018-05-01 17:31:41 +00:00
|
|
|
{
|
2018-10-21 19:43:47 +00:00
|
|
|
foreach ($this->positionWithDefaultValueByMethodNamesByClassTypes as $type => $positionWithDefaultValueByMethodNames) {
|
|
|
|
if (! $this->isType($node, $type)) {
|
|
|
|
continue;
|
|
|
|
}
|
2018-05-01 17:31:41 +00:00
|
|
|
|
2018-10-21 19:43:47 +00:00
|
|
|
foreach ($positionWithDefaultValueByMethodNames as $method => $positionWithDefaultValues) {
|
|
|
|
if (! $this->isName($node, $method)) {
|
|
|
|
continue;
|
|
|
|
}
|
2018-05-01 17:31:41 +00:00
|
|
|
|
2018-10-21 19:43:47 +00:00
|
|
|
$this->processPositionWithDefaultValues($node, $positionWithDefaultValues);
|
2018-05-01 17:31:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-21 19:43:47 +00:00
|
|
|
return $node;
|
2018-05-01 17:31:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-10-21 19:43:47 +00:00
|
|
|
* @param ClassMethod|MethodCall|StaticCall $node
|
|
|
|
* @param mixed[] $positionWithDefaultValues
|
2018-05-01 17:31:41 +00:00
|
|
|
*/
|
2018-10-21 19:43:47 +00:00
|
|
|
private function processPositionWithDefaultValues(Node $node, array $positionWithDefaultValues): void
|
2018-05-01 17:31:41 +00:00
|
|
|
{
|
2019-03-08 20:56:34 +00:00
|
|
|
foreach ($positionWithDefaultValues as $position => $parameterConfiguration) {
|
|
|
|
$name = $parameterConfiguration['name'];
|
|
|
|
$defaultValue = $parameterConfiguration['default_value'] ?? null;
|
|
|
|
$type = $parameterConfiguration['type'] ?? null;
|
2018-10-21 19:43:47 +00:00
|
|
|
|
2019-03-08 23:40:33 +00:00
|
|
|
if ($this->shouldSkipParameter($node, $position, $name)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-07-02 17:53:44 +00:00
|
|
|
// is correct scope?
|
|
|
|
if (! $this->isInCorrectScope($node, $parameterConfiguration)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-10-21 19:43:47 +00:00
|
|
|
if ($node instanceof ClassMethod) {
|
2019-03-08 20:56:34 +00:00
|
|
|
$this->addClassMethodParam($node, $name, $defaultValue, $type, $position);
|
2019-07-02 17:53:44 +00:00
|
|
|
} elseif ($node instanceof StaticCall && $this->isName($node->class, 'parent')) {
|
|
|
|
$node->args[$position] = new Arg(new Variable($name));
|
2018-10-21 19:43:47 +00:00
|
|
|
} else {
|
2019-03-08 20:56:34 +00:00
|
|
|
$arg = new Arg(BuilderHelpers::normalizeValue($defaultValue));
|
|
|
|
$node->args[$position] = $arg;
|
2018-10-21 19:43:47 +00:00
|
|
|
}
|
2018-05-01 17:31:41 +00:00
|
|
|
}
|
|
|
|
}
|
2019-03-08 20:56:34 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param mixed $defaultValue
|
|
|
|
*/
|
|
|
|
private function addClassMethodParam(
|
|
|
|
ClassMethod $classMethod,
|
|
|
|
string $name,
|
|
|
|
$defaultValue,
|
|
|
|
?string $type,
|
|
|
|
int $position
|
|
|
|
): void {
|
|
|
|
$param = new Param(new Variable($name), BuilderHelpers::normalizeValue($defaultValue));
|
|
|
|
if ($type) {
|
|
|
|
$param->type = ctype_upper($type[0]) ? new FullyQualified($type) : new Identifier($type);
|
|
|
|
}
|
|
|
|
|
|
|
|
$classMethod->params[$position] = $param;
|
|
|
|
}
|
2019-03-08 23:40:33 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param ClassMethod|MethodCall|StaticCall $node
|
|
|
|
*/
|
|
|
|
private function shouldSkipParameter(Node $node, int $position, string $name): bool
|
|
|
|
{
|
|
|
|
if ($node instanceof ClassMethod) {
|
|
|
|
// already added?
|
|
|
|
return isset($node->params[$position]) && $this->isName($node->params[$position], $name);
|
|
|
|
}
|
|
|
|
|
|
|
|
// already added?
|
|
|
|
return isset($node->args[$position]) && $this->isName($node->args[$position], $name);
|
|
|
|
}
|
2019-07-02 17:53:44 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param ClassMethod|MethodCall|StaticCall $node
|
|
|
|
* @param mixed[] $parameterConfiguration
|
|
|
|
*/
|
|
|
|
private function isInCorrectScope(Node $node, array $parameterConfiguration): bool
|
|
|
|
{
|
|
|
|
if (! isset($parameterConfiguration['scope'])) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @var string[] $scope */
|
|
|
|
$scope = $parameterConfiguration['scope'];
|
|
|
|
|
|
|
|
if ($node instanceof ClassMethod) {
|
|
|
|
return in_array('class_method', $scope, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($node instanceof StaticCall) {
|
|
|
|
if ($this->isName($node->class, 'parent')) {
|
|
|
|
return in_array('parent_call', $scope, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
return in_array('method_call', $scope, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($node instanceof MethodCall) {
|
|
|
|
return in_array('method_call', $scope, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2018-05-01 17:31:41 +00:00
|
|
|
}
|