mirror of
https://github.com/rectorphp/rector.git
synced 2024-06-08 12:20:52 +00:00
[Php80] Copy PhpDocInfo values on ClassPropertyAssignToConstructorPromotionRector (#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>
This commit is contained in:
parent
241f8a9ce1
commit
7eb5e75cda
|
@ -5,6 +5,7 @@ declare(strict_types=1);
|
|||
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;
|
||||
|
@ -12,6 +13,7 @@ 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;
|
||||
|
@ -123,4 +125,24 @@ final class PhpDocTypeChanger
|
|||
$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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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 = []
|
||||
)
|
||||
{
|
||||
}
|
||||
}
|
||||
?>
|
|
@ -12,6 +12,7 @@ use PhpParser\Node\Stmt\Class_;
|
|||
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;
|
||||
|
@ -37,7 +38,8 @@ final class ClassPropertyAssignToConstructorPromotionRector extends AbstractRect
|
|||
private PromotedPropertyCandidateResolver $promotedPropertyCandidateResolver,
|
||||
private VariableRenamer $variableRenamer,
|
||||
private VarTagRemover $varTagRemover,
|
||||
private ParamAnalyzer $paramAnalyzer
|
||||
private ParamAnalyzer $paramAnalyzer,
|
||||
private PhpDocTypeChanger $phpDocTypeChanger
|
||||
) {
|
||||
}
|
||||
|
||||
|
@ -132,6 +134,8 @@ CODE_SAMPLE
|
|||
// Copy over attributes of the "old" property
|
||||
$param->attrGroups = $property->attrGroups;
|
||||
$this->processNullableType($property, $param);
|
||||
|
||||
$this->phpDocTypeChanger->copyPropertyDocToParam($property, $param);
|
||||
}
|
||||
|
||||
return $node;
|
||||
|
|
Loading…
Reference in New Issue
Block a user