Skip to content

Commit

Permalink
[Php80] Copy PhpDocInfo values on ClassPropertyAssignToConstructorPro…
Browse files Browse the repository at this point in the history
…motionRector (#744)

* Add failing test fixture for ClassPropertyAssignToConstructorPromotionRector

# Failing Test for ClassPropertyAssignToConstructorPromotionRector

Based on https://getrector.org/demo/1ec03dbd-591c-689e-b45c-6563b6259c57

* Update y.php.inc

* update fixture

* Closes #743

* phpstan

* move to PhpDocTypeChanger

Co-authored-by: Jáchym Toušek <enumag@gmail.com>
  • Loading branch information
samsonasik and enumag committed Aug 23, 2021
1 parent 241f8a9 commit 7eb5e75
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@
namespace Rector\BetterPhpDocParser\PhpDocManipulator;

use PhpParser\Node\Param;
use PhpParser\Node\Stmt\Property;
use PHPStan\PhpDocParser\Ast\PhpDoc\ReturnTagValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\VarTagValueNode;
use PHPStan\Type\Constant\ConstantArrayType;
use PHPStan\Type\MixedType;
use PHPStan\Type\NeverType;
use PHPStan\Type\Type;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\NodeTypeResolver\TypeComparator\TypeComparator;
use Rector\PHPStanStaticTypeMapper\ValueObject\TypeKind;
use Rector\StaticTypeMapper\StaticTypeMapper;
Expand Down Expand Up @@ -123,4 +125,24 @@ public function changeParamType(PhpDocInfo $phpDocInfo, Type $newType, Param $pa
$phpDocInfo->addTagValueNode($paramTagValueNode);
}
}

public function copyPropertyDocToParam(Property $property, Param $param): void
{
$phpDocInfo = $property->getAttribute(AttributeKey::PHP_DOC_INFO);
if (! $phpDocInfo) {
return;
}

$varTag = $phpDocInfo->getVarTagValueNode();
if (! $varTag instanceof VarTagValueNode) {
return;
}

if ($varTag->description !== '') {
return;
}

$phpDocInfo->removeByType(VarTagValueNode::class);
$param->setAttribute(AttributeKey::PHP_DOC_INFO, $phpDocInfo);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php declare(strict_types = 1);

namespace Rector\Tests\Php80\Rector\Class_\ClassPropertyAssignToConstructorPromotionRector\Fixture;

use Symfony\Component\Validator\Constraints as Assert;

class Y {}
class Z {}

final class CopyDoc
{
public Y $y;

/**
* @var Z[]
* @Assert\Valid()
* @Assert\NotBlank()
*/
public array $z = [];

/**
* @param Z[] $z
*/
public function __construct(Y $y, array $z = [])
{
$this->y = $y;
$this->z = $z;
}
}
?>
-----
<?php declare(strict_types = 1);

namespace Rector\Tests\Php80\Rector\Class_\ClassPropertyAssignToConstructorPromotionRector\Fixture;

use Symfony\Component\Validator\Constraints as Assert;

class Y {}
class Z {}

final class CopyDoc
{
/**
* @param Z[] $z
*/
public function __construct(
public Y $y,
/**
* @Assert\Valid()
* @Assert\NotBlank()
*/
public array $z = []
)
{
}
}
?>
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Property;
use PHPStan\PhpDocParser\Ast\PhpDoc\ParamTagValueNode;
use Rector\BetterPhpDocParser\PhpDocManipulator\PhpDocTypeChanger;
use Rector\BetterPhpDocParser\ValueObject\PhpDocAttributeKey;
use Rector\Core\NodeAnalyzer\ParamAnalyzer;
use Rector\Core\Rector\AbstractRector;
Expand All @@ -37,7 +38,8 @@ public function __construct(
private PromotedPropertyCandidateResolver $promotedPropertyCandidateResolver,
private VariableRenamer $variableRenamer,
private VarTagRemover $varTagRemover,
private ParamAnalyzer $paramAnalyzer
private ParamAnalyzer $paramAnalyzer,
private PhpDocTypeChanger $phpDocTypeChanger
) {
}

Expand Down Expand Up @@ -132,6 +134,8 @@ public function refactor(Node $node): ?Node
// Copy over attributes of the "old" property
$param->attrGroups = $property->attrGroups;
$this->processNullableType($property, $param);

$this->phpDocTypeChanger->copyPropertyDocToParam($property, $param);
}

return $node;
Expand Down

0 comments on commit 7eb5e75

Please sign in to comment.