2019-12-18 09:44:56 +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-12-18 09:44:56 +00:00
|
|
|
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node;
|
2023-03-23 23:21:34 +00:00
|
|
|
use PhpParser\Node\Name;
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node\Param;
|
|
|
|
use PhpParser\Node\Stmt\Class_;
|
|
|
|
use PhpParser\Node\Stmt\ClassMethod;
|
|
|
|
use PhpParser\Node\Stmt\Interface_;
|
|
|
|
use PHPStan\Type\MixedType;
|
|
|
|
use PHPStan\Type\StringType;
|
2024-01-02 02:40:38 +00:00
|
|
|
use Rector\Contract\Rector\ConfigurableRectorInterface;
|
2022-06-06 17:12:56 +00:00
|
|
|
use Rector\NodeTypeResolver\TypeComparator\TypeComparator;
|
2024-01-02 02:40:38 +00:00
|
|
|
use Rector\Php\PhpVersionProvider;
|
2022-06-06 17:12:56 +00:00
|
|
|
use Rector\PHPStanStaticTypeMapper\Enum\TypeKind;
|
2024-01-02 02:40:38 +00:00
|
|
|
use Rector\Rector\AbstractRector;
|
2023-09-29 12:48:57 +00:00
|
|
|
use Rector\StaticTypeMapper\StaticTypeMapper;
|
2022-06-06 17:12:56 +00:00
|
|
|
use Rector\TypeDeclaration\ValueObject\AddParamTypeDeclaration;
|
2024-01-02 02:40:38 +00:00
|
|
|
use Rector\ValueObject\PhpVersionFeature;
|
2022-06-07 09:18:30 +00:00
|
|
|
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
|
|
|
|
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
2024-05-02 02:21:56 +00:00
|
|
|
use RectorPrefix202405\Webmozart\Assert\Assert;
|
2019-12-18 09:44:56 +00:00
|
|
|
/**
|
2021-03-12 22:20:25 +00:00
|
|
|
* @see \Rector\Tests\TypeDeclaration\Rector\ClassMethod\AddParamTypeDeclarationRector\AddParamTypeDeclarationRectorTest
|
2019-12-18 09:44:56 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
final class AddParamTypeDeclarationRector extends AbstractRector implements ConfigurableRectorInterface
|
2019-12-18 09:44:56 +00:00
|
|
|
{
|
2020-10-30 11:58:35 +00:00
|
|
|
/**
|
2023-06-11 23:01:39 +00:00
|
|
|
* @readonly
|
2021-05-10 23:39:21 +00:00
|
|
|
* @var \Rector\NodeTypeResolver\TypeComparator\TypeComparator
|
2020-10-30 11:58:35 +00:00
|
|
|
*/
|
|
|
|
private $typeComparator;
|
2022-04-28 20:11:07 +00:00
|
|
|
/**
|
2023-06-11 23:01:39 +00:00
|
|
|
* @readonly
|
2024-01-02 02:40:38 +00:00
|
|
|
* @var \Rector\Php\PhpVersionProvider
|
2022-04-28 20:11:07 +00:00
|
|
|
*/
|
|
|
|
private $phpVersionProvider;
|
2023-09-29 12:48:57 +00:00
|
|
|
/**
|
|
|
|
* @readonly
|
|
|
|
* @var \Rector\StaticTypeMapper\StaticTypeMapper
|
|
|
|
*/
|
|
|
|
private $staticTypeMapper;
|
2023-06-08 22:00:17 +00:00
|
|
|
/**
|
|
|
|
* @var AddParamTypeDeclaration[]
|
|
|
|
*/
|
|
|
|
private $addParamTypeDeclarations = [];
|
|
|
|
/**
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
private $hasChanged = \false;
|
2023-09-29 12:48:57 +00:00
|
|
|
public function __construct(TypeComparator $typeComparator, PhpVersionProvider $phpVersionProvider, StaticTypeMapper $staticTypeMapper)
|
2020-10-30 11:58:35 +00:00
|
|
|
{
|
|
|
|
$this->typeComparator = $typeComparator;
|
2022-04-28 20:11:07 +00:00
|
|
|
$this->phpVersionProvider = $phpVersionProvider;
|
2023-09-29 12:48:57 +00:00
|
|
|
$this->staticTypeMapper = $staticTypeMapper;
|
2020-10-30 11:58:35 +00:00
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
public function getRuleDefinition() : RuleDefinition
|
2019-12-18 09:44:56 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return new RuleDefinition('Add param types where needed', [new ConfiguredCodeSample(<<<'CODE_SAMPLE'
|
2019-12-18 09:44:56 +00:00
|
|
|
class SomeClass
|
|
|
|
{
|
|
|
|
public function process($name)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
}
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2021-05-09 20:15:43 +00:00
|
|
|
, <<<'CODE_SAMPLE'
|
2019-12-18 09:44:56 +00:00
|
|
|
class SomeClass
|
|
|
|
{
|
|
|
|
public function process(string $name)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
}
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2022-06-07 08:22:29 +00:00
|
|
|
, [new AddParamTypeDeclaration('SomeClass', 'process', 0, new StringType())])]);
|
2019-12-18 09:44:56 +00:00
|
|
|
}
|
|
|
|
/**
|
2021-02-27 00:06:15 +00:00
|
|
|
* @return array<class-string<Node>>
|
2019-12-18 09:44:56 +00:00
|
|
|
*/
|
2021-05-09 20:15:43 +00:00
|
|
|
public function getNodeTypes() : array
|
2019-12-18 09:44:56 +00:00
|
|
|
{
|
2023-06-09 17:42:49 +00:00
|
|
|
return [Class_::class, Interface_::class];
|
2019-12-18 09:44:56 +00:00
|
|
|
}
|
|
|
|
/**
|
2023-06-09 17:42:49 +00:00
|
|
|
* @param Class_|Interface_ $node
|
2019-12-18 09:44:56 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
public function refactor(Node $node) : ?Node
|
2019-12-18 09:44:56 +00:00
|
|
|
{
|
2023-07-09 11:49:50 +00:00
|
|
|
$this->hasChanged = \false;
|
2023-06-09 17:42:49 +00:00
|
|
|
foreach ($node->getMethods() as $classMethod) {
|
|
|
|
if ($this->shouldSkip($node, $classMethod)) {
|
2019-12-18 09:44:56 +00:00
|
|
|
continue;
|
|
|
|
}
|
2023-09-09 18:18:18 +00:00
|
|
|
$methodName = $this->getName($classMethod);
|
2023-06-09 17:42:49 +00:00
|
|
|
foreach ($this->addParamTypeDeclarations as $addParamTypeDeclaration) {
|
2023-09-09 18:18:18 +00:00
|
|
|
if (!$this->nodeNameResolver->isStringName($methodName, $addParamTypeDeclaration->getMethodName())) {
|
2023-06-09 17:42:49 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!$this->isObjectType($node, $addParamTypeDeclaration->getObjectType())) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$this->refactorClassMethodWithTypehintByParameterPosition($classMethod, $addParamTypeDeclaration);
|
|
|
|
}
|
2023-08-09 06:40:30 +00:00
|
|
|
}
|
|
|
|
if (!$this->hasChanged) {
|
|
|
|
return null;
|
2022-05-31 12:43:03 +00:00
|
|
|
}
|
2019-12-18 09:44:56 +00:00
|
|
|
return $node;
|
|
|
|
}
|
2020-10-30 11:58:35 +00:00
|
|
|
/**
|
2021-11-28 17:01:20 +00:00
|
|
|
* @param mixed[] $configuration
|
2020-10-30 11:58:35 +00:00
|
|
|
*/
|
2021-12-10 10:22:23 +00:00
|
|
|
public function configure(array $configuration) : void
|
2020-07-29 23:39:41 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
Assert::allIsAOf($configuration, AddParamTypeDeclaration::class);
|
2022-02-18 01:46:17 +00:00
|
|
|
$this->addParamTypeDeclarations = $configuration;
|
2020-07-29 23:39:41 +00:00
|
|
|
}
|
2023-06-09 17:42:49 +00:00
|
|
|
/**
|
|
|
|
* @param \PhpParser\Node\Stmt\Class_|\PhpParser\Node\Stmt\Interface_ $classLike
|
|
|
|
*/
|
|
|
|
private function shouldSkip($classLike, ClassMethod $classMethod) : bool
|
2019-12-18 09:44:56 +00:00
|
|
|
{
|
|
|
|
// skip class methods without args
|
2020-12-25 00:22:45 +00:00
|
|
|
if ($classMethod->params === []) {
|
2021-05-09 20:15:43 +00:00
|
|
|
return \true;
|
2019-12-18 09:44:56 +00:00
|
|
|
}
|
|
|
|
// skip class without parents/interfaces
|
2023-06-09 17:42:49 +00:00
|
|
|
if ($classLike instanceof Class_ && $classLike->implements !== []) {
|
|
|
|
return \false;
|
2019-12-18 09:44:56 +00:00
|
|
|
}
|
2023-06-09 17:42:49 +00:00
|
|
|
return !$classLike->extends instanceof Name;
|
2019-12-18 09:44:56 +00:00
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
private function refactorClassMethodWithTypehintByParameterPosition(ClassMethod $classMethod, AddParamTypeDeclaration $addParamTypeDeclaration) : void
|
2021-05-09 20:15:43 +00:00
|
|
|
{
|
2020-09-12 21:19:08 +00:00
|
|
|
$parameter = $classMethod->params[$addParamTypeDeclaration->getPosition()] ?? null;
|
2022-06-07 08:22:29 +00:00
|
|
|
if (!$parameter instanceof Param) {
|
2020-08-25 00:21:00 +00:00
|
|
|
return;
|
2019-12-25 09:49:48 +00:00
|
|
|
}
|
2020-09-12 21:19:08 +00:00
|
|
|
$this->refactorParameter($parameter, $addParamTypeDeclaration);
|
2019-12-25 09:49:48 +00:00
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
private function refactorParameter(Param $param, AddParamTypeDeclaration $addParamTypeDeclaration) : void
|
2019-12-18 09:44:56 +00:00
|
|
|
{
|
|
|
|
// already set → no change
|
2020-10-30 11:58:35 +00:00
|
|
|
if ($param->type !== null) {
|
|
|
|
$currentParamType = $this->staticTypeMapper->mapPhpParserNodePHPStanType($param->type);
|
2020-12-04 23:22:12 +00:00
|
|
|
if ($this->typeComparator->areTypesEqual($currentParamType, $addParamTypeDeclaration->getParamType())) {
|
2020-10-30 11:58:35 +00:00
|
|
|
return;
|
|
|
|
}
|
2019-12-18 09:44:56 +00:00
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
$paramTypeNode = $this->staticTypeMapper->mapPHPStanTypeToPhpParserNode($addParamTypeDeclaration->getParamType(), TypeKind::PARAM);
|
2022-05-31 12:43:03 +00:00
|
|
|
$this->hasChanged = \true;
|
2019-12-18 09:44:56 +00:00
|
|
|
// remove it
|
2022-06-07 08:22:29 +00:00
|
|
|
if ($addParamTypeDeclaration->getParamType() instanceof MixedType) {
|
|
|
|
if ($this->phpVersionProvider->isAtLeastPhpVersion(PhpVersionFeature::MIXED_TYPE)) {
|
2021-12-13 16:47:47 +00:00
|
|
|
$param->type = $paramTypeNode;
|
|
|
|
return;
|
|
|
|
}
|
2019-12-18 09:44:56 +00:00
|
|
|
$param->type = null;
|
|
|
|
return;
|
|
|
|
}
|
2021-12-13 16:47:47 +00:00
|
|
|
$param->type = $paramTypeNode;
|
2019-12-18 09:44:56 +00:00
|
|
|
}
|
|
|
|
}
|