Skip to content

Commit

Permalink
[TypeDeclaration] Traverse the param node that is changed in ParamTyp…
Browse files Browse the repository at this point in the history
…eFromStrictTypedPropertyRector (#6260)
  • Loading branch information
TomasVotruba authored Apr 28, 2021
1 parent cfa8295 commit ca4d1fb
Show file tree
Hide file tree
Showing 18 changed files with 87 additions and 105 deletions.
2 changes: 1 addition & 1 deletion config/set/type-declaration-strict.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@

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;

Expand Down
2 changes: 1 addition & 1 deletion docs/rector_rules_overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ParamTypeFromStrictTypedPropertyRector\Fixture;
namespace Rector\Tests\TypeDeclaration\Rector\Param\ParamTypeFromStrictTypedPropertyRector\Fixture;

final class EvenConstructor
{
Expand All @@ -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
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ParamTypeFromStrictTypedPropertyRector\Fixture;
namespace Rector\Tests\TypeDeclaration\Rector\Param\ParamTypeFromStrictTypedPropertyRector\Fixture;

final class ExternalType
{
Expand All @@ -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
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ParamTypeFromStrictTypedPropertyRector\Fixture;
namespace Rector\Tests\TypeDeclaration\Rector\Param\ParamTypeFromStrictTypedPropertyRector\Fixture;

final class MultipleParams
{
Expand All @@ -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
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ParamTypeFromStrictTypedPropertyRector\Fixture;
namespace Rector\Tests\TypeDeclaration\Rector\Param\ParamTypeFromStrictTypedPropertyRector\Fixture;

final class SkipAlreadyType
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ParamTypeFromStrictTypedPropertyRector\Fixture;
namespace Rector\Tests\TypeDeclaration\Rector\Param\ParamTypeFromStrictTypedPropertyRector\Fixture;

final class SkipDocType
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ParamTypeFromStrictTypedPropertyRector\Fixture;
namespace Rector\Tests\TypeDeclaration\Rector\Param\ParamTypeFromStrictTypedPropertyRector\Fixture;

final class SkipNullableType
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ParamTypeFromStrictTypedPropertyRector\Fixture;
namespace Rector\Tests\TypeDeclaration\Rector\Param\ParamTypeFromStrictTypedPropertyRector\Fixture;

final class SkipUnionType
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ParamTypeFromStrictTypedPropertyRector\Fixture;
namespace Rector\Tests\TypeDeclaration\Rector\Param\ParamTypeFromStrictTypedPropertyRector\Fixture;

final class SomeClass
{
Expand All @@ -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
{
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\TypeDeclaration\Rector\Param\ParamTypeFromStrictTypedPropertyRector\Source;

final class OutOfControlExternalClass
{
public string $name;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -87,61 +73,53 @@ public function setAge(int $age)
*/
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
{
if (! $this->isAtLeastPhpVersion(PhpVersionFeature::TYPED_PROPERTIES)) {
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;
}

if (! $this->nodeComparator->areNodesEqual($node->expr, $param)) {
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 (! $node->var instanceof PropertyFetch) {
return null;
if (! $assign->var instanceof PropertyFetch) {
continue;
}

$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;
}

/**
Expand Down Expand Up @@ -173,6 +151,10 @@ private function matchPropertySingleTypeNode(PropertyFetch $propertyFetch): ?Nod
return null;
}

return $property->type;
// needed to avoid reprinting original tokens bug
$typeNode = clone $property->type;
$typeNode->setAttribute(AttributeKey::ORIGINAL_NODE, null);

return $typeNode;
}
}
2 changes: 1 addition & 1 deletion src/Reflection/FunctionLikeReflectionParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
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;
Expand Down

0 comments on commit ca4d1fb

Please sign in to comment.