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

265 lines
7.4 KiB
PHP
Raw Normal View History

2019-10-13 05:59:52 +00:00
<?php
declare(strict_types=1);
namespace Rector\Arguments\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;
use Rector\Arguments\NodeAnalyzer\ArgumentAddingScope;
use Rector\Arguments\ValueObject\ArgumentAdder;
2020-07-29 23:39:41 +00:00
use Rector\Core\Contract\Rector\ConfigurableRectorInterface;
2020-08-25 21:25:00 +00:00
use Rector\Core\Exception\ShouldNotHappenException;
use Rector\Core\Rector\AbstractRector;
2019-09-04 12:10:29 +00:00
use Rector\NodeTypeResolver\Node\AttributeKey;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
2020-08-25 21:25:00 +00:00
use Webmozart\Assert\Assert;
2019-09-03 09:11:45 +00:00
/**
* @see \Rector\Arguments\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
{
/**
* @var string
*/
public const ADDED_ARGUMENTS = 'added_arguments';
2020-07-29 23:39:41 +00:00
/**
* @var ArgumentAdder[]
2020-08-25 21:25:00 +00:00
*/
private $addedArguments = [];
/**
* @var ArgumentAddingScope
2020-08-25 21:25:00 +00:00
*/
private $argumentAddingScope;
2020-08-25 21:25:00 +00:00
public function __construct(ArgumentAddingScope $argumentAddingScope)
{
$this->argumentAddingScope = $argumentAddingScope;
}
public function getRuleDefinition(): RuleDefinition
{
2020-08-25 21:25:00 +00:00
$exampleConfiguration = [
self::ADDED_ARGUMENTS => [
new ArgumentAdder('SomeExampleClass', 'someMethod', 0, 'someArgument', 'true', 'SomeType'),
2020-08-25 21:25:00 +00:00
],
];
return new RuleDefinition(
'This Rector adds new default arguments in calls of defined methods and class types.',
[
2018-08-01 19:52:44 +00:00
new ConfiguredCodeSample(
<<<'CODE_SAMPLE'
$someObject = new SomeExampleClass;
2018-05-04 12:52:17 +00:00
$someObject->someMethod();
CODE_SAMPLE
2018-05-04 12:52:17 +00:00
,
<<<'CODE_SAMPLE'
$someObject = new SomeExampleClass;
2018-05-04 12:52:17 +00:00
$someObject->someMethod(true);
CODE_SAMPLE
2018-08-01 19:52:44 +00:00
,
2020-08-25 21:25:00 +00:00
$exampleConfiguration
),
2018-08-01 19:52:44 +00:00
new ConfiguredCodeSample(
<<<'CODE_SAMPLE'
class MyCustomClass extends SomeExampleClass
{
public function someMethod()
{
}
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
class MyCustomClass extends SomeExampleClass
{
public function someMethod($value = true)
{
}
}
CODE_SAMPLE
2018-08-01 19:52:44 +00:00
,
2020-08-25 21:25:00 +00:00
$exampleConfiguration
),
]
);
}
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
{
2020-08-25 21:25:00 +00:00
foreach ($this->addedArguments as $addedArgument) {
if (! $this->isObjectTypeMatch($node, $addedArgument->getClass())) {
continue;
}
2020-08-25 21:25:00 +00:00
if (! $this->isName($node->name, $addedArgument->getMethod())) {
continue;
}
2020-08-25 21:25:00 +00:00
$this->processPositionWithDefaultValues($node, $addedArgument);
}
return $node;
}
/**
* @param array<string, ArgumentAdder[]> $configuration
*/
2020-07-29 23:39:41 +00:00
public function configure(array $configuration): void
{
$addedArguments = $configuration[self::ADDED_ARGUMENTS] ?? [];
Assert::allIsInstanceOf($addedArguments, ArgumentAdder::class);
2020-08-25 21:25:00 +00:00
$this->addedArguments = $addedArguments;
2020-07-29 23:39:41 +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);
// anonymous class
if (! $classLike instanceof Class_) {
return false;
}
2020-07-19 18:58:54 +00:00
return $this->isObjectType($classLike, $type);
}
/**
* @param ClassMethod|MethodCall|StaticCall $node
*/
private function processPositionWithDefaultValues(Node $node, ArgumentAdder $argumentAdder): void
{
if ($this->shouldSkipParameter($node, $argumentAdder)) {
2020-08-25 21:25:00 +00:00
return;
}
$defaultValue = $argumentAdder->getArgumentDefaultValue();
$argumentType = $argumentAdder->getArgumentType();
2020-08-25 21:25:00 +00:00
$position = $argumentAdder->getPosition();
2020-08-25 21:25:00 +00:00
if ($node instanceof ClassMethod) {
$this->addClassMethodParam($node, $argumentAdder, $defaultValue, $argumentType, $position);
2020-08-25 21:25:00 +00:00
} elseif ($node instanceof StaticCall) {
$this->processStaticCall($node, $position, $argumentAdder);
2020-08-25 21:25:00 +00:00
} else {
$arg = new Arg(BuilderHelpers::normalizeValue($defaultValue));
if (isset($node->args[$position])) {
return;
}
2020-08-25 21:25:00 +00:00
$node->args[$position] = $arg;
}
}
2019-03-08 20:56:34 +00:00
/**
* @param ClassMethod|MethodCall|StaticCall $node
*/
private function shouldSkipParameter(Node $node, ArgumentAdder $argumentAdder): bool
{
$position = $argumentAdder->getPosition();
$argumentName = $argumentAdder->getArgumentName();
2020-08-25 21:25:00 +00:00
if ($argumentName === null) {
return true;
}
if ($node instanceof ClassMethod) {
// already added?
if (! isset($node->params[$position])) {
return false;
}
return $this->isName($node->params[$position], $argumentName);
}
// already added?
2020-08-25 21:25:00 +00:00
if (isset($node->args[$position]) && $this->isName($node->args[$position], $argumentName)) {
return true;
}
// is correct scope?
return ! $this->argumentAddingScope->isInCorrectScope($node, $argumentAdder);
}
2020-07-27 06:56:25 +00:00
/**
* @param mixed $defaultValue
*/
2020-02-01 16:04:38 +00:00
private function addClassMethodParam(
ClassMethod $classMethod,
ArgumentAdder $argumentAdder,
2020-02-01 16:04:38 +00:00
$defaultValue,
?string $type,
int $position
): void {
$argumentName = $argumentAdder->getArgumentName();
if ($argumentName === null) {
throw new ShouldNotHappenException();
}
$param = new Param(new Variable($argumentName), BuilderHelpers::normalizeValue($defaultValue));
2020-02-01 16:04:38 +00:00
if ($type) {
$param->type = ctype_upper($type[0]) ? new FullyQualified($type) : new Identifier($type);
}
$classMethod->params[$position] = $param;
}
private function processStaticCall(StaticCall $staticCall, int $position, ArgumentAdder $argumentAdder): void
2020-02-01 16:04:38 +00:00
{
$argumentName = $argumentAdder->getArgumentName();
if ($argumentName === null) {
throw new ShouldNotHappenException();
}
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($argumentName));
2020-02-01 16:04:38 +00:00
}
}