From e31bf222bd30c8d61dcd2d80faef6ff5789e3109 Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Sun, 14 May 2023 16:11:36 +0200 Subject: [PATCH 1/3] add default of null --- .../Fixture/add_default_nullable.php.inc | 28 +++++++++++++++++++ ...ertyAssignToConstructorPromotionRector.php | 19 +++++++++++++ 2 files changed, 47 insertions(+) create mode 100644 rules-tests/Php80/Rector/Class_/ClassPropertyAssignToConstructorPromotionRector/Fixture/add_default_nullable.php.inc diff --git a/rules-tests/Php80/Rector/Class_/ClassPropertyAssignToConstructorPromotionRector/Fixture/add_default_nullable.php.inc b/rules-tests/Php80/Rector/Class_/ClassPropertyAssignToConstructorPromotionRector/Fixture/add_default_nullable.php.inc new file mode 100644 index 00000000000..6855053e594 --- /dev/null +++ b/rules-tests/Php80/Rector/Class_/ClassPropertyAssignToConstructorPromotionRector/Fixture/add_default_nullable.php.inc @@ -0,0 +1,28 @@ +name = $name; + } +} + +?> +----- + diff --git a/rules/Php80/Rector/Class_/ClassPropertyAssignToConstructorPromotionRector.php b/rules/Php80/Rector/Class_/ClassPropertyAssignToConstructorPromotionRector.php index 7eb8a2523b4..c3981d1f551 100644 --- a/rules/Php80/Rector/Class_/ClassPropertyAssignToConstructorPromotionRector.php +++ b/rules/Php80/Rector/Class_/ClassPropertyAssignToConstructorPromotionRector.php @@ -14,6 +14,10 @@ use PhpParser\Node\UnionType; use PHPStan\PhpDocParser\Ast\PhpDoc\ParamTagValueNode; use PHPStan\PhpDocParser\Ast\PhpDoc\VarTagValueNode; +use PHPStan\Type\NullType; +use PHPStan\Type\TypeCombinator; +use PHPStan\Type\TypehintHelper; +use PHPStan\Type\TypeUtils; use Rector\BetterPhpDocParser\PhpDocManipulator\PhpDocTypeChanger; use Rector\BetterPhpDocParser\ValueObject\PhpDocAttributeKey; use Rector\Core\Contract\Rector\AllowEmptyConfigurableRectorInterface; @@ -150,6 +154,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); @@ -188,6 +193,20 @@ private function processNullableType(Property $property, Param $param): void $objectType = $this->getType($property); $param->type = $this->staticTypeMapper->mapPHPStanTypeToPhpParserNode($objectType, TypeKind::PARAM); } + + if ($param->default instanceof Node\Expr) { + if ($this->valueResolver->isNull($param->default)) { + $propertyType = $this->getType($property); + if (! TypeCombinator::containsNull($propertyType)) { + $propertyTypeNode = $this->staticTypeMapper->mapPHPStanTypeToPhpParserNode($propertyType, TypeKind::PARAM); + + dd($propertyTypeNode); + die; + + $param->type = new NullableType($propertyTypeNode); + } + } + } } private function decorateParamWithPropertyPhpDocInfo( From ea45acfc79b323138a606c1f128ed16b8f5e6d16 Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Sun, 14 May 2023 16:16:59 +0200 Subject: [PATCH 2/3] add nullable param type --- ...ertyAssignToConstructorPromotionRector.php | 19 +++---------------- 1 file changed, 3 insertions(+), 16 deletions(-) diff --git a/rules/Php80/Rector/Class_/ClassPropertyAssignToConstructorPromotionRector.php b/rules/Php80/Rector/Class_/ClassPropertyAssignToConstructorPromotionRector.php index c3981d1f551..2de4f75af53 100644 --- a/rules/Php80/Rector/Class_/ClassPropertyAssignToConstructorPromotionRector.php +++ b/rules/Php80/Rector/Class_/ClassPropertyAssignToConstructorPromotionRector.php @@ -14,10 +14,6 @@ use PhpParser\Node\UnionType; use PHPStan\PhpDocParser\Ast\PhpDoc\ParamTagValueNode; use PHPStan\PhpDocParser\Ast\PhpDoc\VarTagValueNode; -use PHPStan\Type\NullType; -use PHPStan\Type\TypeCombinator; -use PHPStan\Type\TypehintHelper; -use PHPStan\Type\TypeUtils; use Rector\BetterPhpDocParser\PhpDocManipulator\PhpDocTypeChanger; use Rector\BetterPhpDocParser\ValueObject\PhpDocAttributeKey; use Rector\Core\Contract\Rector\AllowEmptyConfigurableRectorInterface; @@ -194,18 +190,9 @@ private function processNullableType(Property $property, Param $param): void $param->type = $this->staticTypeMapper->mapPHPStanTypeToPhpParserNode($objectType, TypeKind::PARAM); } - if ($param->default instanceof Node\Expr) { - if ($this->valueResolver->isNull($param->default)) { - $propertyType = $this->getType($property); - if (! TypeCombinator::containsNull($propertyType)) { - $propertyTypeNode = $this->staticTypeMapper->mapPHPStanTypeToPhpParserNode($propertyType, TypeKind::PARAM); - - dd($propertyTypeNode); - die; - - $param->type = new NullableType($propertyTypeNode); - } - } + if ($param->default instanceof Node\Expr && $this->valueResolver->isNull($param->default)) { + $paramType = $this->getType($param); + $param->type = $this->staticTypeMapper->mapPHPStanTypeToPhpParserNode($paramType, TypeKind::PARAM); } } From 3c93489e087a3725442f4c9594e735c5aa58c992 Mon Sep 17 00:00:00 2001 From: GitHub Action Date: Sun, 14 May 2023 14:19:55 +0000 Subject: [PATCH 3/3] [ci-review] Rector Rectify --- .../Class_/ClassPropertyAssignToConstructorPromotionRector.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/rules/Php80/Rector/Class_/ClassPropertyAssignToConstructorPromotionRector.php b/rules/Php80/Rector/Class_/ClassPropertyAssignToConstructorPromotionRector.php index 2de4f75af53..f923d340d7d 100644 --- a/rules/Php80/Rector/Class_/ClassPropertyAssignToConstructorPromotionRector.php +++ b/rules/Php80/Rector/Class_/ClassPropertyAssignToConstructorPromotionRector.php @@ -4,6 +4,7 @@ namespace Rector\Php80\Rector\Class_; +use PhpParser\Node\Expr; use PhpParser\Node; use PhpParser\Node\Identifier; use PhpParser\Node\NullableType; @@ -190,7 +191,7 @@ private function processNullableType(Property $property, Param $param): void $param->type = $this->staticTypeMapper->mapPHPStanTypeToPhpParserNode($objectType, TypeKind::PARAM); } - if ($param->default instanceof Node\Expr && $this->valueResolver->isNull($param->default)) { + if ($param->default instanceof Expr && $this->valueResolver->isNull($param->default)) { $paramType = $this->getType($param); $param->type = $this->staticTypeMapper->mapPHPStanTypeToPhpParserNode($paramType, TypeKind::PARAM); }