2021-01-22 00:17:43 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace Rector\Php80\Rector\ClassMethod;
|
|
|
|
|
|
|
|
use PhpParser\Node;
|
2021-01-22 09:16:00 +00:00
|
|
|
use PhpParser\Node\Expr\MethodCall;
|
2021-01-22 01:41:09 +00:00
|
|
|
use PhpParser\Node\Expr\New_;
|
2021-01-22 00:17:43 +00:00
|
|
|
use PhpParser\Node\Param;
|
|
|
|
use PhpParser\Node\Stmt\ClassMethod;
|
|
|
|
use Rector\Core\Rector\AbstractRector;
|
2021-01-22 01:41:09 +00:00
|
|
|
use Rector\NodeTypeResolver\Node\AttributeKey;
|
2021-01-22 09:16:00 +00:00
|
|
|
use Rector\Php80\NodeResolver\ArgumentSorter;
|
2021-01-22 01:41:09 +00:00
|
|
|
use Rector\Php80\NodeResolver\RequireOptionalParamResolver;
|
2021-01-22 00:17:43 +00:00
|
|
|
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
|
|
|
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @see https://php.watch/versions/8.0#deprecate-required-param-after-optional
|
|
|
|
*
|
|
|
|
* @see \Rector\Php80\Tests\Rector\ClassMethod\OptionalParametersAfterRequiredRector\OptionalParametersAfterRequiredRectorTest
|
|
|
|
*/
|
|
|
|
final class OptionalParametersAfterRequiredRector extends AbstractRector
|
|
|
|
{
|
2021-01-22 01:41:09 +00:00
|
|
|
/**
|
|
|
|
* @var RequireOptionalParamResolver
|
|
|
|
*/
|
|
|
|
private $requireOptionalParamResolver;
|
|
|
|
|
2021-01-22 09:16:00 +00:00
|
|
|
/**
|
|
|
|
* @var ArgumentSorter
|
|
|
|
*/
|
|
|
|
private $argumentSorter;
|
|
|
|
|
|
|
|
public function __construct(
|
|
|
|
RequireOptionalParamResolver $requireOptionalParamResolver,
|
|
|
|
ArgumentSorter $argumentSorter
|
|
|
|
) {
|
2021-01-22 01:41:09 +00:00
|
|
|
$this->requireOptionalParamResolver = $requireOptionalParamResolver;
|
2021-01-22 09:16:00 +00:00
|
|
|
$this->argumentSorter = $argumentSorter;
|
2021-01-22 01:41:09 +00:00
|
|
|
}
|
|
|
|
|
2021-01-22 00:17:43 +00:00
|
|
|
public function getRuleDefinition(): RuleDefinition
|
|
|
|
{
|
|
|
|
return new RuleDefinition('Move required parameters after optional ones', [
|
|
|
|
new CodeSample(
|
|
|
|
<<<'CODE_SAMPLE'
|
|
|
|
class SomeObject
|
|
|
|
{
|
|
|
|
public function run($optional = 1, $required)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
}
|
|
|
|
CODE_SAMPLE
|
|
|
|
|
|
|
|
,
|
|
|
|
<<<'CODE_SAMPLE'
|
|
|
|
class SomeObject
|
|
|
|
{
|
|
|
|
public function run($required, $optional = 1)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
}
|
|
|
|
CODE_SAMPLE
|
|
|
|
|
|
|
|
),
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string[]
|
|
|
|
*/
|
|
|
|
public function getNodeTypes(): array
|
|
|
|
{
|
2021-01-22 09:16:00 +00:00
|
|
|
return [ClassMethod::class, New_::class, MethodCall::class];
|
2021-01-22 00:17:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-01-22 09:16:00 +00:00
|
|
|
* @param ClassMethod|New_|MethodCall $node
|
2021-01-22 00:17:43 +00:00
|
|
|
*/
|
|
|
|
public function refactor(Node $node): ?Node
|
|
|
|
{
|
2021-01-22 01:41:09 +00:00
|
|
|
if ($node instanceof ClassMethod) {
|
|
|
|
return $this->refactorClassMethod($node);
|
|
|
|
}
|
|
|
|
|
2021-01-22 09:16:00 +00:00
|
|
|
if ($node instanceof New_) {
|
|
|
|
return $this->refactorNew($node);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->refactorMethodCall($node);
|
2021-01-22 01:41:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private function refactorClassMethod(ClassMethod $classMethod): ?ClassMethod
|
|
|
|
{
|
|
|
|
if ($classMethod->params === []) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$expectedOrderParams = $this->requireOptionalParamResolver->resolve($classMethod);
|
|
|
|
if ($classMethod->params === $expectedOrderParams) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$classMethod->params = $expectedOrderParams;
|
|
|
|
|
|
|
|
return $classMethod;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function refactorNew(New_ $new): ?New_
|
|
|
|
{
|
|
|
|
if ($new->args === []) {
|
2021-01-22 00:17:43 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2021-01-22 09:16:00 +00:00
|
|
|
$constructorClassMethod = $this->nodeRepository->findClassMethodConstructorByNew($new);
|
2021-01-22 01:41:09 +00:00
|
|
|
if (! $constructorClassMethod instanceof ClassMethod) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
// we need orignal node, as the order might have already hcanged
|
|
|
|
$originalClassMethod = $constructorClassMethod->getAttribute(AttributeKey::ORIGINAL_NODE);
|
|
|
|
if (! $originalClassMethod instanceof ClassMethod) {
|
|
|
|
return null;
|
|
|
|
}
|
2021-01-22 00:17:43 +00:00
|
|
|
|
2021-01-22 01:41:09 +00:00
|
|
|
$expectedOrderedParams = $this->requireOptionalParamResolver->resolve($originalClassMethod);
|
2021-01-22 09:16:00 +00:00
|
|
|
if ($expectedOrderedParams === $constructorClassMethod->getParams()) {
|
2021-01-22 00:17:43 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2021-01-22 09:16:00 +00:00
|
|
|
$newArgs = $this->argumentSorter->sortArgsByExpectedParamOrder($new->args, $expectedOrderedParams);
|
2021-01-22 01:41:09 +00:00
|
|
|
if ($new->args === $newArgs) {
|
|
|
|
return null;
|
|
|
|
}
|
2021-01-22 00:17:43 +00:00
|
|
|
|
2021-01-22 01:41:09 +00:00
|
|
|
$new->args = $newArgs;
|
|
|
|
return $new;
|
2021-01-22 00:17:43 +00:00
|
|
|
}
|
|
|
|
|
2021-01-22 09:16:00 +00:00
|
|
|
private function refactorMethodCall(MethodCall $methodCall): ?MethodCall
|
2021-01-22 00:17:43 +00:00
|
|
|
{
|
2021-01-22 09:16:00 +00:00
|
|
|
$classMethod = $this->nodeRepository->findClassMethodByMethodCall($methodCall);
|
|
|
|
if (! $classMethod instanceof ClassMethod) {
|
2021-01-22 01:41:09 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2021-01-22 09:16:00 +00:00
|
|
|
// because parameters can be already changed
|
|
|
|
$originalClassMethod = $classMethod->getAttribute(AttributeKey::ORIGINAL_NODE);
|
|
|
|
if (! $originalClassMethod instanceof ClassMethod) {
|
2021-01-22 01:41:09 +00:00
|
|
|
return null;
|
|
|
|
}
|
2021-01-22 00:17:43 +00:00
|
|
|
|
2021-01-22 09:16:00 +00:00
|
|
|
$expectedOrderedParams = $this->requireOptionalParamResolver->resolve($originalClassMethod);
|
|
|
|
if ($expectedOrderedParams === $classMethod->getParams()) {
|
2021-01-22 01:41:09 +00:00
|
|
|
return null;
|
2021-01-22 00:17:43 +00:00
|
|
|
}
|
|
|
|
|
2021-01-22 09:16:00 +00:00
|
|
|
$newArgs = $this->argumentSorter->sortArgsByExpectedParamOrder($methodCall->args, $expectedOrderedParams);
|
|
|
|
if ($methodCall->args === $newArgs) {
|
|
|
|
return null;
|
2021-01-22 00:17:43 +00:00
|
|
|
}
|
2021-01-22 09:16:00 +00:00
|
|
|
|
|
|
|
$methodCall->args = $newArgs;
|
|
|
|
return $methodCall;
|
2021-01-22 00:17:43 +00:00
|
|
|
}
|
|
|
|
}
|