2021-07-30 10:40:03 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare (strict_types=1);
|
2022-06-06 17:12:56 +00:00
|
|
|
namespace Rector\TypeDeclaration\Rector\ClassMethod;
|
2021-07-30 10:40:03 +00:00
|
|
|
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node;
|
|
|
|
use PhpParser\Node\ComplexType;
|
|
|
|
use PhpParser\Node\Expr\FuncCall;
|
|
|
|
use PhpParser\Node\Expr\MethodCall;
|
|
|
|
use PhpParser\Node\Expr\StaticCall;
|
|
|
|
use PhpParser\Node\Identifier;
|
|
|
|
use PhpParser\Node\Name;
|
|
|
|
use PhpParser\Node\NullableType;
|
|
|
|
use PhpParser\Node\Param;
|
2023-06-09 16:23:18 +00:00
|
|
|
use PhpParser\Node\Stmt\Class_;
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node\Stmt\ClassMethod;
|
|
|
|
use PhpParser\Node\UnionType;
|
|
|
|
use PHPStan\Analyser\Scope;
|
2023-06-09 16:23:18 +00:00
|
|
|
use Rector\NodeTypeResolver\Node\AttributeKey;
|
2024-01-02 02:40:38 +00:00
|
|
|
use Rector\PhpParser\Node\BetterNodeFinder;
|
|
|
|
use Rector\Rector\AbstractScopeAwareRector;
|
2023-08-13 07:18:25 +00:00
|
|
|
use Rector\TypeDeclaration\Guard\ParamTypeAddGuard;
|
2022-06-06 17:12:56 +00:00
|
|
|
use Rector\TypeDeclaration\NodeAnalyzer\CallerParamMatcher;
|
|
|
|
use Rector\VendorLocker\ParentClassMethodTypeOverrideGuard;
|
2022-06-07 09:18:30 +00:00
|
|
|
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
|
|
|
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
2021-07-30 10:40:03 +00:00
|
|
|
/**
|
|
|
|
* @see \Rector\Tests\TypeDeclaration\Rector\ClassMethod\ParamTypeByMethodCallTypeRector\ParamTypeByMethodCallTypeRectorTest
|
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
final class ParamTypeByMethodCallTypeRector extends AbstractScopeAwareRector
|
2021-07-30 10:40:03 +00:00
|
|
|
{
|
|
|
|
/**
|
2023-06-11 23:01:39 +00:00
|
|
|
* @readonly
|
2021-07-30 10:40:03 +00:00
|
|
|
* @var \Rector\TypeDeclaration\NodeAnalyzer\CallerParamMatcher
|
|
|
|
*/
|
|
|
|
private $callerParamMatcher;
|
2021-08-02 19:22:48 +00:00
|
|
|
/**
|
2023-06-11 23:01:39 +00:00
|
|
|
* @readonly
|
2021-10-26 00:34:35 +00:00
|
|
|
* @var \Rector\VendorLocker\ParentClassMethodTypeOverrideGuard
|
2021-08-02 19:22:48 +00:00
|
|
|
*/
|
|
|
|
private $parentClassMethodTypeOverrideGuard;
|
2023-08-13 07:18:25 +00:00
|
|
|
/**
|
|
|
|
* @readonly
|
|
|
|
* @var \Rector\TypeDeclaration\Guard\ParamTypeAddGuard
|
|
|
|
*/
|
|
|
|
private $paramTypeAddGuard;
|
2023-09-20 12:55:35 +00:00
|
|
|
/**
|
|
|
|
* @readonly
|
2024-01-02 02:40:38 +00:00
|
|
|
* @var \Rector\PhpParser\Node\BetterNodeFinder
|
2023-09-20 12:55:35 +00:00
|
|
|
*/
|
|
|
|
private $betterNodeFinder;
|
|
|
|
public function __construct(CallerParamMatcher $callerParamMatcher, ParentClassMethodTypeOverrideGuard $parentClassMethodTypeOverrideGuard, ParamTypeAddGuard $paramTypeAddGuard, BetterNodeFinder $betterNodeFinder)
|
2021-07-30 10:40:03 +00:00
|
|
|
{
|
|
|
|
$this->callerParamMatcher = $callerParamMatcher;
|
2021-08-02 19:22:48 +00:00
|
|
|
$this->parentClassMethodTypeOverrideGuard = $parentClassMethodTypeOverrideGuard;
|
2023-08-13 07:18:25 +00:00
|
|
|
$this->paramTypeAddGuard = $paramTypeAddGuard;
|
2023-09-20 12:55:35 +00:00
|
|
|
$this->betterNodeFinder = $betterNodeFinder;
|
2021-07-30 10:40:03 +00:00
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
public function getRuleDefinition() : RuleDefinition
|
2021-07-30 10:40:03 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return new RuleDefinition('Change param type based on passed method call type', [new CodeSample(<<<'CODE_SAMPLE'
|
2021-07-30 10:40:03 +00:00
|
|
|
class SomeTypedService
|
|
|
|
{
|
|
|
|
public function run(string $name)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
final class UseDependency
|
|
|
|
{
|
|
|
|
public function __construct(
|
|
|
|
private SomeTypedService $someTypedService
|
|
|
|
) {
|
|
|
|
}
|
|
|
|
|
|
|
|
public function go($value)
|
|
|
|
{
|
|
|
|
$this->someTypedService->run($value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
CODE_SAMPLE
|
|
|
|
, <<<'CODE_SAMPLE'
|
|
|
|
class SomeTypedService
|
|
|
|
{
|
|
|
|
public function run(string $name)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
final class UseDependency
|
|
|
|
{
|
|
|
|
public function __construct(
|
|
|
|
private SomeTypedService $someTypedService
|
|
|
|
) {
|
|
|
|
}
|
|
|
|
|
|
|
|
public function go(string $value)
|
|
|
|
{
|
|
|
|
$this->someTypedService->run($value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
CODE_SAMPLE
|
|
|
|
)]);
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* @return array<class-string<Node>>
|
|
|
|
*/
|
|
|
|
public function getNodeTypes() : array
|
|
|
|
{
|
2023-06-09 16:23:18 +00:00
|
|
|
return [Class_::class];
|
2021-07-30 10:40:03 +00:00
|
|
|
}
|
|
|
|
/**
|
2023-06-09 16:23:18 +00:00
|
|
|
* @param Class_ $node
|
2021-07-30 10:40:03 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
public function refactorWithScope(Node $node, Scope $scope) : ?Node
|
2021-07-30 10:40:03 +00:00
|
|
|
{
|
|
|
|
$hasChanged = \false;
|
2023-06-09 16:23:18 +00:00
|
|
|
foreach ($node->getMethods() as $classMethod) {
|
|
|
|
if ($this->shouldSkipClassMethod($classMethod)) {
|
2021-07-30 10:40:03 +00:00
|
|
|
continue;
|
|
|
|
}
|
2023-06-09 16:23:18 +00:00
|
|
|
/** @var array<StaticCall|MethodCall|FuncCall> $callers */
|
|
|
|
$callers = $this->betterNodeFinder->findInstancesOf($classMethod, [StaticCall::class, MethodCall::class, FuncCall::class]);
|
2023-12-31 20:56:31 +00:00
|
|
|
$hasClassMethodChanged = $this->refactorClassMethod($classMethod, $callers, $scope);
|
|
|
|
if ($hasClassMethodChanged) {
|
|
|
|
$hasChanged = \true;
|
2021-07-30 10:40:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if ($hasChanged) {
|
|
|
|
return $node;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
private function shouldSkipClassMethod(ClassMethod $classMethod) : bool
|
2021-07-30 10:40:03 +00:00
|
|
|
{
|
|
|
|
if ($classMethod->params === []) {
|
|
|
|
return \true;
|
|
|
|
}
|
2023-08-07 13:20:45 +00:00
|
|
|
return $this->parentClassMethodTypeOverrideGuard->hasParentClassMethod($classMethod);
|
2021-07-30 10:40:03 +00:00
|
|
|
}
|
|
|
|
/**
|
2022-04-26 08:13:18 +00:00
|
|
|
* @param \PhpParser\Node\Identifier|\PhpParser\Node\Name|\PhpParser\Node\NullableType|\PhpParser\Node\UnionType|\PhpParser\Node\ComplexType $paramType
|
2021-07-30 10:40:03 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
private function mirrorParamType(Param $decoratedParam, $paramType) : void
|
2021-07-30 10:40:03 +00:00
|
|
|
{
|
|
|
|
// mimic type
|
|
|
|
$newParamType = $paramType;
|
2023-06-16 14:35:33 +00:00
|
|
|
$this->traverseNodesWithCallable($newParamType, static function (Node $node) {
|
2023-06-09 16:23:18 +00:00
|
|
|
// original node has to removed to avoid tokens crashing from origin positions
|
|
|
|
$node->setAttribute(AttributeKey::ORIGINAL_NODE, null);
|
2022-02-19 18:54:29 +00:00
|
|
|
return null;
|
2021-07-30 10:40:03 +00:00
|
|
|
});
|
|
|
|
$decoratedParam->type = $newParamType;
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
private function shouldSkipParam(Param $param, ClassMethod $classMethod) : bool
|
2021-07-30 10:40:03 +00:00
|
|
|
{
|
2023-11-28 11:12:27 +00:00
|
|
|
// already has type, skip
|
|
|
|
if ($param->type !== null) {
|
2021-07-30 10:40:03 +00:00
|
|
|
return \true;
|
|
|
|
}
|
2021-10-29 20:22:14 +00:00
|
|
|
if ($param->variadic) {
|
|
|
|
return \true;
|
|
|
|
}
|
2023-11-28 11:12:27 +00:00
|
|
|
return !$this->paramTypeAddGuard->isLegal($param, $classMethod);
|
2021-07-30 10:40:03 +00:00
|
|
|
}
|
2023-12-31 20:56:31 +00:00
|
|
|
/**
|
|
|
|
* @param array<StaticCall|MethodCall|FuncCall> $callers
|
|
|
|
*/
|
|
|
|
private function refactorClassMethod(ClassMethod $classMethod, array $callers, Scope $scope) : bool
|
|
|
|
{
|
|
|
|
$hasChanged = \false;
|
|
|
|
foreach ($classMethod->params as $param) {
|
|
|
|
if ($this->shouldSkipParam($param, $classMethod)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
foreach ($callers as $caller) {
|
|
|
|
$paramType = $this->callerParamMatcher->matchCallParamType($caller, $param, $scope);
|
|
|
|
if ($paramType === null) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$this->mirrorParamType($param, $paramType);
|
|
|
|
$hasChanged = \true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $hasChanged;
|
|
|
|
}
|
2021-07-30 10:40:03 +00:00
|
|
|
}
|