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

[PhpParser] Use general FunctionLike instanceof check for inner scope check on BetterNodeFinder #6334

Merged
merged 1 commit into from
Sep 29, 2024
Merged
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
16 changes: 8 additions & 8 deletions src/PhpParser/Node/BetterNodeFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
namespace Rector\PhpParser\Node;

use PhpParser\Node;
use PhpParser\Node\Expr\ArrowFunction;
use PhpParser\Node\Expr\Closure;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Expr\Yield_;
use PhpParser\Node\Expr\YieldFrom;
use PhpParser\Node\FunctionLike;
use PhpParser\Node\Stmt;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
Expand Down Expand Up @@ -171,7 +171,7 @@ public function hasInstancesOfInFunctionLikeScoped(
$this->simpleCallableNodeTraverser->traverseNodesWithCallable(
(array) $functionLike->stmts,
static function (Node $subNode) use ($types, &$isFoundNode): ?int {
if ($subNode instanceof Class_ || $subNode instanceof Function_ || $subNode instanceof Closure || $subNode instanceof ArrowFunction) {
if ($subNode instanceof Class_ || $subNode instanceof FunctionLike) {
return NodeTraverser::DONT_TRAVERSE_CURRENT_AND_CHILDREN;
}

Expand Down Expand Up @@ -199,7 +199,7 @@ public function findReturnsScoped(ClassMethod | Function_ | Closure $functionLik
$this->simpleCallableNodeTraverser->traverseNodesWithCallable(
(array) $functionLike->stmts,
function (Node $subNode) use (&$returns): ?int {
if ($subNode instanceof Class_ || $subNode instanceof Function_ || $subNode instanceof Closure) {
if ($subNode instanceof Class_ || $subNode instanceof FunctionLike) {
return NodeTraverser::DONT_TRAVERSE_CURRENT_AND_CHILDREN;
}

Expand Down Expand Up @@ -233,8 +233,8 @@ public function findInstancesOfScoped(array $nodes, string|array $types): array
{
// here verify only pass single nodes as FunctionLike
if (count($nodes) === 1
&& ($nodes[0] instanceof ClassMethod || $nodes[0] instanceof Function_ || $nodes[0] instanceof Closure)) {
$nodes = (array) $nodes[0]->stmts;
&& ($nodes[0] instanceof FunctionLike)) {
$nodes = (array) $nodes[0]->getStmts();
}

if (is_string($types)) {
Expand All @@ -247,7 +247,7 @@ public function findInstancesOfScoped(array $nodes, string|array $types): array
$this->simpleCallableNodeTraverser->traverseNodesWithCallable(
$nodes,
static function (Node $subNode) use ($types, &$foundNodes): ?int {
if ($subNode instanceof Class_ || $subNode instanceof Function_ || $subNode instanceof Closure || $subNode instanceof ArrowFunction) {
if ($subNode instanceof Class_ || $subNode instanceof FunctionLike) {
return NodeTraverser::DONT_TRAVERSE_CURRENT_AND_CHILDREN;
}

Expand Down Expand Up @@ -293,15 +293,15 @@ public function findFirstInFunctionLikeScoped(
return null;
}

if (! $this->hasInstancesOf($functionLike->stmts, [Class_::class, Function_::class, Closure::class])) {
if (! $this->hasInstancesOf($functionLike->stmts, [Class_::class, FunctionLike::class])) {
return $foundNode;
}

$scopedNode = null;
$this->simpleCallableNodeTraverser->traverseNodesWithCallable(
$functionLike->stmts,
function (Node $subNode) use (&$scopedNode, $foundNode, $filter): ?int {
if ($subNode instanceof Class_ || $subNode instanceof Function_ || $subNode instanceof Closure) {
if ($subNode instanceof Class_ || $subNode instanceof FunctionLike) {
if ($foundNode instanceof $subNode && $subNode === $foundNode) {
$scopedNode = $subNode;
return NodeTraverser::STOP_TRAVERSAL;
Expand Down