Skip to content

Commit

Permalink
[TypeDeclaration] Skip return after return closure on AddVoidReturnTy…
Browse files Browse the repository at this point in the history
…peWhereNoReturnRector (#4930)

* [TypeDeclaration] Skip return after return closure on AddVoidReturnTypeWhereNoReturnRector

* fixed 🎉
  • Loading branch information
samsonasik committed Sep 7, 2023
1 parent e47f631 commit 54799fc
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

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

final class SkipReturnAfterClosureReturn
{
private function run($someObject)
{
$someData = $someObject->run('foo', function (): array {
return [];
}, strtotime('+6 hours'));

return $someData[$this->get()];
}
}
13 changes: 8 additions & 5 deletions rules/TypeDeclaration/TypeInferer/SilentVoidResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,14 @@ public function hasExclusiveVoid(ClassMethod | Closure | Function_ $functionLike
return false;
}

$return = $this->betterNodeFinder->findFirstInFunctionLikeScoped(
$functionLike,
static fn (Node $node): bool => $node instanceof Return_ && $node->expr instanceof Expr
);
return ! $return instanceof Return_;
$returns = $this->betterNodeFinder->findInstancesOfInFunctionLikeScoped($functionLike, Return_::class);
foreach ($returns as $return) {
if ($return->expr instanceof Expr) {
return false;
}
}

return true;
}

public function hasSilentVoid(FunctionLike $functionLike): bool
Expand Down

0 comments on commit 54799fc

Please sign in to comment.