Skip to content

Commit

Permalink
Fix nullable param in ClassPropertyAssignToConstructorPromotionRector (
Browse files Browse the repository at this point in the history
…#3842)

Co-authored-by: GitHub Action <actions@github.com>
  • Loading branch information
TomasVotruba and actions-user committed May 14, 2023
1 parent 25d0163 commit f554fe1
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php declare(strict_types = 1);

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

final class AddDefaultNullable
{
public $name;

public function __construct(string $name = null)
{
$this->name = $name;
}
}

?>
-----
<?php declare(strict_types = 1);

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

final class AddDefaultNullable
{
public function __construct(public ?string $name = null)
{
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Rector\Php80\Rector\Class_;

use PhpParser\Node\Expr;
use PhpParser\Node;
use PhpParser\Node\Identifier;
use PhpParser\Node\NullableType;
Expand Down Expand Up @@ -150,6 +151,7 @@ public function refactor(Node $node): ?Node

// rename also following calls
$propertyName = $this->getName($property->props[0]);

/** @var string $oldName */
$oldName = $this->getName($param->var);
$this->variableRenamer->renameVariableInFunctionLike($constructClassMethod, $oldName, $propertyName, null);
Expand Down Expand Up @@ -188,6 +190,11 @@ private function processNullableType(Property $property, Param $param): void
$objectType = $this->getType($property);
$param->type = $this->staticTypeMapper->mapPHPStanTypeToPhpParserNode($objectType, TypeKind::PARAM);
}

if ($param->default instanceof Expr && $this->valueResolver->isNull($param->default)) {
$paramType = $this->getType($param);
$param->type = $this->staticTypeMapper->mapPHPStanTypeToPhpParserNode($paramType, TypeKind::PARAM);
}
}

private function decorateParamWithPropertyPhpDocInfo(
Expand Down

0 comments on commit f554fe1

Please sign in to comment.