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;
|
2024-04-20 11:01:46 +00:00
|
|
|
use PhpParser\Node\Expr\FuncCall;
|
2022-06-06 17:12:56 +00:00
|
|
|
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;
|
2024-04-20 11:01:46 +00:00
|
|
|
use PhpParser\Node\Stmt\Function_;
|
2022-07-28 14:06:21 +00:00
|
|
|
use PHPStan\Analyser\Scope;
|
2024-04-20 11:01:46 +00:00
|
|
|
use PHPStan\Reflection\FunctionReflection;
|
2022-06-06 17:12:56 +00:00
|
|
|
use PHPStan\Reflection\MethodReflection;
|
2024-04-20 19:24:39 +00:00
|
|
|
use PHPStan\Reflection\Native\NativeFunctionReflection;
|
2022-06-06 17:12:56 +00:00
|
|
|
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
|
|
|
{
|
2024-04-20 11:01:46 +00:00
|
|
|
return [ClassMethod::class, Function_::class, New_::class, MethodCall::class, StaticCall::class, FuncCall::class];
|
2021-01-22 00:17:43 +00:00
|
|
|
}
|
|
|
|
/**
|
2024-04-20 11:01:46 +00:00
|
|
|
* @param ClassMethod|Function_|New_|MethodCall|StaticCall|FuncCall $node
|
|
|
|
* @return \PhpParser\Node\Stmt\ClassMethod|\PhpParser\Node\Stmt\Function_|null|\PhpParser\Node\Expr\New_|\PhpParser\Node\Expr\MethodCall|\PhpParser\Node\Expr\StaticCall|\PhpParser\Node\Expr\FuncCall
|
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
|
|
|
{
|
2024-04-20 11:01:46 +00:00
|
|
|
if ($node instanceof ClassMethod || $node instanceof Function_) {
|
|
|
|
return $this->refactorClassMethodOrFunction($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
|
|
|
}
|
2024-04-20 11:01:46 +00:00
|
|
|
return $this->refactorMethodCallOrFuncCall($node, $scope);
|
2021-01-22 01:41:09 +00:00
|
|
|
}
|
2024-04-20 11:01:46 +00:00
|
|
|
/**
|
|
|
|
* @param \PhpParser\Node\Stmt\ClassMethod|\PhpParser\Node\Stmt\Function_ $node
|
|
|
|
* @return \PhpParser\Node\Stmt\ClassMethod|\PhpParser\Node\Stmt\Function_|null
|
|
|
|
*/
|
|
|
|
private function refactorClassMethodOrFunction($node, Scope $scope)
|
2021-01-22 01:41:09 +00:00
|
|
|
{
|
2024-04-20 11:01:46 +00:00
|
|
|
if ($node->params === []) {
|
2021-01-22 01:41:09 +00:00
|
|
|
return null;
|
|
|
|
}
|
2024-04-20 11:01:46 +00:00
|
|
|
if ($node->getAttribute(self::HAS_SWAPPED_PARAMS, \false) === \true) {
|
2023-07-11 23:50:02 +00:00
|
|
|
return null;
|
|
|
|
}
|
2024-04-20 11:01:46 +00:00
|
|
|
if ($node instanceof ClassMethod) {
|
|
|
|
$reflection = $this->reflectionResolver->resolveMethodReflectionFromClassMethod($node, $scope);
|
|
|
|
} else {
|
|
|
|
$reflection = $this->reflectionResolver->resolveFunctionReflectionFromFunction($node, $scope);
|
|
|
|
}
|
|
|
|
if (!$reflection instanceof MethodReflection && !$reflection instanceof FunctionReflection) {
|
2021-06-24 08:24:49 +00:00
|
|
|
return null;
|
|
|
|
}
|
2024-04-20 11:01:46 +00:00
|
|
|
$expectedArgOrParamOrder = $this->resolveExpectedArgParamOrderIfDifferent($reflection, $node, $scope);
|
2021-06-26 18:46:16 +00:00
|
|
|
if ($expectedArgOrParamOrder === null) {
|
2021-01-22 01:41:09 +00:00
|
|
|
return null;
|
|
|
|
}
|
2024-04-20 11:01:46 +00:00
|
|
|
$node->params = $this->argumentSorter->sortArgsByExpectedParamOrder($node->params, $expectedArgOrParamOrder);
|
|
|
|
$node->setAttribute(self::HAS_SWAPPED_PARAMS, \true);
|
|
|
|
return $node;
|
2021-01-22 01:41:09 +00:00
|
|
|
}
|
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
|
|
|
/**
|
2024-04-20 11:01:46 +00:00
|
|
|
* @param \PhpParser\Node\Expr\MethodCall|\PhpParser\Node\Expr\StaticCall|\PhpParser\Node\Expr\FuncCall $node
|
|
|
|
* @return \PhpParser\Node\Expr\MethodCall|\PhpParser\Node\Expr\StaticCall|\PhpParser\Node\Expr\FuncCall|null
|
2022-08-22 10:08:19 +00:00
|
|
|
*/
|
2024-04-20 11:01:46 +00:00
|
|
|
private function refactorMethodCallOrFuncCall($node, Scope $scope)
|
2021-06-26 18:46:16 +00:00
|
|
|
{
|
2024-04-20 11:01:46 +00:00
|
|
|
if ($node->isFirstClassCallable()) {
|
2023-06-02 12:32:12 +00:00
|
|
|
return null;
|
|
|
|
}
|
2024-04-20 11:01:46 +00:00
|
|
|
$reflection = $this->reflectionResolver->resolveFunctionLikeReflectionFromCall($node);
|
|
|
|
if (!$reflection instanceof MethodReflection && !$reflection instanceof FunctionReflection) {
|
2021-01-22 00:17:43 +00:00
|
|
|
return null;
|
|
|
|
}
|
2024-04-20 11:01:46 +00:00
|
|
|
$expectedArgOrParamOrder = $this->resolveExpectedArgParamOrderIfDifferent($reflection, $node, $scope);
|
2021-06-26 18:46:16 +00:00
|
|
|
if ($expectedArgOrParamOrder === null) {
|
2021-01-22 09:37:29 +00:00
|
|
|
return null;
|
|
|
|
}
|
2024-04-20 11:01:46 +00:00
|
|
|
$newArgs = $this->argumentSorter->sortArgsByExpectedParamOrder($node->getArgs(), $expectedArgOrParamOrder);
|
|
|
|
if ($node->args === $newArgs) {
|
2021-01-22 01:41:09 +00:00
|
|
|
return null;
|
|
|
|
}
|
2024-04-20 11:01:46 +00:00
|
|
|
$node->args = $newArgs;
|
|
|
|
return $node;
|
2021-01-22 00:17:43 +00:00
|
|
|
}
|
2021-06-26 18:46:16 +00:00
|
|
|
/**
|
|
|
|
* @return int[]|null
|
2024-04-20 11:01:46 +00:00
|
|
|
* @param \PHPStan\Reflection\MethodReflection|\PHPStan\Reflection\FunctionReflection $reflection
|
|
|
|
* @param \PhpParser\Node\Expr\New_|\PhpParser\Node\Expr\MethodCall|\PhpParser\Node\Stmt\ClassMethod|\PhpParser\Node\Stmt\Function_|\PhpParser\Node\Expr\StaticCall|\PhpParser\Node\Expr\FuncCall $node
|
2021-06-26 18:46:16 +00:00
|
|
|
*/
|
2024-04-20 11:01:46 +00:00
|
|
|
private function resolveExpectedArgParamOrderIfDifferent($reflection, $node, Scope $scope) : ?array
|
2021-01-22 00:17:43 +00:00
|
|
|
{
|
2024-04-20 19:24:39 +00:00
|
|
|
if ($reflection instanceof NativeFunctionReflection) {
|
|
|
|
return null;
|
|
|
|
}
|
2024-04-20 11:01:46 +00:00
|
|
|
if ($reflection instanceof MethodReflection && $this->vendorLocationDetector->detectMethodReflection($reflection)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
if ($reflection instanceof FunctionReflection && $this->vendorLocationDetector->detectFunctionReflection($reflection)) {
|
2021-01-22 01:41:09 +00:00
|
|
|
return null;
|
2021-01-22 00:17:43 +00:00
|
|
|
}
|
2024-04-20 11:01:46 +00:00
|
|
|
$parametersAcceptor = ParametersAcceptorSelectorVariantsWrapper::select($reflection, $node, $scope);
|
2022-12-13 12:09:37 +00:00
|
|
|
$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
|
|
|
}
|
|
|
|
}
|