Param doctype not copied to property in DowngradePropertyPromotionRector (#734)

* Param doctype not copied to property in DowngradePropertyPromotionRector

* Fix fixture by adding strict types

* Add param type to downgraded property

* Update rules/DowngradePhp80/Rector/Class_/DowngradePropertyPromotionRector.php

Change how constructor is found

Co-authored-by: Abdul Malik Ikhsan <samsonasik@gmail.com>

Co-authored-by: Abdul Malik Ikhsan <samsonasik@gmail.com>
This commit is contained in:
Brandon Olivares 2021-08-22 16:52:44 -04:00 committed by GitHub
parent af1cbb92fd
commit 138aa5ea68
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 70 additions and 1 deletions

View File

@ -0,0 +1,41 @@
<?php
declare(strict_types=1);
namespace Rector\Tests\DowngradePhp80\Rector\Class_\DowngradePropertyPromotionRector\Fixture;
final class UseParamType
{
/**
* @param array<string, int> $values
*/
public function __construct(
private array $values
) {
}
}
?>
-----
<?php
declare(strict_types=1);
namespace Rector\Tests\DowngradePhp80\Rector\Class_\DowngradePropertyPromotionRector\Fixture;
final class UseParamType
{
/**
* @var array<string, int>
*/
private array $values;
/**
* @param array<string, int> $values
*/
public function __construct(array $values)
{
$this->values = $values;
}
}
?>

View File

@ -11,6 +11,8 @@ use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Expression;
use PhpParser\Node\Stmt\Property;
use PHPStan\Type\MixedType;
use Rector\BetterPhpDocParser\PhpDocManipulator\PhpDocTypeChanger;
use Rector\Core\NodeManipulator\ClassInsertManipulator;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\ValueObject\MethodName;
@ -26,7 +28,8 @@ use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
final class DowngradePropertyPromotionRector extends AbstractRector
{
public function __construct(
private ClassInsertManipulator $classInsertManipulator
private ClassInsertManipulator $classInsertManipulator,
private PhpDocTypeChanger $phpDocTypeChanger
) {
}
@ -201,6 +204,7 @@ CODE_SAMPLE
$property = $this->nodeFactory->createProperty($name);
$property->flags = $param->flags;
$property->type = $param->type;
$this->decoratePropertyWithParamDocInfo($param, $property);
if ($param->default !== null) {
$property->props[0]->default = $param->default;
@ -211,4 +215,28 @@ CODE_SAMPLE
return $properties;
}
private function decoratePropertyWithParamDocInfo(Param $param, Property $property): void
{
$constructor = $param->getAttribute(AttributeKey::METHOD_NODE);
$phpDocInfo = $this->phpDocInfoFactory->createFromNode($constructor);
if ($phpDocInfo === null) {
return;
}
$name = $this->getName($param->var);
if ($name === null) {
return;
}
$type = $phpDocInfo->getParamType($name);
// MixedType likely means there was no param type defined
if ($type instanceof MixedType) {
return;
}
$propertyDocInfo = $this->phpDocInfoFactory->createEmpty($property);
$this->phpDocTypeChanger->changeVarType($propertyDocInfo, $type);
}
}