From 514dc47589be9362d734e04c05750bc89c652dd2 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Thu, 31 Aug 2023 16:55:37 +0700 Subject: [PATCH 1/3] [TypeDeclaration] Do not add default value when assigned in __construct() on TypedPropertyFromStrictGetterMethodReturnTypeRector --- .../Fixture/assigned_in_construct.php.inc | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 rules-tests/TypeDeclaration/Rector/Property/TypedPropertyFromStrictGetterMethodReturnTypeRector/Fixture/assigned_in_construct.php.inc diff --git a/rules-tests/TypeDeclaration/Rector/Property/TypedPropertyFromStrictGetterMethodReturnTypeRector/Fixture/assigned_in_construct.php.inc b/rules-tests/TypeDeclaration/Rector/Property/TypedPropertyFromStrictGetterMethodReturnTypeRector/Fixture/assigned_in_construct.php.inc new file mode 100644 index 00000000000..3aedaa32930 --- /dev/null +++ b/rules-tests/TypeDeclaration/Rector/Property/TypedPropertyFromStrictGetterMethodReturnTypeRector/Fixture/assigned_in_construct.php.inc @@ -0,0 +1,45 @@ +name = $name; + } + + public function getName(): string + { + return $this->name; + } +} + +?> +----- +name = $name; + } + + public function getName(): string + { + return $this->name; + } +} + +?> From 4679586e2baef09f98f09c85640b661f34cff77e Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Thu, 31 Aug 2023 17:02:13 +0700 Subject: [PATCH 2/3] Fixed :tada: --- ...FromStrictGetterMethodReturnTypeRector.php | 23 +++++++++++++++---- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/rules/TypeDeclaration/Rector/Property/TypedPropertyFromStrictGetterMethodReturnTypeRector.php b/rules/TypeDeclaration/Rector/Property/TypedPropertyFromStrictGetterMethodReturnTypeRector.php index 14cbb0dc1c8..c79c61847eb 100644 --- a/rules/TypeDeclaration/Rector/Property/TypedPropertyFromStrictGetterMethodReturnTypeRector.php +++ b/rules/TypeDeclaration/Rector/Property/TypedPropertyFromStrictGetterMethodReturnTypeRector.php @@ -4,9 +4,9 @@ namespace Rector\TypeDeclaration\Rector\Property; -use PhpParser\Node\Scalar\String_; use PhpParser\Node; use PhpParser\Node\Expr; +use PhpParser\Node\Scalar\String_; use PhpParser\Node\Stmt\Class_; use PhpParser\Node\Stmt\Property; use PHPStan\Type\MixedType; @@ -20,6 +20,7 @@ use Rector\DeadCode\PhpDoc\TagRemover\VarTagRemover; use Rector\PHPStanStaticTypeMapper\Enum\TypeKind; use Rector\Privatization\Guard\ParentPropertyLookupGuard; +use Rector\TypeDeclaration\AlreadyAssignDetector\ConstructorAssignDetector; use Rector\TypeDeclaration\TypeInferer\PropertyTypeInferer\GetterTypeDeclarationPropertyTypeInferer; use Rector\VersionBonding\Contract\MinPhpVersionInterface; use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; @@ -34,7 +35,8 @@ public function __construct( private readonly GetterTypeDeclarationPropertyTypeInferer $getterTypeDeclarationPropertyTypeInferer, private readonly VarTagRemover $varTagRemover, private readonly ParentPropertyLookupGuard $parentPropertyLookupGuard, - private readonly ReflectionResolver $reflectionResolver + private readonly ReflectionResolver $reflectionResolver, + private readonly ConstructorAssignDetector $constructorAssignDetector ) { } @@ -100,8 +102,15 @@ public function refactor(Node $node): null|Class_ continue; } + $isAssignedInConstructor = $this->constructorAssignDetector->isPropertyAssigned( + $node, + (string) $this->getName($property) + ); + // if property is public, it should be nullable - if ($property->isPublic() && ! TypeCombinator::containsNull($getterReturnType)) { + if ($property->isPublic() && ! TypeCombinator::containsNull( + $getterReturnType + ) && ! $isAssignedInConstructor) { $getterReturnType = TypeCombinator::addNull($getterReturnType); } @@ -119,7 +128,7 @@ public function refactor(Node $node): null|Class_ } $property->type = $propertyTypeNode; - $this->decorateDefaultExpr($getterReturnType, $property); + $this->decorateDefaultExpr($getterReturnType, $property, $isAssignedInConstructor); $this->refactorPhpDoc($property); @@ -138,8 +147,12 @@ public function provideMinPhpVersion(): int return PhpVersionFeature::TYPED_PROPERTIES; } - private function decorateDefaultExpr(Type $propertyType, Property $property): void + private function decorateDefaultExpr(Type $propertyType, Property $property, bool $isAssignedInConstructor): void { + if ($isAssignedInConstructor) { + return; + } + $propertyProperty = $property->props[0]; // already has a default value From a1f6a0d13e3730dcec09bb8c98eb5c3522f307ee Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Thu, 31 Aug 2023 17:04:23 +0700 Subject: [PATCH 3/3] Fixed :tada: --- .../TypedPropertyFromStrictGetterMethodReturnTypeRector.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rules/TypeDeclaration/Rector/Property/TypedPropertyFromStrictGetterMethodReturnTypeRector.php b/rules/TypeDeclaration/Rector/Property/TypedPropertyFromStrictGetterMethodReturnTypeRector.php index c79c61847eb..46dd5a5d2ef 100644 --- a/rules/TypeDeclaration/Rector/Property/TypedPropertyFromStrictGetterMethodReturnTypeRector.php +++ b/rules/TypeDeclaration/Rector/Property/TypedPropertyFromStrictGetterMethodReturnTypeRector.php @@ -104,7 +104,7 @@ public function refactor(Node $node): null|Class_ $isAssignedInConstructor = $this->constructorAssignDetector->isPropertyAssigned( $node, - (string) $this->getName($property) + $this->getName($property) ); // if property is public, it should be nullable