[TypeDeclaration] Traverse the param node that is changed in ParamTypeFromStrictTypedPropertyRector (#6260)

This commit is contained in:
Tomas Votruba 2021-04-28 22:33:20 +02:00 committed by GitHub
parent cfa8295e95
commit ca4d1fb1aa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
18 changed files with 87 additions and 105 deletions

View File

@ -4,11 +4,11 @@ declare(strict_types=1);
use Rector\TypeDeclaration\Rector\ClassMethod\AddMethodCallBasedStrictParamTypeRector;
use Rector\TypeDeclaration\Rector\ClassMethod\AddVoidReturnTypeWhereNoReturnRector;
use Rector\TypeDeclaration\Rector\ClassMethod\ParamTypeFromStrictTypedPropertyRector;
use Rector\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromReturnNewRector;
use Rector\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictTypedCallRector;
use Rector\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictTypedPropertyRector;
use Rector\TypeDeclaration\Rector\Closure\AddClosureReturnTypeRector;
use Rector\TypeDeclaration\Rector\Param\ParamTypeFromStrictTypedPropertyRector;
use Rector\TypeDeclaration\Rector\Property\TypedPropertyFromStrictConstructorRector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;

View File

@ -12175,7 +12175,7 @@ Change `@param` types to type declarations if not a BC-break
Add param type from `$param` set to typed property
- class: [`Rector\TypeDeclaration\Rector\ClassMethod\ParamTypeFromStrictTypedPropertyRector`](../rules/TypeDeclaration/Rector/ClassMethod/ParamTypeFromStrictTypedPropertyRector.php)
- class: [`Rector\TypeDeclaration\Rector\Param\ParamTypeFromStrictTypedPropertyRector`](../rules/TypeDeclaration/Rector/ClassMethod/ParamTypeFromStrictTypedPropertyRector.php)
```diff
final class SomeClass

View File

@ -1,31 +0,0 @@
<?php
namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ParamTypeFromStrictTypedPropertyRector\Fixture;
use Rector\Tests\TypeDeclaration\Rector\ClassMethod\ParamTypeFromStrictTypedPropertyRector\Source\OutOfControlExternalClass;
final class VendorExternalType
{
public function setValues($age, OutOfControlExternalClass $outOfControlExternalClass)
{
$outOfControlExternalClass->name = $age;
}
}
?>
-----
<?php
namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ParamTypeFromStrictTypedPropertyRector\Fixture;
use Rector\Tests\TypeDeclaration\Rector\ClassMethod\ParamTypeFromStrictTypedPropertyRector\Source\OutOfControlExternalClass;
final class VendorExternalType
{
public function setValues(string $age, OutOfControlExternalClass $outOfControlExternalClass)
{
$outOfControlExternalClass->name = $age;
}
}
?>

View File

@ -1,10 +0,0 @@
<?php
declare(strict_types=1);
namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ParamTypeFromStrictTypedPropertyRector\Source;
final class OutOfControlExternalClass
{
public string $name;
}

View File

@ -1,6 +1,6 @@
<?php
namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ParamTypeFromStrictTypedPropertyRector\Fixture;
namespace Rector\Tests\TypeDeclaration\Rector\Param\ParamTypeFromStrictTypedPropertyRector\Fixture;
final class EvenConstructor
{
@ -16,7 +16,7 @@ final class EvenConstructor
-----
<?php
namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ParamTypeFromStrictTypedPropertyRector\Fixture;
namespace Rector\Tests\TypeDeclaration\Rector\Param\ParamTypeFromStrictTypedPropertyRector\Fixture;
final class EvenConstructor
{

View File

@ -1,6 +1,6 @@
<?php
namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ParamTypeFromStrictTypedPropertyRector\Fixture;
namespace Rector\Tests\TypeDeclaration\Rector\Param\ParamTypeFromStrictTypedPropertyRector\Fixture;
final class ExternalType
{
@ -19,7 +19,7 @@ final class ExternalClass
-----
<?php
namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ParamTypeFromStrictTypedPropertyRector\Fixture;
namespace Rector\Tests\TypeDeclaration\Rector\Param\ParamTypeFromStrictTypedPropertyRector\Fixture;
final class ExternalType
{

View File

@ -1,6 +1,6 @@
<?php
namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ParamTypeFromStrictTypedPropertyRector\Fixture;
namespace Rector\Tests\TypeDeclaration\Rector\Param\ParamTypeFromStrictTypedPropertyRector\Fixture;
final class MultipleParams
{
@ -19,7 +19,7 @@ final class MultipleParams
-----
<?php
namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ParamTypeFromStrictTypedPropertyRector\Fixture;
namespace Rector\Tests\TypeDeclaration\Rector\Param\ParamTypeFromStrictTypedPropertyRector\Fixture;
final class MultipleParams
{

View File

@ -1,6 +1,6 @@
<?php
namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ParamTypeFromStrictTypedPropertyRector\Fixture;
namespace Rector\Tests\TypeDeclaration\Rector\Param\ParamTypeFromStrictTypedPropertyRector\Fixture;
final class SkipAlreadyType
{

View File

@ -1,6 +1,6 @@
<?php
namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ParamTypeFromStrictTypedPropertyRector\Fixture;
namespace Rector\Tests\TypeDeclaration\Rector\Param\ParamTypeFromStrictTypedPropertyRector\Fixture;
final class SkipDocType
{

View File

@ -1,6 +1,6 @@
<?php
namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ParamTypeFromStrictTypedPropertyRector\Fixture;
namespace Rector\Tests\TypeDeclaration\Rector\Param\ParamTypeFromStrictTypedPropertyRector\Fixture;
final class SkipNullableType
{

View File

@ -1,6 +1,6 @@
<?php
namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ParamTypeFromStrictTypedPropertyRector\Fixture;
namespace Rector\Tests\TypeDeclaration\Rector\Param\ParamTypeFromStrictTypedPropertyRector\Fixture;
final class SkipUnionType
{

View File

@ -1,6 +1,6 @@
<?php
namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ParamTypeFromStrictTypedPropertyRector\Fixture;
namespace Rector\Tests\TypeDeclaration\Rector\Param\ParamTypeFromStrictTypedPropertyRector\Fixture;
final class SomeClass
{
@ -16,7 +16,7 @@ final class SomeClass
-----
<?php
namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ParamTypeFromStrictTypedPropertyRector\Fixture;
namespace Rector\Tests\TypeDeclaration\Rector\Param\ParamTypeFromStrictTypedPropertyRector\Fixture;
final class SomeClass
{

View File

@ -0,0 +1,31 @@
<?php
namespace Rector\Tests\TypeDeclaration\Rector\Param\ParamTypeFromStrictTypedPropertyRector\Fixture;
use Rector\Tests\TypeDeclaration\Rector\Param\ParamTypeFromStrictTypedPropertyRector\Source\OutOfControlExternalClass;
final class VendorExternalType
{
public function setValues($age, OutOfControlExternalClass $outOfControlExternalClass)
{
$outOfControlExternalClass->name = $age;
}
}
?>
-----
<?php
namespace Rector\Tests\TypeDeclaration\Rector\Param\ParamTypeFromStrictTypedPropertyRector\Fixture;
use Rector\Tests\TypeDeclaration\Rector\Param\ParamTypeFromStrictTypedPropertyRector\Source\OutOfControlExternalClass;
final class VendorExternalType
{
public function setValues(string $age, OutOfControlExternalClass $outOfControlExternalClass)
{
$outOfControlExternalClass->name = $age;
}
}
?>

View File

@ -2,7 +2,7 @@
declare(strict_types=1);
namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ParamTypeFromStrictTypedPropertyRector;
namespace Rector\Tests\TypeDeclaration\Rector\Param\ParamTypeFromStrictTypedPropertyRector;
use Iterator;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;

View File

@ -0,0 +1,10 @@
<?php
declare(strict_types=1);
namespace Rector\Tests\TypeDeclaration\Rector\Param\ParamTypeFromStrictTypedPropertyRector\Source;
final class OutOfControlExternalClass
{
public string $name;
}

View File

@ -2,7 +2,7 @@
declare(strict_types=1);
use Rector\TypeDeclaration\Rector\ClassMethod\ParamTypeFromStrictTypedPropertyRector;
use Rector\TypeDeclaration\Rector\Param\ParamTypeFromStrictTypedPropertyRector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {

View File

@ -2,50 +2,36 @@
declare(strict_types=1);
namespace Rector\TypeDeclaration\Rector\ClassMethod;
namespace Rector\TypeDeclaration\Rector\Param;
use PhpParser\Node;
use PhpParser\Node\Expr\ArrowFunction;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\Closure;
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\FunctionLike;
use PhpParser\Node\NullableType;
use PhpParser\Node\Param;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Function_;
use PhpParser\Node\Stmt\Property;
use PhpParser\Node\UnionType;
use PhpParser\NodeTraverser;
use PHPStan\Type\Type;
use Rector\ChangesReporting\ValueObject\RectorWithLineChange;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\ValueObject\PhpVersionFeature;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\TypeDeclaration\Reflection\ReflectionTypeResolver;
use Symplify\Astral\NodeTraverser\SimpleCallableNodeTraverser;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @see \Rector\Tests\TypeDeclaration\Rector\ClassMethod\ParamTypeFromStrictTypedPropertyRector\ParamTypeFromStrictTypedPropertyRectorTest
* @see \Rector\Tests\TypeDeclaration\Rector\Param\ParamTypeFromStrictTypedPropertyRector\ParamTypeFromStrictTypedPropertyRectorTest
*/
final class ParamTypeFromStrictTypedPropertyRector extends AbstractRector
{
/**
* @var SimpleCallableNodeTraverser
*/
private $simpleCallableNodeTraverser;
/**
* @var ReflectionTypeResolver
*/
private $reflectionTypeResolver;
public function __construct(
SimpleCallableNodeTraverser $simpleCallableNodeTraverser,
ReflectionTypeResolver $reflectionTypeResolver
) {
$this->simpleCallableNodeTraverser = $simpleCallableNodeTraverser;
public function __construct(ReflectionTypeResolver $reflectionTypeResolver)
{
$this->reflectionTypeResolver = $reflectionTypeResolver;
}
@ -87,11 +73,11 @@ CODE_SAMPLE
*/
public function getNodeTypes(): array
{
return [ClassMethod::class, Function_::class, Closure::class, ArrowFunction::class];
return [Param::class];
}
/**
* @param ClassMethod|Function_|Closure|ArrowFunction $node
* @param Param $node
*/
public function refactor(Node $node): ?Node
{
@ -99,49 +85,41 @@ CODE_SAMPLE
return null;
}
foreach ($node->getParams() as $param) {
$this->decorateParamWithType($node, $param);
$parent = $node->getAttribute(AttributeKey::PARENT_NODE);
if (! $parent instanceof FunctionLike) {
return null;
}
return $node;
return $this->decorateParamWithType($parent, $node);
}
/**
* @param ClassMethod|Function_|Closure|ArrowFunction $functionLike
*/
public function decorateParamWithType(FunctionLike $functionLike, Param $param): void
public function decorateParamWithType(FunctionLike $functionLike, Param $param): ?Param
{
if ($param->type !== null) {
return;
return null;
}
$this->simpleCallableNodeTraverser->traverseNodesWithCallable((array) $functionLike->getStmts(), function (
Node $node
) use ($param): ?int {
if (! $node instanceof Assign) {
return null;
/** @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;
}
if (! $this->nodeComparator->areNodesEqual($node->expr, $param)) {
return null;
if (! $assign->var instanceof PropertyFetch) {
continue;
}
if (! $node->var instanceof PropertyFetch) {
return null;
}
$singlePropertyTypeNode = $this->matchPropertySingleTypeNode($node->var);
$singlePropertyTypeNode = $this->matchPropertySingleTypeNode($assign->var);
if (! $singlePropertyTypeNode instanceof Node) {
return null;
}
$rectorWithLineChange = new RectorWithLineChange($this, $node->getLine());
$this->file->addRectorClassWithLine($rectorWithLineChange);
$param->type = $singlePropertyTypeNode;
return $param;
}
return NodeTraverser::STOP_TRAVERSAL;
});
return null;
}
/**
@ -173,6 +151,10 @@ CODE_SAMPLE
return null;
}
return $property->type;
// needed to avoid reprinting original tokens bug
$typeNode = clone $property->type;
$typeNode->setAttribute(AttributeKey::ORIGINAL_NODE, null);
return $typeNode;
}
}

View File

@ -16,8 +16,8 @@ use PhpParser\Parser;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Type\ThisType;
use PHPStan\Type\ObjectType;
use PHPStan\Type\ThisType;
use Rector\Core\ValueObject\Application\File;
use Rector\NodeNameResolver\NodeNameResolver;
use Rector\NodeTypeResolver\NodeScopeAndMetadataDecorator;