2019-10-13 05:59:52 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
2018-05-01 17:31:41 +00:00
|
|
|
|
2020-08-18 15:57:30 +00:00
|
|
|
namespace Rector\Generic\Rector\ClassMethod;
|
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;
|
2019-12-04 21:06:09 +00:00
|
|
|
use PhpParser\Node\Name;
|
2019-03-08 20:56:34 +00:00
|
|
|
use PhpParser\Node\Name\FullyQualified;
|
2018-10-21 19:43:47 +00:00
|
|
|
use PhpParser\Node\Param;
|
2019-09-06 10:30:58 +00:00
|
|
|
use PhpParser\Node\Stmt\Class_;
|
2018-05-01 17:31:41 +00:00
|
|
|
use PhpParser\Node\Stmt\ClassMethod;
|
2020-07-29 23:39:41 +00:00
|
|
|
use Rector\Core\Contract\Rector\ConfigurableRectorInterface;
|
2020-02-06 21:48:18 +00:00
|
|
|
use Rector\Core\Rector\AbstractRector;
|
|
|
|
use Rector\Core\RectorDefinition\ConfiguredCodeSample;
|
|
|
|
use Rector\Core\RectorDefinition\RectorDefinition;
|
2019-09-04 12:10:29 +00:00
|
|
|
use Rector\NodeTypeResolver\Node\AttributeKey;
|
2018-05-01 17:31:41 +00:00
|
|
|
|
2019-09-03 09:11:45 +00:00
|
|
|
/**
|
2020-08-18 15:57:30 +00:00
|
|
|
* @see \Rector\Generic\Tests\Rector\ClassMethod\ArgumentAdderRector\ArgumentAdderRectorTest
|
2019-09-03 09:11:45 +00:00
|
|
|
*/
|
2020-07-29 23:39:41 +00:00
|
|
|
final class ArgumentAdderRector extends AbstractRector implements ConfigurableRectorInterface
|
2018-05-01 17:31:41 +00:00
|
|
|
{
|
2020-07-29 23:39:41 +00:00
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public const POSITION_WITH_DEFAULT_VALUE_BY_METHOD_NAMES_BY_CLASS_TYPES = '$positionWithDefaultValueByMethodNamesByClassTypes';
|
|
|
|
|
2020-02-17 15:13:38 +00:00
|
|
|
/**
|
|
|
|
* @var string[][][][][]
|
|
|
|
*/
|
|
|
|
private const CONFIGURATION = [
|
2020-08-03 07:26:01 +00:00
|
|
|
self::POSITION_WITH_DEFAULT_VALUE_BY_METHOD_NAMES_BY_CLASS_TYPES => [
|
2020-02-17 15:13:38 +00:00
|
|
|
'SomeExampleClass' => [
|
|
|
|
'someMethod' => [
|
|
|
|
0 => [
|
|
|
|
'name' => 'someArgument',
|
|
|
|
'default_value' => 'true',
|
|
|
|
'type' => 'SomeType',
|
|
|
|
],
|
|
|
|
],
|
|
|
|
],
|
|
|
|
],
|
|
|
|
];
|
|
|
|
|
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
|
|
|
|
|
|
|
public function getDefinition(): RectorDefinition
|
|
|
|
{
|
|
|
|
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(
|
2019-09-18 06:14:35 +00:00
|
|
|
<<<'PHP'
|
2018-10-21 19:43:47 +00:00
|
|
|
$someObject = new SomeExampleClass;
|
2018-05-04 12:52:17 +00:00
|
|
|
$someObject->someMethod();
|
2019-09-18 06:14:35 +00:00
|
|
|
PHP
|
2018-05-04 12:52:17 +00:00
|
|
|
,
|
2019-09-18 06:14:35 +00:00
|
|
|
<<<'PHP'
|
2018-10-21 19:43:47 +00:00
|
|
|
$someObject = new SomeExampleClass;
|
2018-05-04 12:52:17 +00:00
|
|
|
$someObject->someMethod(true);
|
2019-09-18 06:14:35 +00:00
|
|
|
PHP
|
2018-08-01 19:52:44 +00:00
|
|
|
,
|
2020-02-17 15:13:38 +00:00
|
|
|
self::CONFIGURATION
|
2018-05-04 13:01:35 +00:00
|
|
|
),
|
2018-08-01 19:52:44 +00:00
|
|
|
new ConfiguredCodeSample(
|
2019-09-18 06:14:35 +00:00
|
|
|
<<<'PHP'
|
2018-10-21 19:43:47 +00:00
|
|
|
class MyCustomClass extends SomeExampleClass
|
2018-05-04 13:01:35 +00:00
|
|
|
{
|
|
|
|
public function someMethod()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
}
|
2019-09-18 06:14:35 +00:00
|
|
|
PHP
|
2018-05-04 13:01:35 +00:00
|
|
|
,
|
2019-09-18 06:14:35 +00:00
|
|
|
<<<'PHP'
|
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)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
}
|
2019-09-18 06:14:35 +00:00
|
|
|
PHP
|
2018-08-01 19:52:44 +00:00
|
|
|
,
|
2020-02-17 15:13:38 +00:00
|
|
|
self::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) {
|
2019-09-04 12:10:29 +00:00
|
|
|
if (! $this->isObjectTypeMatch($node, $type)) {
|
2018-10-21 19:43:47 +00:00
|
|
|
continue;
|
|
|
|
}
|
2018-05-01 17:31:41 +00:00
|
|
|
|
2018-10-21 19:43:47 +00:00
|
|
|
foreach ($positionWithDefaultValueByMethodNames as $method => $positionWithDefaultValues) {
|
2019-10-24 09:25:30 +00:00
|
|
|
if (! $this->isName($node->name, $method)) {
|
2018-10-21 19:43:47 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-07-29 23:39:41 +00:00
|
|
|
public function configure(array $configuration): void
|
|
|
|
{
|
|
|
|
$this->positionWithDefaultValueByMethodNamesByClassTypes = $configuration[self::POSITION_WITH_DEFAULT_VALUE_BY_METHOD_NAMES_BY_CLASS_TYPES] ?? [];
|
|
|
|
}
|
|
|
|
|
2019-10-30 09:49:07 +00:00
|
|
|
/**
|
|
|
|
* @param MethodCall|StaticCall|ClassMethod $node
|
|
|
|
*/
|
|
|
|
private function isObjectTypeMatch(Node $node, string $type): bool
|
|
|
|
{
|
|
|
|
if ($node instanceof MethodCall) {
|
|
|
|
return $this->isObjectType($node->var, $type);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($node instanceof StaticCall) {
|
|
|
|
return $this->isObjectType($node->class, $type);
|
|
|
|
}
|
|
|
|
|
|
|
|
// ClassMethod
|
2020-07-19 18:58:54 +00:00
|
|
|
/** @var Class_|null $classLike */
|
|
|
|
$classLike = $node->getAttribute(AttributeKey::CLASS_NODE);
|
2019-10-30 09:49:07 +00:00
|
|
|
|
|
|
|
// anonymous class
|
2020-07-19 18:58:54 +00:00
|
|
|
if ($classLike === null) {
|
2019-10-30 09:49:07 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-07-19 18:58:54 +00:00
|
|
|
return $this->isObjectType($classLike, $type);
|
2019-10-30 09:49:07 +00:00
|
|
|
}
|
|
|
|
|
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-12-04 21:06:09 +00:00
|
|
|
if ($this->shouldSkipParameter($node, $position, $name, $parameterConfiguration)) {
|
2019-07-02 17:53:44 +00:00
|
|
|
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-12-04 21:06:09 +00:00
|
|
|
} elseif ($node instanceof StaticCall) {
|
|
|
|
$this->processStaticCall($node, $position, $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
|
|
|
|
2019-03-08 23:40:33 +00:00
|
|
|
/**
|
|
|
|
* @param ClassMethod|MethodCall|StaticCall $node
|
2019-12-04 21:06:09 +00:00
|
|
|
* @param mixed[] $parameterConfiguration
|
2019-03-08 23:40:33 +00:00
|
|
|
*/
|
2019-12-04 21:06:09 +00:00
|
|
|
private function shouldSkipParameter(Node $node, int $position, string $name, array $parameterConfiguration): bool
|
2019-03-08 23:40:33 +00:00
|
|
|
{
|
|
|
|
if ($node instanceof ClassMethod) {
|
|
|
|
// already added?
|
|
|
|
return isset($node->params[$position]) && $this->isName($node->params[$position], $name);
|
|
|
|
}
|
|
|
|
|
|
|
|
// already added?
|
2019-12-04 21:06:09 +00:00
|
|
|
if (isset($node->args[$position]) && $this->isName($node->args[$position], $name)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// is correct scope?
|
|
|
|
return ! $this->isInCorrectScope($node, $parameterConfiguration);
|
2019-03-08 23:40:33 +00:00
|
|
|
}
|
2019-07-02 17:53:44 +00:00
|
|
|
|
2020-07-27 06:56:25 +00:00
|
|
|
/**
|
|
|
|
* @param mixed $defaultValue
|
|
|
|
*/
|
2020-02-01 16:04:38 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-07-27 06:56:25 +00:00
|
|
|
private function processStaticCall(StaticCall $staticCall, int $position, string $name): void
|
2020-02-01 16:04:38 +00:00
|
|
|
{
|
|
|
|
if (! $staticCall->class instanceof Name) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (! $this->isName($staticCall->class, 'parent')) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$staticCall->args[$position] = new Arg(new Variable($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) {
|
2019-12-04 21:06:09 +00:00
|
|
|
if (! $node->class instanceof Name) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-07-02 17:53:44 +00:00
|
|
|
if ($this->isName($node->class, 'parent')) {
|
|
|
|
return in_array('parent_call', $scope, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
return in_array('method_call', $scope, true);
|
|
|
|
}
|
|
|
|
|
2019-10-02 22:32:59 +00:00
|
|
|
// MethodCall
|
|
|
|
return in_array('method_call', $scope, true);
|
2019-07-02 17:53:44 +00:00
|
|
|
}
|
2018-05-01 17:31:41 +00:00
|
|
|
}
|