Skip to content

Commit

Permalink
Param doctype not copied to property in DowngradePropertyPromotionRec…
Browse files Browse the repository at this point in the history
…tor (#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>
  • Loading branch information
devbanana and samsonasik committed Aug 22, 2021
1 parent af1cbb9 commit 138aa5e
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -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;
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
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;
Expand All @@ -26,7 +28,8 @@
final class DowngradePropertyPromotionRector extends AbstractRector
{
public function __construct(
private ClassInsertManipulator $classInsertManipulator
private ClassInsertManipulator $classInsertManipulator,
private PhpDocTypeChanger $phpDocTypeChanger
) {
}

Expand Down Expand Up @@ -201,6 +204,7 @@ private function createPropertiesFromParams(array $params): array
$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;
Expand All @@ -211,4 +215,28 @@ private function createPropertiesFromParams(array $params): array

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

0 comments on commit 138aa5e

Please sign in to comment.