2021-01-22 00:17:43 +00:00
|
|
|
<?php
|
|
|
|
|
2021-05-09 20:15:43 +00:00
|
|
|
declare (strict_types=1);
|
2022-06-06 17:12:56 +00:00
|
|
|
namespace Rector\CodeQuality\Rector\ClassMethod;
|
2021-01-22 00:17:43 +00:00
|
|
|
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node;
|
|
|
|
use PhpParser\Node\Expr\MethodCall;
|
|
|
|
use PhpParser\Node\Expr\New_;
|
2022-08-22 10:08:19 +00:00
|
|
|
use PhpParser\Node\Expr\StaticCall;
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node\Stmt\ClassMethod;
|
2022-07-28 14:06:21 +00:00
|
|
|
use PHPStan\Analyser\Scope;
|
2022-06-06 17:12:56 +00:00
|
|
|
use PHPStan\Reflection\MethodReflection;
|
|
|
|
use Rector\CodingStyle\Reflection\VendorLocationDetector;
|
2022-07-28 14:06:21 +00:00
|
|
|
use Rector\NodeTypeResolver\PHPStan\ParametersAcceptorSelectorVariantsWrapper;
|
2022-06-06 17:12:56 +00:00
|
|
|
use Rector\Php80\NodeResolver\ArgumentSorter;
|
|
|
|
use Rector\Php80\NodeResolver\RequireOptionalParamResolver;
|
2024-01-02 02:40:38 +00:00
|
|
|
use Rector\Rector\AbstractScopeAwareRector;
|
|
|
|
use Rector\Reflection\ReflectionResolver;
|
2022-06-07 09:18:30 +00:00
|
|
|
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
|
|
|
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
2021-01-22 00:17:43 +00:00
|
|
|
/**
|
2021-04-10 18:47:17 +00:00
|
|
|
* @changelog https://php.watch/versions/8.0#deprecate-required-param-after-optional
|
2021-01-22 00:17:43 +00:00
|
|
|
*
|
2022-02-15 15:35:02 +00:00
|
|
|
* @see \Rector\Tests\CodeQuality\Rector\ClassMethod\OptionalParametersAfterRequiredRector\OptionalParametersAfterRequiredRectorTest
|
2021-01-22 00:17:43 +00:00
|
|
|
*/
|
2022-12-13 12:09:37 +00:00
|
|
|
final class OptionalParametersAfterRequiredRector extends AbstractScopeAwareRector
|
2021-01-22 00:17:43 +00:00
|
|
|
{
|
2021-01-22 01:41:09 +00:00
|
|
|
/**
|
2023-06-11 23:01:39 +00:00
|
|
|
* @readonly
|
2021-05-10 23:39:21 +00:00
|
|
|
* @var \Rector\Php80\NodeResolver\RequireOptionalParamResolver
|
2021-01-22 01:41:09 +00:00
|
|
|
*/
|
|
|
|
private $requireOptionalParamResolver;
|
2021-01-22 09:16:00 +00:00
|
|
|
/**
|
2023-06-11 23:01:39 +00:00
|
|
|
* @readonly
|
2021-05-10 23:39:21 +00:00
|
|
|
* @var \Rector\Php80\NodeResolver\ArgumentSorter
|
2021-01-22 09:16:00 +00:00
|
|
|
*/
|
|
|
|
private $argumentSorter;
|
2021-06-26 11:50:38 +00:00
|
|
|
/**
|
2023-06-11 23:01:39 +00:00
|
|
|
* @readonly
|
2024-01-02 02:40:38 +00:00
|
|
|
* @var \Rector\Reflection\ReflectionResolver
|
2021-06-26 11:50:38 +00:00
|
|
|
*/
|
2021-06-26 14:52:05 +00:00
|
|
|
private $reflectionResolver;
|
2021-07-19 20:57:26 +00:00
|
|
|
/**
|
2023-06-11 23:01:39 +00:00
|
|
|
* @readonly
|
2021-07-19 20:57:26 +00:00
|
|
|
* @var \Rector\CodingStyle\Reflection\VendorLocationDetector
|
|
|
|
*/
|
|
|
|
private $vendorLocationDetector;
|
2023-07-11 23:50:02 +00:00
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private const HAS_SWAPPED_PARAMS = 'has_swapped_params';
|
2022-06-07 08:22:29 +00:00
|
|
|
public function __construct(RequireOptionalParamResolver $requireOptionalParamResolver, ArgumentSorter $argumentSorter, ReflectionResolver $reflectionResolver, VendorLocationDetector $vendorLocationDetector)
|
2021-05-09 20:15:43 +00:00
|
|
|
{
|
2021-01-22 01:41:09 +00:00
|
|
|
$this->requireOptionalParamResolver = $requireOptionalParamResolver;
|
2021-01-22 09:16:00 +00:00
|
|
|
$this->argumentSorter = $argumentSorter;
|
2021-06-26 14:52:05 +00:00
|
|
|
$this->reflectionResolver = $reflectionResolver;
|
2021-07-19 20:57:26 +00:00
|
|
|
$this->vendorLocationDetector = $vendorLocationDetector;
|
2021-01-22 01:41:09 +00:00
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
public function getRuleDefinition() : RuleDefinition
|
2021-01-22 00:17:43 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return new RuleDefinition('Move required parameters after optional ones', [new CodeSample(<<<'CODE_SAMPLE'
|
2021-01-22 00:17:43 +00:00
|
|
|
class SomeObject
|
|
|
|
{
|
|
|
|
public function run($optional = 1, $required)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
}
|
|
|
|
CODE_SAMPLE
|
2021-05-09 20:15:43 +00:00
|
|
|
, <<<'CODE_SAMPLE'
|
2021-01-22 00:17:43 +00:00
|
|
|
class SomeObject
|
|
|
|
{
|
|
|
|
public function run($required, $optional = 1)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
}
|
|
|
|
CODE_SAMPLE
|
2021-05-09 20:15:43 +00:00
|
|
|
)]);
|
2021-01-22 00:17:43 +00:00
|
|
|
}
|
|
|
|
/**
|
2021-02-27 00:06:15 +00:00
|
|
|
* @return array<class-string<Node>>
|
2021-01-22 00:17:43 +00:00
|
|
|
*/
|
2021-05-09 20:15:43 +00:00
|
|
|
public function getNodeTypes() : array
|
2021-01-22 00:17:43 +00:00
|
|
|
{
|
2022-08-22 10:08:19 +00:00
|
|
|
return [ClassMethod::class, New_::class, MethodCall::class, StaticCall::class];
|
2021-01-22 00:17:43 +00:00
|
|
|
}
|
|
|
|
/**
|
2022-08-22 10:08:19 +00:00
|
|
|
* @param ClassMethod|New_|MethodCall|StaticCall $node
|
2023-02-28 18:23:25 +00:00
|
|
|
* @return \PhpParser\Node\Stmt\ClassMethod|null|\PhpParser\Node\Expr\New_|\PhpParser\Node\Expr\MethodCall|\PhpParser\Node\Expr\StaticCall
|
2021-01-22 00:17:43 +00:00
|
|
|
*/
|
2022-12-13 12:09:37 +00:00
|
|
|
public function refactorWithScope(Node $node, Scope $scope)
|
2021-01-22 00:17:43 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
if ($node instanceof ClassMethod) {
|
2022-12-13 12:09:37 +00:00
|
|
|
return $this->refactorClassMethod($node, $scope);
|
2021-01-22 01:41:09 +00:00
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
if ($node instanceof New_) {
|
2022-12-13 12:09:37 +00:00
|
|
|
return $this->refactorNew($node, $scope);
|
2021-01-22 09:16:00 +00:00
|
|
|
}
|
2022-12-13 12:09:37 +00:00
|
|
|
return $this->refactorMethodCall($node, $scope);
|
2021-01-22 01:41:09 +00:00
|
|
|
}
|
2022-12-13 12:09:37 +00:00
|
|
|
private function refactorClassMethod(ClassMethod $classMethod, Scope $scope) : ?ClassMethod
|
2021-01-22 01:41:09 +00:00
|
|
|
{
|
|
|
|
if ($classMethod->params === []) {
|
|
|
|
return null;
|
|
|
|
}
|
2023-07-11 23:50:02 +00:00
|
|
|
if ($classMethod->getAttribute(self::HAS_SWAPPED_PARAMS, \false) === \true) {
|
|
|
|
return null;
|
|
|
|
}
|
2023-07-20 07:43:17 +00:00
|
|
|
$classMethodReflection = $this->reflectionResolver->resolveMethodReflectionFromClassMethod($classMethod, $scope);
|
2022-06-07 08:22:29 +00:00
|
|
|
if (!$classMethodReflection instanceof MethodReflection) {
|
2021-06-24 08:24:49 +00:00
|
|
|
return null;
|
|
|
|
}
|
2022-12-13 12:09:37 +00:00
|
|
|
$expectedArgOrParamOrder = $this->resolveExpectedArgParamOrderIfDifferent($classMethodReflection, $classMethod, $scope);
|
2021-06-26 18:46:16 +00:00
|
|
|
if ($expectedArgOrParamOrder === null) {
|
2021-01-22 01:41:09 +00:00
|
|
|
return null;
|
|
|
|
}
|
2022-07-03 12:14:45 +00:00
|
|
|
$classMethod->params = $this->argumentSorter->sortArgsByExpectedParamOrder($classMethod->params, $expectedArgOrParamOrder);
|
2023-07-11 23:50:02 +00:00
|
|
|
$classMethod->setAttribute(self::HAS_SWAPPED_PARAMS, \true);
|
2021-01-22 01:41:09 +00:00
|
|
|
return $classMethod;
|
|
|
|
}
|
2022-12-13 12:09:37 +00:00
|
|
|
private function refactorNew(New_ $new, Scope $scope) : ?New_
|
2021-01-22 01:41:09 +00:00
|
|
|
{
|
|
|
|
if ($new->args === []) {
|
2021-01-22 00:17:43 +00:00
|
|
|
return null;
|
|
|
|
}
|
2023-06-02 12:32:12 +00:00
|
|
|
if ($new->isFirstClassCallable()) {
|
|
|
|
return null;
|
|
|
|
}
|
2021-06-26 18:46:16 +00:00
|
|
|
$methodReflection = $this->reflectionResolver->resolveMethodReflectionFromNew($new);
|
2022-06-07 08:22:29 +00:00
|
|
|
if (!$methodReflection instanceof MethodReflection) {
|
2021-01-22 01:41:09 +00:00
|
|
|
return null;
|
|
|
|
}
|
2022-12-13 12:09:37 +00:00
|
|
|
$expectedArgOrParamOrder = $this->resolveExpectedArgParamOrderIfDifferent($methodReflection, $new, $scope);
|
2021-06-26 18:46:16 +00:00
|
|
|
if ($expectedArgOrParamOrder === null) {
|
2021-06-26 18:33:28 +00:00
|
|
|
return null;
|
|
|
|
}
|
2022-03-28 22:48:45 +00:00
|
|
|
$new->args = $this->argumentSorter->sortArgsByExpectedParamOrder($new->getArgs(), $expectedArgOrParamOrder);
|
2021-06-26 18:46:16 +00:00
|
|
|
return $new;
|
|
|
|
}
|
2022-08-22 10:08:19 +00:00
|
|
|
/**
|
|
|
|
* @param \PhpParser\Node\Expr\MethodCall|\PhpParser\Node\Expr\StaticCall $methodCall
|
|
|
|
* @return \PhpParser\Node\Expr\MethodCall|\PhpParser\Node\Expr\StaticCall|null
|
|
|
|
*/
|
2022-12-13 12:09:37 +00:00
|
|
|
private function refactorMethodCall($methodCall, Scope $scope)
|
2021-06-26 18:46:16 +00:00
|
|
|
{
|
2023-06-02 12:32:12 +00:00
|
|
|
if ($methodCall->isFirstClassCallable()) {
|
|
|
|
return null;
|
|
|
|
}
|
2021-06-27 17:35:23 +00:00
|
|
|
$methodReflection = $this->reflectionResolver->resolveFunctionLikeReflectionFromCall($methodCall);
|
2022-06-07 08:22:29 +00:00
|
|
|
if (!$methodReflection instanceof MethodReflection) {
|
2021-01-22 00:17:43 +00:00
|
|
|
return null;
|
|
|
|
}
|
2022-12-13 12:09:37 +00:00
|
|
|
$expectedArgOrParamOrder = $this->resolveExpectedArgParamOrderIfDifferent($methodReflection, $methodCall, $scope);
|
2021-06-26 18:46:16 +00:00
|
|
|
if ($expectedArgOrParamOrder === null) {
|
2021-01-22 09:37:29 +00:00
|
|
|
return null;
|
|
|
|
}
|
2022-03-28 22:48:45 +00:00
|
|
|
$newArgs = $this->argumentSorter->sortArgsByExpectedParamOrder($methodCall->getArgs(), $expectedArgOrParamOrder);
|
2021-06-26 18:46:16 +00:00
|
|
|
if ($methodCall->args === $newArgs) {
|
2021-01-22 01:41:09 +00:00
|
|
|
return null;
|
|
|
|
}
|
2021-06-26 18:46:16 +00:00
|
|
|
$methodCall->args = $newArgs;
|
|
|
|
return $methodCall;
|
2021-01-22 00:17:43 +00:00
|
|
|
}
|
2021-06-26 18:46:16 +00:00
|
|
|
/**
|
|
|
|
* @return int[]|null
|
2022-08-22 10:08:19 +00:00
|
|
|
* @param \PhpParser\Node\Expr\New_|\PhpParser\Node\Expr\MethodCall|\PhpParser\Node\Stmt\ClassMethod|\PhpParser\Node\Expr\StaticCall $node
|
2021-06-26 18:46:16 +00:00
|
|
|
*/
|
2022-12-13 12:09:37 +00:00
|
|
|
private function resolveExpectedArgParamOrderIfDifferent(MethodReflection $methodReflection, $node, Scope $scope) : ?array
|
2021-01-22 00:17:43 +00:00
|
|
|
{
|
2021-10-31 18:04:12 +00:00
|
|
|
if ($this->vendorLocationDetector->detectMethodReflection($methodReflection)) {
|
2021-01-22 01:41:09 +00:00
|
|
|
return null;
|
2021-01-22 00:17:43 +00:00
|
|
|
}
|
2022-12-13 12:09:37 +00:00
|
|
|
$parametersAcceptor = ParametersAcceptorSelectorVariantsWrapper::select($methodReflection, $node, $scope);
|
|
|
|
$expectedParameterReflections = $this->requireOptionalParamResolver->resolveFromParametersAcceptor($parametersAcceptor);
|
2021-06-26 18:46:16 +00:00
|
|
|
if ($expectedParameterReflections === $parametersAcceptor->getParameters()) {
|
2021-01-22 09:16:00 +00:00
|
|
|
return null;
|
2021-01-22 00:17:43 +00:00
|
|
|
}
|
2021-06-26 18:46:16 +00:00
|
|
|
return \array_keys($expectedParameterReflections);
|
2021-01-22 00:17:43 +00:00
|
|
|
}
|
|
|
|
}
|