Skip to content

Commit

Permalink
[TypeDeclaration] Skip optional yield on AddReturnTypeDeclarationFrom…
Browse files Browse the repository at this point in the history
…YieldsRector (#3228)

* [TypeDeclration] Skip optional yield on AddReturnTypeDeclarationFromYieldsRector

* fixed 🎉

* [ci-review] Rector Rectify

* Fix phpstan

Co-authored-by: GitHub Action <action@github.com>
  • Loading branch information
samsonasik and actions-user committed Dec 20, 2022
1 parent 25ccfe1 commit cf2805b
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Rector\Tests\TypeDeclaration\Rector\FunctionLike\AddReturnTypeDeclarationFromYieldsRector\Fixture;

$func = function () {
if (rand(0, 1)) {
yield 1;
}
};

Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@
use PhpParser\Node\FunctionLike;
use PhpParser\Node\Identifier;
use PhpParser\Node\Name;
use PhpParser\Node\Stmt;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Expression;
use PhpParser\Node\Stmt\Function_;
use PhpParser\NodeTraverser;
use PHPStan\Type\MixedType;
Expand Down Expand Up @@ -131,6 +133,11 @@ private function findCurrentScopeYieldNodes(FunctionLike $functionLike): array
return NodeTraverser::DONT_TRAVERSE_CHILDREN;
}

if ($node instanceof Stmt && ! $node instanceof Expression) {
$yieldNodes = [];
return NodeTraverser::STOP_TRAVERSAL;
}

if (! $node instanceof Yield_ && ! $node instanceof YieldFrom) {
return null;
}
Expand Down

0 comments on commit cf2805b

Please sign in to comment.