2019-10-13 05:59:52 +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\TypeDeclaration\Rector\ClassMethod;
|
2019-09-24 08:37:29 +00:00
|
|
|
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node;
|
2023-07-30 00:32:15 +00:00
|
|
|
use PhpParser\Node\Name;
|
2023-06-11 06:39:53 +00:00
|
|
|
use PhpParser\Node\Stmt\Class_;
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node\Stmt\ClassMethod;
|
2024-01-02 02:40:38 +00:00
|
|
|
use Rector\PhpParser\NodeFinder\LocalMethodCallFinder;
|
|
|
|
use Rector\Rector\AbstractRector;
|
2022-06-06 17:12:56 +00:00
|
|
|
use Rector\TypeDeclaration\NodeAnalyzer\CallTypesResolver;
|
|
|
|
use Rector\TypeDeclaration\NodeAnalyzer\ClassMethodParamTypeCompleter;
|
2022-06-07 09:18:30 +00:00
|
|
|
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
|
|
|
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
2019-09-24 08:37:29 +00:00
|
|
|
/**
|
2021-06-24 13:19:56 +00:00
|
|
|
* @changelog https://github.com/symplify/phpstan-rules/blob/master/docs/rules_overview.md#checktypehintcallertyperule
|
|
|
|
*
|
2021-03-12 22:20:25 +00:00
|
|
|
* @see \Rector\Tests\TypeDeclaration\Rector\ClassMethod\AddMethodCallBasedStrictParamTypeRector\AddMethodCallBasedStrictParamTypeRectorTest
|
2019-09-24 08:37:29 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
final class AddMethodCallBasedStrictParamTypeRector extends AbstractRector
|
2019-09-24 08:37:29 +00:00
|
|
|
{
|
|
|
|
/**
|
2023-06-11 23:01:39 +00:00
|
|
|
* @readonly
|
2021-05-10 23:39:21 +00:00
|
|
|
* @var \Rector\TypeDeclaration\NodeAnalyzer\CallTypesResolver
|
2019-09-24 08:37:29 +00:00
|
|
|
*/
|
2021-03-06 21:16:18 +00:00
|
|
|
private $callTypesResolver;
|
|
|
|
/**
|
2023-06-11 23:01:39 +00:00
|
|
|
* @readonly
|
2021-05-10 23:39:21 +00:00
|
|
|
* @var \Rector\TypeDeclaration\NodeAnalyzer\ClassMethodParamTypeCompleter
|
2021-03-06 21:16:18 +00:00
|
|
|
*/
|
|
|
|
private $classMethodParamTypeCompleter;
|
2021-06-24 13:19:56 +00:00
|
|
|
/**
|
2023-06-11 23:01:39 +00:00
|
|
|
* @readonly
|
2024-01-02 02:40:38 +00:00
|
|
|
* @var \Rector\PhpParser\NodeFinder\LocalMethodCallFinder
|
2021-06-24 13:19:56 +00:00
|
|
|
*/
|
|
|
|
private $localMethodCallFinder;
|
2023-06-08 22:00:17 +00:00
|
|
|
/**
|
|
|
|
* @var int
|
|
|
|
*/
|
|
|
|
private const MAX_UNION_TYPES = 3;
|
2022-06-07 08:22:29 +00:00
|
|
|
public function __construct(CallTypesResolver $callTypesResolver, ClassMethodParamTypeCompleter $classMethodParamTypeCompleter, LocalMethodCallFinder $localMethodCallFinder)
|
2021-05-09 20:15:43 +00:00
|
|
|
{
|
2021-03-06 21:16:18 +00:00
|
|
|
$this->callTypesResolver = $callTypesResolver;
|
|
|
|
$this->classMethodParamTypeCompleter = $classMethodParamTypeCompleter;
|
2021-06-24 13:19:56 +00:00
|
|
|
$this->localMethodCallFinder = $localMethodCallFinder;
|
2019-09-24 08:37:29 +00:00
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
public function getRuleDefinition() : RuleDefinition
|
2019-09-24 08:37:29 +00:00
|
|
|
{
|
2024-01-31 14:27:18 +00:00
|
|
|
return new RuleDefinition('Change private method param type to strict type, based on passed strict types', [new CodeSample(<<<'CODE_SAMPLE'
|
2021-10-28 22:22:07 +00:00
|
|
|
final class SomeClass
|
2019-09-24 08:37:29 +00:00
|
|
|
{
|
2021-10-28 22:22:07 +00:00
|
|
|
public function run(int $value)
|
2021-02-17 15:40:29 +00:00
|
|
|
{
|
2021-10-28 22:22:07 +00:00
|
|
|
$this->resolve($value);
|
2021-02-17 15:40:29 +00:00
|
|
|
}
|
|
|
|
|
2021-10-28 22:22:07 +00:00
|
|
|
private function resolve($value)
|
2019-09-24 08:37:29 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
}
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2021-05-09 20:15:43 +00:00
|
|
|
, <<<'CODE_SAMPLE'
|
2021-10-28 22:22:07 +00:00
|
|
|
final class SomeClass
|
2019-09-24 08:37:29 +00:00
|
|
|
{
|
2021-10-28 22:22:07 +00:00
|
|
|
public function run(int $value)
|
2021-02-17 15:40:29 +00:00
|
|
|
{
|
2021-10-28 22:22:07 +00:00
|
|
|
$this->resolve($value);
|
2021-02-17 15:40:29 +00:00
|
|
|
}
|
|
|
|
|
2021-10-28 22:22:07 +00:00
|
|
|
private function resolve(int $value)
|
2019-09-24 08:37:29 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
}
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2021-10-23 21:09:26 +00:00
|
|
|
)]);
|
2019-09-24 08:37:29 +00:00
|
|
|
}
|
|
|
|
/**
|
2021-02-27 00:06:15 +00:00
|
|
|
* @return array<class-string<Node>>
|
2019-09-24 08:37:29 +00:00
|
|
|
*/
|
2021-05-09 20:15:43 +00:00
|
|
|
public function getNodeTypes() : array
|
2019-09-24 08:37:29 +00:00
|
|
|
{
|
2023-06-11 06:39:53 +00:00
|
|
|
return [Class_::class];
|
2019-09-24 08:37:29 +00:00
|
|
|
}
|
|
|
|
/**
|
2023-06-11 06:39:53 +00:00
|
|
|
* @param Class_ $node
|
2019-09-24 08:37:29 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
public function refactor(Node $node) : ?Node
|
2019-09-24 08:37:29 +00:00
|
|
|
{
|
2023-06-11 06:39:53 +00:00
|
|
|
$hasChanged = \false;
|
2024-01-30 23:57:24 +00:00
|
|
|
foreach ($node->getMethods() as $classMethod) {
|
|
|
|
if ($classMethod->params === []) {
|
2023-06-11 06:39:53 +00:00
|
|
|
continue;
|
|
|
|
}
|
2024-01-30 23:57:24 +00:00
|
|
|
if (!$this->isClassMethodPrivate($node, $classMethod)) {
|
2023-06-11 06:39:53 +00:00
|
|
|
continue;
|
|
|
|
}
|
2024-01-30 23:57:24 +00:00
|
|
|
if ($classMethod->isPublic()) {
|
2023-08-05 16:29:56 +00:00
|
|
|
continue;
|
|
|
|
}
|
2024-01-30 23:57:24 +00:00
|
|
|
$methodCalls = $this->localMethodCallFinder->match($node, $classMethod);
|
2023-06-11 06:39:53 +00:00
|
|
|
$classMethodParameterTypes = $this->callTypesResolver->resolveStrictTypesFromCalls($methodCalls);
|
2024-01-30 23:57:24 +00:00
|
|
|
$classMethod = $this->classMethodParamTypeCompleter->complete($classMethod, $classMethodParameterTypes, self::MAX_UNION_TYPES);
|
2023-06-11 06:39:53 +00:00
|
|
|
if ($classMethod instanceof ClassMethod) {
|
|
|
|
$hasChanged = \true;
|
|
|
|
}
|
2019-10-30 09:49:07 +00:00
|
|
|
}
|
2023-06-11 06:39:53 +00:00
|
|
|
if ($hasChanged) {
|
|
|
|
return $node;
|
2021-06-24 13:19:56 +00:00
|
|
|
}
|
2023-06-11 06:39:53 +00:00
|
|
|
return null;
|
2021-02-17 15:40:29 +00:00
|
|
|
}
|
2024-01-30 23:57:24 +00:00
|
|
|
private function isClassMethodPrivate(Class_ $class, ClassMethod $classMethod) : bool
|
|
|
|
{
|
|
|
|
if ($classMethod->isPrivate()) {
|
|
|
|
return \true;
|
|
|
|
}
|
|
|
|
if ($classMethod->isFinal() && !$class->extends instanceof Name && $class->implements === []) {
|
|
|
|
return \true;
|
|
|
|
}
|
|
|
|
return $class->isFinal() && !$class->extends instanceof Name && $class->implements === [] && $classMethod->isProtected();
|
|
|
|
}
|
2019-09-24 08:37:29 +00:00
|
|
|
}
|