2021-02-15 16:38:30 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
2021-04-28 20:33:20 +00:00
|
|
|
namespace Rector\TypeDeclaration\Rector\Param;
|
2021-02-15 16:38:30 +00:00
|
|
|
|
|
|
|
use PhpParser\Node;
|
|
|
|
use PhpParser\Node\Expr\Assign;
|
|
|
|
use PhpParser\Node\Expr\PropertyFetch;
|
2021-02-15 19:31:53 +00:00
|
|
|
use PhpParser\Node\FunctionLike;
|
2021-02-15 16:38:30 +00:00
|
|
|
use PhpParser\Node\NullableType;
|
|
|
|
use PhpParser\Node\Param;
|
|
|
|
use PhpParser\Node\Stmt\Property;
|
|
|
|
use PhpParser\Node\UnionType;
|
2021-04-28 21:12:26 +00:00
|
|
|
use PHPStan\Analyser\Scope;
|
2021-05-03 18:03:01 +00:00
|
|
|
use PHPStan\Type\MixedType;
|
2021-02-15 16:38:30 +00:00
|
|
|
use PHPStan\Type\Type;
|
|
|
|
use Rector\Core\Rector\AbstractRector;
|
|
|
|
use Rector\Core\ValueObject\PhpVersionFeature;
|
2021-04-28 20:33:20 +00:00
|
|
|
use Rector\NodeTypeResolver\Node\AttributeKey;
|
2021-02-15 19:31:53 +00:00
|
|
|
use Rector\TypeDeclaration\Reflection\ReflectionTypeResolver;
|
2021-02-15 16:38:30 +00:00
|
|
|
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
|
|
|
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
|
|
|
|
|
|
|
/**
|
2021-04-28 20:33:20 +00:00
|
|
|
* @see \Rector\Tests\TypeDeclaration\Rector\Param\ParamTypeFromStrictTypedPropertyRector\ParamTypeFromStrictTypedPropertyRectorTest
|
2021-02-15 16:38:30 +00:00
|
|
|
*/
|
|
|
|
final class ParamTypeFromStrictTypedPropertyRector extends AbstractRector
|
|
|
|
{
|
2021-02-15 19:31:53 +00:00
|
|
|
/**
|
|
|
|
* @var ReflectionTypeResolver
|
|
|
|
*/
|
|
|
|
private $reflectionTypeResolver;
|
|
|
|
|
2021-04-28 20:33:20 +00:00
|
|
|
public function __construct(ReflectionTypeResolver $reflectionTypeResolver)
|
|
|
|
{
|
2021-02-15 19:31:53 +00:00
|
|
|
$this->reflectionTypeResolver = $reflectionTypeResolver;
|
2021-02-15 16:38:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getRuleDefinition(): RuleDefinition
|
|
|
|
{
|
|
|
|
return new RuleDefinition('Add param type from $param set to typed property', [
|
|
|
|
new CodeSample(
|
|
|
|
<<<'CODE_SAMPLE'
|
|
|
|
final class SomeClass
|
|
|
|
{
|
|
|
|
private int $age;
|
|
|
|
|
|
|
|
public function setAge($age)
|
|
|
|
{
|
|
|
|
$this->age = $age;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
CODE_SAMPLE
|
|
|
|
|
|
|
|
,
|
|
|
|
<<<'CODE_SAMPLE'
|
|
|
|
final class SomeClass
|
|
|
|
{
|
|
|
|
private int $age;
|
|
|
|
|
|
|
|
public function setAge(int $age)
|
|
|
|
{
|
|
|
|
$this->age = $age;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
CODE_SAMPLE
|
|
|
|
),
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-02-27 00:06:15 +00:00
|
|
|
* @return array<class-string<Node>>
|
2021-02-15 16:38:30 +00:00
|
|
|
*/
|
|
|
|
public function getNodeTypes(): array
|
|
|
|
{
|
2021-04-28 20:33:20 +00:00
|
|
|
return [Param::class];
|
2021-02-15 16:38:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-04-28 20:33:20 +00:00
|
|
|
* @param Param $node
|
2021-02-15 16:38:30 +00:00
|
|
|
*/
|
|
|
|
public function refactor(Node $node): ?Node
|
|
|
|
{
|
|
|
|
if (! $this->isAtLeastPhpVersion(PhpVersionFeature::TYPED_PROPERTIES)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2021-04-28 20:33:20 +00:00
|
|
|
$parent = $node->getAttribute(AttributeKey::PARENT_NODE);
|
|
|
|
if (! $parent instanceof FunctionLike) {
|
|
|
|
return null;
|
2021-02-15 16:38:30 +00:00
|
|
|
}
|
|
|
|
|
2021-04-28 20:33:20 +00:00
|
|
|
return $this->decorateParamWithType($parent, $node);
|
2021-02-15 16:38:30 +00:00
|
|
|
}
|
|
|
|
|
2021-04-28 20:33:20 +00:00
|
|
|
public function decorateParamWithType(FunctionLike $functionLike, Param $param): ?Param
|
2021-02-15 16:38:30 +00:00
|
|
|
{
|
|
|
|
if ($param->type !== null) {
|
2021-04-28 20:33:20 +00:00
|
|
|
return null;
|
2021-02-15 16:38:30 +00:00
|
|
|
}
|
|
|
|
|
2021-05-03 18:03:01 +00:00
|
|
|
$originalParamType = $this->resolveParamOriginalType($param);
|
2021-04-28 21:12:26 +00:00
|
|
|
|
|
|
|
$paramName = $this->getName($param);
|
|
|
|
|
2021-04-28 20:33:20 +00:00
|
|
|
/** @var Assign[] $assigns */
|
|
|
|
$assigns = $this->betterNodeFinder->findInstanceOf((array) $functionLike->getStmts(), Assign::class);
|
|
|
|
foreach ($assigns as $assign) {
|
|
|
|
if (! $this->nodeComparator->areNodesEqual($assign->expr, $param->var)) {
|
|
|
|
continue;
|
2021-02-15 16:38:30 +00:00
|
|
|
}
|
|
|
|
|
2021-04-28 20:33:20 +00:00
|
|
|
if (! $assign->var instanceof PropertyFetch) {
|
|
|
|
continue;
|
2021-02-15 16:38:30 +00:00
|
|
|
}
|
|
|
|
|
2021-04-28 21:12:26 +00:00
|
|
|
if ($this->hasTypeChangedBeforeAssign($assign, $paramName, $originalParamType)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2021-04-28 20:33:20 +00:00
|
|
|
$singlePropertyTypeNode = $this->matchPropertySingleTypeNode($assign->var);
|
2021-02-15 19:31:53 +00:00
|
|
|
if (! $singlePropertyTypeNode instanceof Node) {
|
2021-02-15 16:38:30 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2021-02-15 19:31:53 +00:00
|
|
|
$param->type = $singlePropertyTypeNode;
|
2021-04-28 20:33:20 +00:00
|
|
|
return $param;
|
|
|
|
}
|
2021-02-15 16:38:30 +00:00
|
|
|
|
2021-04-28 20:33:20 +00:00
|
|
|
return null;
|
2021-02-15 16:38:30 +00:00
|
|
|
}
|
|
|
|
|
2021-04-25 23:12:14 +00:00
|
|
|
/**
|
|
|
|
* @return Node\Identifier|Node\Name|UnionType|NullableType|null
|
|
|
|
*/
|
2021-02-15 19:31:53 +00:00
|
|
|
private function matchPropertySingleTypeNode(PropertyFetch $propertyFetch): ?Node
|
2021-02-15 16:38:30 +00:00
|
|
|
{
|
|
|
|
$property = $this->nodeRepository->findPropertyByPropertyFetch($propertyFetch);
|
|
|
|
if (! $property instanceof Property) {
|
2021-02-15 19:31:53 +00:00
|
|
|
// code from /vendor
|
|
|
|
$propertyFetchType = $this->reflectionTypeResolver->resolvePropertyFetchType($propertyFetch);
|
|
|
|
if (! $propertyFetchType instanceof Type) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->staticTypeMapper->mapPHPStanTypeToPhpParserNode($propertyFetchType);
|
2021-02-15 16:38:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($property->type === null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
// move type to param if not union type
|
|
|
|
if ($property->type instanceof UnionType) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($property->type instanceof NullableType) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2021-04-28 20:33:20 +00:00
|
|
|
// needed to avoid reprinting original tokens bug
|
|
|
|
$typeNode = clone $property->type;
|
|
|
|
$typeNode->setAttribute(AttributeKey::ORIGINAL_NODE, null);
|
|
|
|
|
|
|
|
return $typeNode;
|
2021-02-15 16:38:30 +00:00
|
|
|
}
|
2021-04-28 21:12:26 +00:00
|
|
|
|
|
|
|
private function hasTypeChangedBeforeAssign(Assign $assign, string $paramName, Type $originalType): bool
|
|
|
|
{
|
|
|
|
$scope = $assign->getAttribute(AttributeKey::SCOPE);
|
|
|
|
if (! $scope instanceof Scope) {
|
|
|
|
return false;
|
|
|
|
}
|
2021-05-03 18:03:01 +00:00
|
|
|
|
|
|
|
if (! $scope->hasVariableType($paramName)->yes()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-04-28 21:12:26 +00:00
|
|
|
$currentParamType = $scope->getVariableType($paramName);
|
|
|
|
return ! $currentParamType->equals($originalType);
|
|
|
|
}
|
2021-05-03 18:03:01 +00:00
|
|
|
|
|
|
|
private function resolveParamOriginalType(Param $param): Type
|
|
|
|
{
|
|
|
|
$scope = $param->getAttribute(AttributeKey::SCOPE);
|
|
|
|
if (! $scope instanceof Scope) {
|
|
|
|
return new MixedType();
|
|
|
|
}
|
|
|
|
|
|
|
|
$paramName = $this->getName($param);
|
|
|
|
if (! $scope->hasVariableType($paramName)->yes()) {
|
|
|
|
return new MixedType();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $scope->getVariableType($paramName);
|
|
|
|
}
|
2021-02-15 16:38:30 +00:00
|
|
|
}
|