rector/rules/generic/src/Rector/ClassMethod/ArgumentAdderRector.php

270 lines
7.8 KiB
PHP
Raw Normal View History

2019-10-13 05:59:52 +00:00
<?php
declare(strict_types=1);
namespace Rector\Generic\Rector\ClassMethod;
use PhpParser\BuilderHelpers;
use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Expr\Variable;
2019-03-08 20:56:34 +00:00
use PhpParser\Node\Identifier;
use PhpParser\Node\Name;
2019-03-08 20:56:34 +00:00
use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\Param;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
2020-07-29 23:39:41 +00:00
use Rector\Core\Contract\Rector\ConfigurableRectorInterface;
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;
2019-09-03 09:11:45 +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
{
2020-07-29 23:39:41 +00:00
/**
* @var string
*/
public const POSITION_WITH_DEFAULT_VALUE_BY_METHOD_NAMES_BY_CLASS_TYPES = '$positionWithDefaultValueByMethodNamesByClassTypes';
/**
* @var string[][][][][]
*/
private const CONFIGURATION = [
self::POSITION_WITH_DEFAULT_VALUE_BY_METHOD_NAMES_BY_CLASS_TYPES => [
'SomeExampleClass' => [
'someMethod' => [
0 => [
'name' => 'someArgument',
'default_value' => 'true',
'type' => 'SomeType',
],
],
],
],
];
/**
* @var mixed[]
*/
private $positionWithDefaultValueByMethodNamesByClassTypes = [];
public function getDefinition(): RectorDefinition
{
return new RectorDefinition(
'This Rector adds new default arguments in calls of defined methods and class types.',
[
2018-08-01 19:52:44 +00:00
new ConfiguredCodeSample(
2019-09-18 06:14:35 +00:00
<<<'PHP'
$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'
$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
,
self::CONFIGURATION
),
2018-08-01 19:52:44 +00:00
new ConfiguredCodeSample(
2019-09-18 06:14:35 +00:00
<<<'PHP'
class MyCustomClass extends SomeExampleClass
{
public function someMethod()
{
}
}
2019-09-18 06:14:35 +00:00
PHP
,
2019-09-18 06:14:35 +00:00
<<<'PHP'
class MyCustomClass extends SomeExampleClass
{
public function someMethod($value = true)
{
}
}
2019-09-18 06:14:35 +00:00
PHP
2018-08-01 19:52:44 +00:00
,
self::CONFIGURATION
),
]
);
}
2018-08-14 22:12:41 +00:00
/**
* @return string[]
*/
public function getNodeTypes(): array
{
return [MethodCall::class, StaticCall::class, ClassMethod::class];
}
/**
* @param MethodCall|StaticCall|ClassMethod $node
*/
2018-08-14 22:12:41 +00:00
public function refactor(Node $node): ?Node
{
foreach ($this->positionWithDefaultValueByMethodNamesByClassTypes as $type => $positionWithDefaultValueByMethodNames) {
2019-09-04 12:10:29 +00:00
if (! $this->isObjectTypeMatch($node, $type)) {
continue;
}
foreach ($positionWithDefaultValueByMethodNames as $method => $positionWithDefaultValues) {
2019-10-24 09:25:30 +00:00
if (! $this->isName($node->name, $method)) {
continue;
}
$this->processPositionWithDefaultValues($node, $positionWithDefaultValues);
}
}
return $node;
}
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] ?? [];
}
/**
* @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);
// anonymous class
2020-07-19 18:58:54 +00:00
if ($classLike === null) {
return false;
}
2020-07-19 18:58:54 +00:00
return $this->isObjectType($classLike, $type);
}
/**
* @param ClassMethod|MethodCall|StaticCall $node
* @param mixed[] $positionWithDefaultValues
*/
private function processPositionWithDefaultValues(Node $node, array $positionWithDefaultValues): void
{
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;
if ($this->shouldSkipParameter($node, $position, $name, $parameterConfiguration)) {
continue;
}
if ($node instanceof ClassMethod) {
2019-03-08 20:56:34 +00:00
$this->addClassMethodParam($node, $name, $defaultValue, $type, $position);
} elseif ($node instanceof StaticCall) {
$this->processStaticCall($node, $position, $name);
} else {
2019-03-08 20:56:34 +00:00
$arg = new Arg(BuilderHelpers::normalizeValue($defaultValue));
$node->args[$position] = $arg;
}
}
}
2019-03-08 20:56:34 +00:00
/**
* @param ClassMethod|MethodCall|StaticCall $node
* @param mixed[] $parameterConfiguration
*/
private function shouldSkipParameter(Node $node, int $position, string $name, array $parameterConfiguration): bool
{
if ($node instanceof ClassMethod) {
// already added?
return isset($node->params[$position]) && $this->isName($node->params[$position], $name);
}
// already added?
if (isset($node->args[$position]) && $this->isName($node->args[$position], $name)) {
return true;
}
// is correct scope?
return ! $this->isInCorrectScope($node, $parameterConfiguration);
}
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));
}
/**
* @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 (! $node->class instanceof Name) {
return false;
}
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);
}
}