rector/rules/TypeDeclaration/Rector/ClassMethod/AddArrayParamDocTypeRector.php
Tomas Votruba 918f365f5d Updated Rector to commit 17db511043
17db511043 [TypeDeclaration] Handle nullable array param type on AddArrayParamDocTypeRector (#485)
2021-07-22 17:04:20 +00:00

153 lines
5.2 KiB
PHP

<?php
declare (strict_types=1);
namespace Rector\TypeDeclaration\Rector\ClassMethod;
use PhpParser\Node;
use PhpParser\Node\Param;
use PhpParser\Node\Stmt\ClassMethod;
use PHPStan\Type\ArrayType;
use PHPStan\Type\MixedType;
use PHPStan\Type\NullType;
use PHPStan\Type\ObjectType;
use PHPStan\Type\Type;
use PHPStan\Type\UnionType;
use Rector\BetterPhpDocParser\PhpDocManipulator\PhpDocTypeChanger;
use Rector\Core\NodeAnalyzer\ParamAnalyzer;
use Rector\Core\Rector\AbstractRector;
use Rector\DeadCode\PhpDoc\TagRemover\ParamTagRemover;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\TypeDeclaration\TypeInferer\ParamTypeInferer;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @see \Rector\Tests\TypeDeclaration\Rector\ClassMethod\AddArrayParamDocTypeRector\AddArrayParamDocTypeRectorTest
*/
final class AddArrayParamDocTypeRector extends \Rector\Core\Rector\AbstractRector
{
/**
* @var \Rector\TypeDeclaration\TypeInferer\ParamTypeInferer
*/
private $paramTypeInferer;
/**
* @var \Rector\BetterPhpDocParser\PhpDocManipulator\PhpDocTypeChanger
*/
private $phpDocTypeChanger;
/**
* @var \Rector\DeadCode\PhpDoc\TagRemover\ParamTagRemover
*/
private $paramTagRemover;
/**
* @var \Rector\Core\NodeAnalyzer\ParamAnalyzer
*/
private $paramAnalyzer;
public function __construct(\Rector\TypeDeclaration\TypeInferer\ParamTypeInferer $paramTypeInferer, \Rector\BetterPhpDocParser\PhpDocManipulator\PhpDocTypeChanger $phpDocTypeChanger, \Rector\DeadCode\PhpDoc\TagRemover\ParamTagRemover $paramTagRemover, \Rector\Core\NodeAnalyzer\ParamAnalyzer $paramAnalyzer)
{
$this->paramTypeInferer = $paramTypeInferer;
$this->phpDocTypeChanger = $phpDocTypeChanger;
$this->paramTagRemover = $paramTagRemover;
$this->paramAnalyzer = $paramAnalyzer;
}
public function getRuleDefinition() : \Symplify\RuleDocGenerator\ValueObject\RuleDefinition
{
return new \Symplify\RuleDocGenerator\ValueObject\RuleDefinition('Adds @param annotation to array parameters inferred from the rest of the code', [new \Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample(<<<'CODE_SAMPLE'
class SomeClass
{
/**
* @var int[]
*/
private $values;
public function __construct(array $values)
{
$this->values = $values;
}
}
CODE_SAMPLE
, <<<'CODE_SAMPLE'
class SomeClass
{
/**
* @var int[]
*/
private $values;
/**
* @param int[] $values
*/
public function __construct(array $values)
{
$this->values = $values;
}
}
CODE_SAMPLE
)]);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes() : array
{
return [\PhpParser\Node\Stmt\ClassMethod::class];
}
/**
* @param ClassMethod $node
*/
public function refactor(\PhpParser\Node $node) : ?\PhpParser\Node
{
if ($node->getParams() === []) {
return null;
}
$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($node);
foreach ($node->getParams() as $param) {
if ($this->shouldSkipParam($param)) {
continue;
}
$paramType = $this->paramTypeInferer->inferParam($param);
if ($paramType instanceof \PHPStan\Type\MixedType) {
continue;
}
if ($this->paramAnalyzer->isNullable($param) && !$paramType instanceof \PHPStan\Type\UnionType) {
$paramType = new \PHPStan\Type\UnionType([$paramType, new \PHPStan\Type\NullType()]);
}
$paramName = $this->getName($param);
$this->phpDocTypeChanger->changeParamType($phpDocInfo, $paramType, $param, $paramName);
}
if ($phpDocInfo->hasChanged()) {
$node->setAttribute(\Rector\NodeTypeResolver\Node\AttributeKey::HAS_PHP_DOC_INFO_JUST_CHANGED, \true);
$this->paramTagRemover->removeParamTagsIfUseless($phpDocInfo, $node);
return $node;
}
return null;
}
private function shouldSkipParam(\PhpParser\Node\Param $param) : bool
{
// type missing at all
if ($param->type === null) {
return \true;
}
// not an array type
$paramType = $this->nodeTypeResolver->resolve($param->type);
// weird case for maybe interface
if ($paramType->isIterable()->maybe() && $paramType instanceof \PHPStan\Type\ObjectType) {
return \true;
}
$isArrayable = $paramType->isIterable()->yes() || $paramType->isArray()->yes() || ($paramType->isIterable()->maybe() || $paramType->isArray()->maybe());
if (!$isArrayable) {
return \true;
}
return $this->isArrayExplicitMixed($paramType);
}
private function isArrayExplicitMixed(\PHPStan\Type\Type $type) : bool
{
if (!$type instanceof \PHPStan\Type\ArrayType) {
return \false;
}
$iterableValueType = $type->getIterableValueType();
if (!$iterableValueType instanceof \PHPStan\Type\MixedType) {
return \false;
}
return $iterableValueType->isExplicitMixed();
}
}