Skip to content

Commit 7b0b7a0

Browse files
author
ILJA MANYUTIN
committed
[Php80] Do not widen promoted param with nullable property type on ClassPropertyAssignToConstructorPromotionRector (#9809)
1 parent d8bfe6b commit 7b0b7a0

1 file changed

Lines changed: 14 additions & 1 deletion

File tree

rules/Php80/Rector/Class_/ClassPropertyAssignToConstructorPromotionRector.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -484,7 +484,20 @@ private function shouldUsePropertyTypeForPromotedParam(Property $property, Param
484484
$paramType = $this->staticTypeMapper->mapPhpParserNodePHPStanType($param->type);
485485
$paramTypeWithoutNull = TypeCombinator::removeNull($paramType);
486486

487-
return $this->typeComparator->areTypesEqual($type, $paramTypeWithoutNull);
487+
if (! $this->typeComparator->areTypesEqual($type, $paramTypeWithoutNull)) {
488+
return false;
489+
}
490+
491+
if ($param->default instanceof Expr) {
492+
$paramType = TypeCombinator::union($paramType, $this->getType($param->default));
493+
}
494+
495+
if ($this->typeComparator->isSubtype($paramType, $propertyType)
496+
&& ! $this->typeComparator->areTypesEqual($propertyType, $paramType)) {
497+
return false;
498+
}
499+
500+
return true;
488501
}
489502

490503
private function shouldSkipPropertyAssignedNull(Class_ $class, string $propertyName): bool

0 commit comments

Comments
 (0)