2021-02-15 16:38:30 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace Rector\TypeDeclaration\Rector\ClassMethod;
|
|
|
|
|
|
|
|
use PhpParser\Node;
|
2021-02-15 19:31:53 +00:00
|
|
|
use PhpParser\Node\Expr\ArrowFunction;
|
2021-02-15 16:38:30 +00:00
|
|
|
use PhpParser\Node\Expr\Assign;
|
2021-02-15 19:31:53 +00:00
|
|
|
use PhpParser\Node\Expr\Closure;
|
2021-02-15 16:38:30 +00:00
|
|
|
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\ClassMethod;
|
2021-02-15 19:31:53 +00:00
|
|
|
use PhpParser\Node\Stmt\Function_;
|
2021-02-15 16:38:30 +00:00
|
|
|
use PhpParser\Node\Stmt\Property;
|
|
|
|
use PhpParser\Node\UnionType;
|
|
|
|
use PhpParser\NodeTraverser;
|
|
|
|
use PHPStan\Type\Type;
|
2021-04-22 21:51:32 +00:00
|
|
|
use Rector\ChangesReporting\ValueObject\RectorWithLineChange;
|
2021-02-15 16:38:30 +00:00
|
|
|
use Rector\Core\Rector\AbstractRector;
|
|
|
|
use Rector\Core\ValueObject\PhpVersionFeature;
|
2021-02-15 19:31:53 +00:00
|
|
|
use Rector\TypeDeclaration\Reflection\ReflectionTypeResolver;
|
2021-02-15 16:38:30 +00:00
|
|
|
use Symplify\Astral\NodeTraverser\SimpleCallableNodeTraverser;
|
|
|
|
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
|
|
|
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
|
|
|
|
|
|
|
/**
|
2021-03-12 22:20:25 +00:00
|
|
|
* @see \Rector\Tests\TypeDeclaration\Rector\ClassMethod\ParamTypeFromStrictTypedPropertyRector\ParamTypeFromStrictTypedPropertyRectorTest
|
2021-02-15 16:38:30 +00:00
|
|
|
*/
|
|
|
|
final class ParamTypeFromStrictTypedPropertyRector extends AbstractRector
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var SimpleCallableNodeTraverser
|
|
|
|
*/
|
|
|
|
private $simpleCallableNodeTraverser;
|
|
|
|
|
2021-02-15 19:31:53 +00:00
|
|
|
/**
|
|
|
|
* @var ReflectionTypeResolver
|
|
|
|
*/
|
|
|
|
private $reflectionTypeResolver;
|
|
|
|
|
|
|
|
public function __construct(
|
|
|
|
SimpleCallableNodeTraverser $simpleCallableNodeTraverser,
|
|
|
|
ReflectionTypeResolver $reflectionTypeResolver
|
|
|
|
) {
|
2021-02-15 16:38:30 +00:00
|
|
|
$this->simpleCallableNodeTraverser = $simpleCallableNodeTraverser;
|
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-02-15 19:31:53 +00:00
|
|
|
return [ClassMethod::class, Function_::class, Closure::class, ArrowFunction::class];
|
2021-02-15 16:38:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-02-15 19:31:53 +00:00
|
|
|
* @param ClassMethod|Function_|Closure|ArrowFunction $node
|
2021-02-15 16:38:30 +00:00
|
|
|
*/
|
|
|
|
public function refactor(Node $node): ?Node
|
|
|
|
{
|
|
|
|
if (! $this->isAtLeastPhpVersion(PhpVersionFeature::TYPED_PROPERTIES)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($node->getParams() as $param) {
|
|
|
|
$this->decorateParamWithType($node, $param);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $node;
|
|
|
|
}
|
|
|
|
|
2021-02-15 19:31:53 +00:00
|
|
|
/**
|
|
|
|
* @param ClassMethod|Function_|Closure|ArrowFunction $functionLike
|
|
|
|
*/
|
|
|
|
public function decorateParamWithType(FunctionLike $functionLike, Param $param): void
|
2021-02-15 16:38:30 +00:00
|
|
|
{
|
|
|
|
if ($param->type !== null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-02-15 19:31:53 +00:00
|
|
|
$this->simpleCallableNodeTraverser->traverseNodesWithCallable((array) $functionLike->getStmts(), function (
|
2021-02-15 16:38:30 +00:00
|
|
|
Node $node
|
|
|
|
) use ($param): ?int {
|
|
|
|
if (! $node instanceof Assign) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2021-02-19 12:01:23 +00:00
|
|
|
if (! $this->nodeComparator->areNodesEqual($node->expr, $param)) {
|
2021-02-15 16:38:30 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (! $node->var instanceof PropertyFetch) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2021-02-15 19:31:53 +00:00
|
|
|
$singlePropertyTypeNode = $this->matchPropertySingleTypeNode($node->var);
|
|
|
|
if (! $singlePropertyTypeNode instanceof Node) {
|
2021-02-15 16:38:30 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2021-04-22 21:51:32 +00:00
|
|
|
$rectorWithLineChange = new RectorWithLineChange($this, $node->getLine());
|
|
|
|
$this->file->addRectorClassWithLine($rectorWithLineChange);
|
|
|
|
|
2021-02-15 19:31:53 +00:00
|
|
|
$param->type = $singlePropertyTypeNode;
|
2021-02-15 16:38:30 +00:00
|
|
|
|
|
|
|
return NodeTraverser::STOP_TRAVERSAL;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
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-02-15 19:31:53 +00:00
|
|
|
return $property->type;
|
2021-02-15 16:38:30 +00:00
|
|
|
}
|
|
|
|
}
|