Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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