Skip to content

Commit

Permalink
Fix PropertyTypeFromStrictSetterGetterRector for default null (#6231)
Browse files Browse the repository at this point in the history
* add fixture

* Fix PropertyTypeFromStrictSetterGetterRector for default null
  • Loading branch information
TomasVotruba authored Aug 12, 2024
1 parent e4ceb29 commit e09d82c
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace Rector\Tests\TypeDeclaration\Rector\Class_\PropertyTypeFromStrictSetterGetterRector\Fixture;

final class RemoveDefaultNull
{
private $name = null;

public function setName(string $name): void
{
$this->name = $name;
}

public function getName(): string
{
return $this->name;
}
}

?>
-----
<?php

namespace Rector\Tests\TypeDeclaration\Rector\Class_\PropertyTypeFromStrictSetterGetterRector\Fixture;

final class RemoveDefaultNull
{
private string $name;

public function setName(string $name): void
{
$this->name = $name;
}

public function getName(): string
{
return $this->name;
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,12 @@ public function refactor(Node $node): ?Node
continue;
}

if (! $this->isDefaultExprTypeCompatible($property, $getterSetterPropertyType)) {
$hasPropertyDefaultNull = $this->hasPropertyDefaultNull($property);

if (! $hasPropertyDefaultNull && ! $this->isDefaultExprTypeCompatible(
$property,
$getterSetterPropertyType
)) {
continue;
}

Expand All @@ -136,7 +141,7 @@ public function refactor(Node $node): ?Node
continue;
}

$this->decorateDefaultExpr($getterSetterPropertyType, $property);
$this->decorateDefaultExpr($getterSetterPropertyType, $property, $hasPropertyDefaultNull);

$property->type = $propertyTypeDeclaration;
$hasChanged = true;
Expand Down Expand Up @@ -194,9 +199,17 @@ private function isDefaultExprTypeCompatible(Property $property, Type $getterSet
return $defaultExprType->equals($getterSetterPropertyType);
}

private function decorateDefaultExpr(Type $getterSetterPropertyType, Property $property): void
{
private function decorateDefaultExpr(
Type $getterSetterPropertyType,
Property $property,
bool $hasPropertyDefaultNull
): void {
if (! TypeCombinator::containsNull($getterSetterPropertyType)) {
if ($hasPropertyDefaultNull) {
// reset to nothign
$property->props[0]->default = null;
}

return;
}

Expand All @@ -209,4 +222,14 @@ private function decorateDefaultExpr(Type $getterSetterPropertyType, Property $p

$propertyProperty->default = new ConstFetch(new Name('null'));
}

private function hasPropertyDefaultNull(Property $property): bool
{
$defaultExpr = $property->props[0]->default ?? null;
if (! $defaultExpr instanceof ConstFetch) {
return false;
}

return $defaultExpr->name->toLowerString() === 'null';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@
*/
private const MIXED = 'mixed';

public function __construct(private PhpVersionProvider $phpVersionProvider)
{
public function __construct(
private PhpVersionProvider $phpVersionProvider
) {
}

public function getNodeClass(): string
Expand Down

0 comments on commit e09d82c

Please sign in to comment.