2019-10-13 05:59:52 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
2018-05-01 17:31:41 +00:00
|
|
|
|
2021-02-20 23:21:19 +00:00
|
|
|
namespace Rector\Arguments\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;
|
2021-02-20 23:21:19 +00:00
|
|
|
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;
|
2020-02-06 21:48:18 +00:00
|
|
|
use Rector\Core\Rector\AbstractRector;
|
2019-09-04 12:10:29 +00:00
|
|
|
use Rector\NodeTypeResolver\Node\AttributeKey;
|
2020-11-16 17:50:38 +00:00
|
|
|
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
|
|
|
|
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
2020-08-25 21:25:00 +00:00
|
|
|
use Webmozart\Assert\Assert;
|
2018-05-01 17:31:41 +00:00
|
|
|
|
2019-09-03 09:11:45 +00:00
|
|
|
/**
|
2021-02-20 23:21:19 +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
|
2018-05-01 17:31:41 +00:00
|
|
|
{
|
2021-01-24 11:23:50 +00:00
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public const ADDED_ARGUMENTS = 'added_arguments';
|
|
|
|
|
2020-07-29 23:39:41 +00:00
|
|
|
/**
|
2021-01-17 15:43:47 +00:00
|
|
|
* @var ArgumentAdder[]
|
2020-08-25 21:25:00 +00:00
|
|
|
*/
|
2021-01-17 15:43:47 +00:00
|
|
|
private $addedArguments = [];
|
2020-02-17 15:13:38 +00:00
|
|
|
|
2018-05-01 17:31:41 +00:00
|
|
|
/**
|
2021-01-17 15:43:47 +00:00
|
|
|
* @var ArgumentAddingScope
|
2020-08-25 21:25:00 +00:00
|
|
|
*/
|
2021-01-17 15:43:47 +00:00
|
|
|
private $argumentAddingScope;
|
2020-08-25 21:25:00 +00:00
|
|
|
|
2021-01-17 15:43:47 +00:00
|
|
|
public function __construct(ArgumentAddingScope $argumentAddingScope)
|
|
|
|
{
|
|
|
|
$this->argumentAddingScope = $argumentAddingScope;
|
|
|
|
}
|
2018-05-01 17:31:41 +00:00
|
|
|
|
2020-11-16 17:50:38 +00:00
|
|
|
public function getRuleDefinition(): RuleDefinition
|
2018-05-01 17:31:41 +00:00
|
|
|
{
|
2020-08-25 21:25:00 +00:00
|
|
|
$exampleConfiguration = [
|
2021-01-24 11:23:50 +00:00
|
|
|
self::ADDED_ARGUMENTS => [
|
2020-09-12 21:19:08 +00:00
|
|
|
new ArgumentAdder('SomeExampleClass', 'someMethod', 0, 'someArgument', 'true', 'SomeType'),
|
2020-08-25 21:25:00 +00:00
|
|
|
],
|
|
|
|
];
|
|
|
|
|
2020-11-16 17:50:38 +00:00
|
|
|
return new RuleDefinition(
|
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(
|
2020-09-15 08:23:13 +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();
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2018-05-04 12:52:17 +00:00
|
|
|
,
|
2020-09-15 08:23:13 +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(true);
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2018-08-01 19:52:44 +00:00
|
|
|
,
|
2020-08-25 21:25:00 +00:00
|
|
|
$exampleConfiguration
|
2018-05-04 13:01:35 +00:00
|
|
|
),
|
2018-08-01 19:52:44 +00:00
|
|
|
new ConfiguredCodeSample(
|
2020-09-15 08:23:13 +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()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
}
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2018-05-04 13:01:35 +00:00
|
|
|
,
|
2020-09-15 08:23:13 +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($value = true)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
}
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2018-08-01 19:52:44 +00:00
|
|
|
,
|
2020-08-25 21:25:00 +00:00
|
|
|
$exampleConfiguration
|
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
|
|
|
{
|
2020-08-25 21:25:00 +00:00
|
|
|
foreach ($this->addedArguments as $addedArgument) {
|
|
|
|
if (! $this->isObjectTypeMatch($node, $addedArgument->getClass())) {
|
2018-10-21 19:43:47 +00:00
|
|
|
continue;
|
|
|
|
}
|
2018-05-01 17:31:41 +00:00
|
|
|
|
2020-08-25 21:25:00 +00:00
|
|
|
if (! $this->isName($node->name, $addedArgument->getMethod())) {
|
|
|
|
continue;
|
2018-05-01 17:31:41 +00:00
|
|
|
}
|
2020-08-25 21:25:00 +00:00
|
|
|
|
|
|
|
$this->processPositionWithDefaultValues($node, $addedArgument);
|
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-08-22 20:02:50 +00:00
|
|
|
/**
|
2021-01-24 11:23:50 +00:00
|
|
|
* @param array<string, ArgumentAdder[]> $configuration
|
2020-08-22 20:02:50 +00:00
|
|
|
*/
|
2020-07-29 23:39:41 +00:00
|
|
|
public function configure(array $configuration): void
|
|
|
|
{
|
2021-01-24 11:23:50 +00:00
|
|
|
$addedArguments = $configuration[self::ADDED_ARGUMENTS] ?? [];
|
2020-09-12 21:19:08 +00:00
|
|
|
Assert::allIsInstanceOf($addedArguments, ArgumentAdder::class);
|
2020-08-25 21:25:00 +00:00
|
|
|
$this->addedArguments = $addedArguments;
|
2020-07-29 23:39:41 +00:00
|
|
|
}
|
|
|
|
|
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
|
2021-01-19 11:24:48 +00:00
|
|
|
if (! $classLike instanceof Class_) {
|
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
|
2018-05-01 17:31:41 +00:00
|
|
|
*/
|
2020-09-12 21:19:08 +00:00
|
|
|
private function processPositionWithDefaultValues(Node $node, ArgumentAdder $argumentAdder): void
|
2018-05-01 17:31:41 +00:00
|
|
|
{
|
2020-09-12 21:19:08 +00:00
|
|
|
if ($this->shouldSkipParameter($node, $argumentAdder)) {
|
2020-08-25 21:25:00 +00:00
|
|
|
return;
|
|
|
|
}
|
2018-10-21 19:43:47 +00:00
|
|
|
|
2020-09-12 21:19:08 +00:00
|
|
|
$defaultValue = $argumentAdder->getArgumentDefaultValue();
|
|
|
|
$argumentType = $argumentAdder->getArgumentType();
|
2020-08-25 21:25:00 +00:00
|
|
|
|
2020-09-12 21:19:08 +00:00
|
|
|
$position = $argumentAdder->getPosition();
|
2019-07-02 17:53:44 +00:00
|
|
|
|
2020-08-25 21:25:00 +00:00
|
|
|
if ($node instanceof ClassMethod) {
|
2020-10-09 20:00:45 +00:00
|
|
|
$this->addClassMethodParam($node, $argumentAdder, $defaultValue, $argumentType, $position);
|
2020-08-25 21:25:00 +00:00
|
|
|
} elseif ($node instanceof StaticCall) {
|
2020-10-09 20:00:45 +00:00
|
|
|
$this->processStaticCall($node, $position, $argumentAdder);
|
2020-08-25 21:25:00 +00:00
|
|
|
} else {
|
|
|
|
$arg = new Arg(BuilderHelpers::normalizeValue($defaultValue));
|
2020-10-09 20:00:45 +00:00
|
|
|
if (isset($node->args[$position])) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-08-25 21:25:00 +00:00
|
|
|
$node->args[$position] = $arg;
|
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
|
|
|
|
*/
|
2020-09-12 21:19:08 +00:00
|
|
|
private function shouldSkipParameter(Node $node, ArgumentAdder $argumentAdder): bool
|
2019-03-08 23:40:33 +00:00
|
|
|
{
|
2020-09-12 21:19:08 +00:00
|
|
|
$position = $argumentAdder->getPosition();
|
|
|
|
$argumentName = $argumentAdder->getArgumentName();
|
2020-08-25 21:25:00 +00:00
|
|
|
|
2021-01-28 19:52:11 +00:00
|
|
|
if ($argumentName === null) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-03-08 23:40:33 +00:00
|
|
|
if ($node instanceof ClassMethod) {
|
|
|
|
// already added?
|
2020-12-19 15:24:53 +00:00
|
|
|
if (! isset($node->params[$position])) {
|
|
|
|
return false;
|
|
|
|
}
|
2021-01-28 19:52:11 +00:00
|
|
|
|
2020-12-19 15:24:53 +00:00
|
|
|
return $this->isName($node->params[$position], $argumentName);
|
2019-03-08 23:40:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// already added?
|
2020-08-25 21:25:00 +00:00
|
|
|
if (isset($node->args[$position]) && $this->isName($node->args[$position], $argumentName)) {
|
2019-12-04 21:06:09 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// is correct scope?
|
2021-01-17 15:43:47 +00:00
|
|
|
return ! $this->argumentAddingScope->isInCorrectScope($node, $argumentAdder);
|
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,
|
2020-10-09 20:00:45 +00:00
|
|
|
ArgumentAdder $argumentAdder,
|
2020-02-01 16:04:38 +00:00
|
|
|
$defaultValue,
|
|
|
|
?string $type,
|
|
|
|
int $position
|
|
|
|
): void {
|
2020-10-09 20:00:45 +00:00
|
|
|
$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;
|
|
|
|
}
|
|
|
|
|
2020-10-09 20:00:45 +00:00
|
|
|
private function processStaticCall(StaticCall $staticCall, int $position, ArgumentAdder $argumentAdder): void
|
2020-02-01 16:04:38 +00:00
|
|
|
{
|
2020-10-09 20:00:45 +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;
|
|
|
|
}
|
|
|
|
|
2020-10-09 20:00:45 +00:00
|
|
|
$staticCall->args[$position] = new Arg(new Variable($argumentName));
|
2020-02-01 16:04:38 +00:00
|
|
|
}
|
2018-05-01 17:31:41 +00:00
|
|
|
}
|