Skip to content

Commit

Permalink
[TypeDeclaration] Skip yield return on AddClosureReturnTypeRector (#3227
Browse files Browse the repository at this point in the history
)
  • Loading branch information
samsonasik committed Dec 20, 2022
1 parent bb8dedd commit 25ccfe1
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Rector\Tests\TypeDeclaration\Rector\Closure\AddClosureReturnTypeRector\Fixture;

final class SkipYield
{
public function someFunction(string $type): array
{
return collect($this->types, static function (SomeClass $parameterType, int $index) use ($type) {
if ($parameterType::class === $type) {
yield $index;
}
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

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

$func = function (): iterable {
yield 1;
};

?>
-----
<?php

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

$func = function (): \Iterator {
yield 1;
};

?>
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\Closure;
use PhpParser\Node\Expr\Yield_;
use PhpParser\Node\Expr\YieldFrom;
use PhpParser\Node\FunctionLike;
Expand Down Expand Up @@ -74,11 +75,11 @@ public function provide(): Iterator
*/
public function getNodeTypes(): array
{
return [Function_::class, ClassMethod::class];
return [Function_::class, ClassMethod::class, Closure::class];
}

/**
* @param Function_|ClassMethod $node
* @param Function_|ClassMethod|Closure $node
*/
public function refactor(Node $node): ?Node
{
Expand Down Expand Up @@ -181,7 +182,7 @@ private function resolveYieldedTypes(array $yieldNodes): array
*/
private function resolveYieldType(
array $yieldNodes,
ClassMethod|Function_ $functionLike
ClassMethod|Function_|Closure $functionLike
): FullyQualifiedObjectType|FullyQualifiedGenericObjectType {
$yieldedTypes = $this->resolveYieldedTypes($yieldNodes);

Expand All @@ -195,7 +196,7 @@ private function resolveYieldType(
return new FullyQualifiedGenericObjectType($className, [$yieldedTypes]);
}

private function resolveClassName(Function_|ClassMethod $functionLike): string
private function resolveClassName(Function_|ClassMethod|Closure $functionLike): string
{
$returnTypeNode = $functionLike->getReturnType();

Expand Down
4 changes: 3 additions & 1 deletion rules/TypeDeclaration/TypeInferer/ReturnTypeInferer.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\ArrowFunction;
use PhpParser\Node\Expr\Closure;
use PhpParser\Node\Expr\Yield_;
use PhpParser\Node\FunctionLike;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
Expand Down Expand Up @@ -134,7 +135,8 @@ private function resolveTypeWithVoidHandling(
$functionLike,
static function (Node $subNode): bool {
if (! $subNode instanceof Return_) {
return false;
// yield return is handled on speicific rule: AddReturnTypeDeclarationFromYieldsRector
return $subNode instanceof Yield_;
}

return $subNode->expr instanceof Expr;
Expand Down

0 comments on commit 25ccfe1

Please sign in to comment.