From 4c40eb60ca984574eea3fa75e8ac59e9c8e07e5e Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Mon, 1 Apr 2024 02:05:13 +0700 Subject: [PATCH] [TypeDeclaration] Handle initialized on getter on ReturnTypeFromStrictTypedPropertyRector (#5784) * Added failling test * update fixture * Fixes #5775 --------- Co-authored-by: Markus Staab --- .../Fixture/initialized_in_getter.php.inc | 39 +++++++++++++++++++ ...eturnTypeFromStrictTypedPropertyRector.php | 2 +- 2 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 rules-tests/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictTypedPropertyRector/Fixture/initialized_in_getter.php.inc diff --git a/rules-tests/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictTypedPropertyRector/Fixture/initialized_in_getter.php.inc b/rules-tests/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictTypedPropertyRector/Fixture/initialized_in_getter.php.inc new file mode 100644 index 00000000000..f534528d2ea --- /dev/null +++ b/rules-tests/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictTypedPropertyRector/Fixture/initialized_in_getter.php.inc @@ -0,0 +1,39 @@ +demo === null) { + $this->demo = new stdClass(); + } + return $this->demo; + } +} + +?> +----- +demo === null) { + $this->demo = new stdClass(); + } + return $this->demo; + } +} + +?> diff --git a/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictTypedPropertyRector.php b/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictTypedPropertyRector.php index b0a5f8e657a..197a78cef10 100644 --- a/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictTypedPropertyRector.php +++ b/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictTypedPropertyRector.php @@ -150,7 +150,7 @@ private function resolveReturnPropertyType(ClassMethod $classMethod): array return []; } - $propertyTypes[] = $phpPropertyReflection->getNativeType(); + $propertyTypes[] = $this->nodeTypeResolver->getNativeType($return->expr); } return $propertyTypes;