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] Handle fallback from param same type object on ReturnTypeFromReturnNewRector #5039

Merged
merged 11 commits into from
Sep 18, 2023
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

declare(strict_types=1);

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

use Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromReturnNewRector\Source\SomeResponse;

final class FallbackFromParam
{
public function action(SomeResponse $someResponse)
{
if (rand(0, 1)) {
return new SomeResponse();
}

return $someResponse;
}
}

?>
-----
<?php

declare(strict_types=1);

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

use Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromReturnNewRector\Source\SomeResponse;

final class FallbackFromParam
{
public function action(SomeResponse $someResponse): \Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromReturnNewRector\Source\SomeResponse
{
if (rand(0, 1)) {
return new SomeResponse();
}

return $someResponse;
}
}

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

declare(strict_types=1);

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

final class FallbackFromParamSelf
{
public function action(self $obj)
{
if (rand(0, 1)) {
return new FallbackFromParamSelf();
}

return $obj;
}
}

?>
-----
<?php

declare(strict_types=1);

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

final class FallbackFromParamSelf
{
public function action(self $obj): \Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromReturnNewRector\Fixture\FallbackFromParamSelf
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we need another test for a static and/or parent typed param?

Copy link
Member Author

Choose a reason for hiding this comment

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

static already error on the first place :)

Copy link
Member Author

Choose a reason for hiding this comment

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

multiple return already skipped at

$returnType = $this->returnTypeInferer->inferFunctionLike($node);
if ($returnType instanceof UnionType) {

{
if (rand(0, 1)) {
return new FallbackFromParamSelf();
}

return $obj;
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Rector\TypeDeclaration\Rector\ClassMethod;

use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\ArrowFunction;
use PhpParser\Node\Expr\Closure;
use PhpParser\Node\Expr\New_;
Expand Down Expand Up @@ -200,7 +201,17 @@ private function resolveReturnNewType(array $returns): ?array
{
$newTypes = [];
foreach ($returns as $return) {
if (! $return->expr instanceof Expr) {
return null;
}

if (! $return->expr instanceof New_) {
$returnType = $this->nodeTypeResolver->getNativeType($return->expr);
if ($returnType instanceof ObjectType) {
$newTypes[] = $returnType;
continue;
}

return null;
}

Expand Down
Loading