Skip to content

Commit

Permalink
[TypeDeclaration] Handle initialized on getter on ReturnTypeFromStric…
Browse files Browse the repository at this point in the history
…tTypedPropertyRector (#5784)

* Added failling test

* update fixture

* Fixes #5775

---------

Co-authored-by: Markus Staab <[email protected]>
  • Loading branch information
samsonasik and staabm authored Mar 31, 2024
1 parent 93e0259 commit 4c40eb6
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictTypedPropertyRector\Fixture;

use stdClass;

final class InitializedInGetter
{
private ?stdClass $demo = null;

public function getDemo() {
if ($this->demo === null) {
$this->demo = new stdClass();
}
return $this->demo;
}
}

?>
-----
<?php

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictTypedPropertyRector\Fixture;

use stdClass;

final class InitializedInGetter
{
private ?stdClass $demo = null;

public function getDemo(): \stdClass {
if ($this->demo === null) {
$this->demo = new stdClass();
}
return $this->demo;
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ private function resolveReturnPropertyType(ClassMethod $classMethod): array
return [];
}

$propertyTypes[] = $phpPropertyReflection->getNativeType();
$propertyTypes[] = $this->nodeTypeResolver->getNativeType($return->expr);
}

return $propertyTypes;
Expand Down

0 comments on commit 4c40eb6

Please sign in to comment.