mirror of
https://github.com/rectorphp/rector.git
synced 2024-06-28 13:43:31 +00:00
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:
parent
af1cbb92fd
commit
138aa5ea68
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
?>
|
|
@ -11,6 +11,8 @@ use PhpParser\Node\Stmt\Class_;
|
||||||
use PhpParser\Node\Stmt\ClassMethod;
|
use PhpParser\Node\Stmt\ClassMethod;
|
||||||
use PhpParser\Node\Stmt\Expression;
|
use PhpParser\Node\Stmt\Expression;
|
||||||
use PhpParser\Node\Stmt\Property;
|
use PhpParser\Node\Stmt\Property;
|
||||||
|
use PHPStan\Type\MixedType;
|
||||||
|
use Rector\BetterPhpDocParser\PhpDocManipulator\PhpDocTypeChanger;
|
||||||
use Rector\Core\NodeManipulator\ClassInsertManipulator;
|
use Rector\Core\NodeManipulator\ClassInsertManipulator;
|
||||||
use Rector\Core\Rector\AbstractRector;
|
use Rector\Core\Rector\AbstractRector;
|
||||||
use Rector\Core\ValueObject\MethodName;
|
use Rector\Core\ValueObject\MethodName;
|
||||||
|
@ -26,7 +28,8 @@ use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
||||||
final class DowngradePropertyPromotionRector extends AbstractRector
|
final class DowngradePropertyPromotionRector extends AbstractRector
|
||||||
{
|
{
|
||||||
public function __construct(
|
public function __construct(
|
||||||
private ClassInsertManipulator $classInsertManipulator
|
private ClassInsertManipulator $classInsertManipulator,
|
||||||
|
private PhpDocTypeChanger $phpDocTypeChanger
|
||||||
) {
|
) {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -201,6 +204,7 @@ CODE_SAMPLE
|
||||||
$property = $this->nodeFactory->createProperty($name);
|
$property = $this->nodeFactory->createProperty($name);
|
||||||
$property->flags = $param->flags;
|
$property->flags = $param->flags;
|
||||||
$property->type = $param->type;
|
$property->type = $param->type;
|
||||||
|
$this->decoratePropertyWithParamDocInfo($param, $property);
|
||||||
|
|
||||||
if ($param->default !== null) {
|
if ($param->default !== null) {
|
||||||
$property->props[0]->default = $param->default;
|
$property->props[0]->default = $param->default;
|
||||||
|
@ -211,4 +215,28 @@ CODE_SAMPLE
|
||||||
|
|
||||||
return $properties;
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user