Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TypeDeclaration] Add return static object support on ReturnTypeFromStrictFluentReturnRector #4916

Merged
merged 5 commits into from
Sep 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we could additionally add static phpdoc type on php7?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That seems can be an improvement next 👍 with check not support static, but not final class and not anonymous class.

? 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);
}
}