Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix PropertyTypeFromStrictSetterGetterRector for default null #6231

Merged
merged 2 commits into from
Aug 12, 2024
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,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