Skip to content

Commit

Permalink
[TypeDeclaration] Add nullable property type support to PropertyTypeF…
Browse files Browse the repository at this point in the history
…romStrictSetterGetterRector (#3877)
  • Loading branch information
TomasVotruba committed May 17, 2023
1 parent 16f3c96 commit f2348ae
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 7 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 NullableGetterSetter
{
private $name;

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 NullableGetterSetter
{
private ?string $name = null;

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 @@ -5,9 +5,14 @@
namespace Rector\TypeDeclaration\Rector\Class_;

use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\ConstFetch;
use PhpParser\Node\Name;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\Property;
use PHPStan\Type\Type;
use PHPStan\Type\TypeCombinator;
use PHPStan\Type\UnionType;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\ValueObject\PhpVersionFeature;
use Rector\Php74\Guard\MakePropertyTypedGuard;
Expand Down Expand Up @@ -109,15 +114,17 @@ public function refactor(Node $node): ?Node
continue;
}

$propertyTypeDclaration = $this->staticTypeMapper->mapPHPStanTypeToPhpParserNode(
$propertyTypeDeclaration = $this->staticTypeMapper->mapPHPStanTypeToPhpParserNode(
$getterSetterPropertyType,
TypeKind::PROPERTY
);
if (! $propertyTypeDclaration instanceof Node) {
if (! $propertyTypeDeclaration instanceof Node) {
continue;
}

$property->type = $propertyTypeDclaration;
$this->decorateDefaultExpr($getterSetterPropertyType, $property);

$property->type = $propertyTypeDeclaration;
$hasChanged = true;
}

Expand All @@ -141,16 +148,22 @@ private function matchGetterSetterIdenticalType(Property $property, Class_ $clas
}

$setterBasedStrictType = $this->setterTypeDeclarationPropertyTypeInferer->inferProperty($property, $class);

if (! $setterBasedStrictType instanceof Type) {
return null;
}

if (! $getterBasedStrictType->equals($setterBasedStrictType)) {
return null;
// single type
if ($setterBasedStrictType->equals($getterBasedStrictType)) {
return $setterBasedStrictType;
}

return $getterBasedStrictType;
if ($getterBasedStrictType instanceof UnionType) {
$getterBasedStrictTypes = $getterBasedStrictType->getTypes();
} else {
$getterBasedStrictTypes = [$getterBasedStrictType];
}

return new UnionType([$setterBasedStrictType, ...$getterBasedStrictTypes]);
}

private function isDefaultExprTypeCompatible(Property $property, Type $getterSetterPropertyType): bool
Expand All @@ -166,4 +179,20 @@ private function isDefaultExprTypeCompatible(Property $property, Type $getterSet
$defaultExprType = $this->staticTypeMapper->mapPhpParserNodePHPStanType($defaultExpr);
return $defaultExprType->equals($getterSetterPropertyType);
}

private function decorateDefaultExpr(Type $getterSetterPropertyType, Property $property): void
{
if (! TypeCombinator::containsNull($getterSetterPropertyType)) {
return;
}

$propertyProperty = $property->props[0];

// already set → skip it
if ($propertyProperty->default instanceof Expr) {
return;
}

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

0 comments on commit f2348ae

Please sign in to comment.