Skip to content

Commit

Permalink
[TypeDeclaration] Add return static object support on ReturnTypeFromS…
Browse files Browse the repository at this point in the history
…trictFluentReturnRector (#4916)

* [TypeDeclaration] Add return static object support on ReturnTypeFromStrictFluentReturnRector

* [ci-review] Rector Rectify

* [ci-review] Rector Rectify

* clean up

* trigger CI

---------

Co-authored-by: GitHub Action <[email protected]>
  • Loading branch information
samsonasik and actions-user authored Sep 5, 2023
1 parent 6d009cc commit aa72821
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

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

class ReturnStatic
{
private $foo = 'bar';

public function test()
{
$obj = new static();
$obj->foo = 'foo';

return $obj;
}
}

?>
-----
<?php

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

class ReturnStatic
{
private $foo = 'bar';

public function test(): static
{
$obj = new static();
$obj->foo = 'foo';

return $obj;
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictFluentReturnRector\FixturePhp74;

class ReturnStatic
{
private $foo = 'bar';

public function test()
{
$obj = new static();
$obj->foo = 'foo';

return $obj;
}
}

?>
-----
<?php

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictFluentReturnRector\FixturePhp74;

class ReturnStatic
{
private $foo = 'bar';

public function test(): self
{
$obj = new static();
$obj->foo = 'foo';

return $obj;
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Type\ObjectType;
use PHPStan\Type\StaticType;
use PHPStan\Type\ThisType;
use Rector\Core\Php\PhpVersionProvider;
use Rector\Core\Rector\AbstractScopeAwareRector;
Expand Down Expand Up @@ -93,6 +94,11 @@ public function refactorWithScope(Node $node, Scope $scope): ?Node
}

$returnType = $this->returnTypeInferer->inferFunctionLike($node);

if ($returnType instanceof StaticType && $returnType->getStaticObjectType()->getClassName() === $classReflection->getName()) {
return $this->processAddReturnSelfOrStatic($node, $classReflection);
}

if ($returnType instanceof ObjectType && $returnType->getClassName() === $classReflection->getName()) {
$node->returnType = new Name('self');
return $node;
Expand All @@ -102,14 +108,30 @@ public function refactorWithScope(Node $node, Scope $scope): ?Node
return null;
}

if ($classReflection->isAnonymous()
|| $classReflection->isFinalByKeyword()
|| ! $this->phpVersionProvider->isAtLeastPhpVersion(PhpVersionFeature::STATIC_RETURN_TYPE)) {
$node->returnType = new Name('self');
} else {
$node->returnType = new Name('static');
return $this->processAddReturnSelfOrStatic($node, $classReflection);
}

private function processAddReturnSelfOrStatic(
ClassMethod $classMethod,
ClassReflection $classReflection
): ClassMethod {
$classMethod->returnType = $this->shouldSelf($classReflection)
? new Name('self')
: new Name('static');

return $classMethod;
}

private function shouldSelf(ClassReflection $classReflection): bool
{
if ($classReflection->isAnonymous()) {
return true;
}

if ($classReflection->isFinalByKeyword()) {
return true;
}

return $node;
return ! $this->phpVersionProvider->isAtLeastPhpVersion(PhpVersionFeature::STATIC_RETURN_TYPE);
}
}

0 comments on commit aa72821

Please sign in to comment.