From e09d82c508da47d736599a57f3a02cae48979027 Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Mon, 12 Aug 2024 23:37:39 +0200 Subject: [PATCH] Fix PropertyTypeFromStrictSetterGetterRector for default null (#6231) * add fixture * Fix PropertyTypeFromStrictSetterGetterRector for default null --- .../Fixture/remove_default_null.php.inc | 41 +++++++++++++++++++ ...opertyTypeFromStrictSetterGetterRector.php | 31 ++++++++++++-- .../TypeMapper/StrictMixedTypeMapper.php | 5 ++- 3 files changed, 71 insertions(+), 6 deletions(-) create mode 100644 rules-tests/TypeDeclaration/Rector/Class_/PropertyTypeFromStrictSetterGetterRector/Fixture/remove_default_null.php.inc diff --git a/rules-tests/TypeDeclaration/Rector/Class_/PropertyTypeFromStrictSetterGetterRector/Fixture/remove_default_null.php.inc b/rules-tests/TypeDeclaration/Rector/Class_/PropertyTypeFromStrictSetterGetterRector/Fixture/remove_default_null.php.inc new file mode 100644 index 00000000000..d078d891ee2 --- /dev/null +++ b/rules-tests/TypeDeclaration/Rector/Class_/PropertyTypeFromStrictSetterGetterRector/Fixture/remove_default_null.php.inc @@ -0,0 +1,41 @@ +name = $name; + } + + public function getName(): string + { + return $this->name; + } +} + +?> +----- +name = $name; + } + + public function getName(): string + { + return $this->name; + } +} + +?> diff --git a/rules/TypeDeclaration/Rector/Class_/PropertyTypeFromStrictSetterGetterRector.php b/rules/TypeDeclaration/Rector/Class_/PropertyTypeFromStrictSetterGetterRector.php index 99a1060f5ac..e93215db26a 100644 --- a/rules/TypeDeclaration/Rector/Class_/PropertyTypeFromStrictSetterGetterRector.php +++ b/rules/TypeDeclaration/Rector/Class_/PropertyTypeFromStrictSetterGetterRector.php @@ -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; } @@ -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; @@ -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; } @@ -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'; + } } diff --git a/src/PHPStanStaticTypeMapper/TypeMapper/StrictMixedTypeMapper.php b/src/PHPStanStaticTypeMapper/TypeMapper/StrictMixedTypeMapper.php index fb1d9955047..e20a68e4ab7 100644 --- a/src/PHPStanStaticTypeMapper/TypeMapper/StrictMixedTypeMapper.php +++ b/src/PHPStanStaticTypeMapper/TypeMapper/StrictMixedTypeMapper.php @@ -24,8 +24,9 @@ */ private const MIXED = 'mixed'; - public function __construct(private PhpVersionProvider $phpVersionProvider) - { + public function __construct( + private PhpVersionProvider $phpVersionProvider + ) { } public function getNodeClass(): string