Skip to content

Commit

Permalink
[TypeDeclaration] Handle inner function Yield and Return_ on ReturnTy…
Browse files Browse the repository at this point in the history
…peFromStrictReturnExprRector (#2566)

* [TypeDeclaration] Handle inner function Yield and Return_ on ReturnTypeFromStrictReturnExprRector

* implemented 🎉
  • Loading branch information
samsonasik authored Jun 25, 2022
1 parent 6845975 commit c6fe564
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

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

final class VoidReturnInInnerFunction
{
public function run()
{
return $this->first() && true;
}

public function first()
{
function () {
return;
};

return true;
}
}

?>
-----
<?php

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

final class VoidReturnInInnerFunction
{
public function run(): bool
{
return $this->first() && true;
}

public function first(): bool
{
function () {
return;
};

return true;
}
}

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

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

final class YieldParentInnerFunction
{
public function run()
{
return $this->first() && true;
}

public function first()
{
function () {
yield 1;
};

return true;
}
}

?>
-----
<?php

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

final class YieldParentInnerFunction
{
public function run(): bool
{
return $this->first() && true;
}

public function first(): bool
{
function () {
yield 1;
};

return true;
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,11 @@ private function hasSingleStrictReturn(ClassMethod $classMethod): bool
return false;
}

if ($this->betterNodeFinder->hasInstancesOf($classMethod->stmts, [Yield_::class])) {
if ($this->betterNodeFinder->hasInstancesOfInFunctionLikeScoped($classMethod, [Yield_::class])) {
return false;
}

$returns = $this->betterNodeFinder->findInstanceOf($classMethod->stmts, Return_::class);
$returns = $this->betterNodeFinder->findInstancesOfInFunctionLikeScoped($classMethod, Return_::class);
if (count($returns) !== 1) {
return false;
}
Expand Down

0 comments on commit c6fe564

Please sign in to comment.