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 inner Closure on ReturnTypeFromReturnNewRector #1467

Merged
merged 5 commits into from
Dec 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

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

final class InnerClosure
{
public function __construct()
{
$a = function () {
return new \stdClass;
};
}
}

?>
-----
<?php

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

final class InnerClosure
{
public function __construct()
{
$a = function (): \stdClass {
return new \stdClass;
};
}
}

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

use PhpParser\Node;
use PhpParser\Node\Expr\ArrowFunction;
use PhpParser\Node\Expr\Closure;
use PhpParser\Node\Expr\New_;
use PhpParser\Node\Name;
use PhpParser\Node\Stmt\ClassMethod;
Expand Down Expand Up @@ -71,7 +72,7 @@ public function action(): Response
*/
public function getNodeTypes(): array
{
return [ClassMethod::class, Function_::class, ArrowFunction::class];
return [ClassMethod::class, Function_::class, Closure::class, ArrowFunction::class];
}

/**
Expand All @@ -87,7 +88,7 @@ public function refactor(Node $node): ?Node
$returns = [new Return_($node->expr)];
} else {
/** @var Return_[] $returns */
$returns = $this->betterNodeFinder->findInstanceOf((array) $node->stmts, Return_::class);
$returns = $this->betterNodeFinder->findInstancesOfInFunctionLikeScoped($node, Return_::class);
}

if ($returns === []) {
Expand Down
45 changes: 45 additions & 0 deletions src/PhpParser/Node/BetterNodeFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,51 @@ public function hasInstancesOfInFunctionLikeScoped(
return false;
}

/**
* @template T of Node
* @param array<class-string<T>>|class-string<T> $types
* @return T[]
*/
public function findInstancesOfInFunctionLikeScoped(
ClassMethod | Function_ | Closure $functionLike,
string|array $types
): array {
if (is_string($types)) {
$types = [$types];
}

/** @var T[] $foundNodes */
$foundNodes = [];

foreach ($types as $type) {
/** @var T[] $nodes */
$nodes = $this->findInstanceOf((array) $functionLike->stmts, $type);

if ($nodes === []) {
continue;
}

foreach ($nodes as $key => $node) {
$parentFunctionLike = $this->findParentByTypes(
$node,
[ClassMethod::class, Function_::class, Closure::class]
);

if ($parentFunctionLike !== $functionLike) {
unset($nodes[$key]);
}
}

if ($nodes === []) {
continue;
}

$foundNodes = array_merge($foundNodes, $nodes);
}

return $foundNodes;
}

public function findFirstInFunctionLikeScoped(
ClassMethod | Function_ | Closure $functionLike,
callable $filter
Expand Down