Skip to content

Commit

Permalink
phpstan
Browse files Browse the repository at this point in the history
  • Loading branch information
samsonasik committed Apr 9, 2022
1 parent 23887bd commit b4319dd
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 30 deletions.
41 changes: 40 additions & 1 deletion rules/Php74/NodeAnalyzer/ClosureArrowFunctionAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ private function shouldSkipForUsedReferencedValue(Closure $closure): bool
return false;
}

return (bool) $this->betterNodeFinder->findFirstInFunctionLikeScoped($closure, function (Node $node) use (
$isFoundInStmt = (bool) $this->betterNodeFinder->findFirstInFunctionLikeScoped($closure, function (Node $node) use (
$referencedValues
): bool {
foreach ($referencedValues as $referencedValue) {
Expand All @@ -63,6 +63,45 @@ private function shouldSkipForUsedReferencedValue(Closure $closure): bool

return false;
});

if ($isFoundInStmt) {
return true;
}

return $this->isFoundFromUse($closure);
}

private function isFoundFromUse(Closure $node): bool
{
foreach ($node->uses as $use) {
if (! $use->byRef) {
continue;
}

$variable = $use->var;
$isFoundInClosure = (bool) $this->betterNodeFinder->findFirstInFunctionLikeScoped(
$node,
function (Node $subNode) use ($variable): bool {
if (! $subNode instanceof Closure) {
return false;
}

return (bool) array_filter(
$subNode->uses,
fn (ClosureUse $closureUse): bool => $closureUse->byRef && $this->nodeComparator->areNodesEqual(
$closureUse->var,
$variable
)
);
}
);

if ($isFoundInClosure) {
return true;
}
}

return false;
}

/**
Expand Down
29 changes: 0 additions & 29 deletions rules/Php74/Rector/Closure/ClosureToArrowFunctionRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,35 +75,6 @@ public function refactor(Node $node): ?Node
return null;
}

foreach ($node->uses as $use) {
if (! $use->byRef) {
continue;
}

$variable = $use->var;
$isFoundInClosure = $this->betterNodeFinder->findFirstInFunctionLikeScoped($node, function (Node $subNode) use ($variable): bool {
if (! $subNode instanceof Closure) {
return false;
}

foreach ($subNode->uses as $subNodeUse) {
if (! $subNodeUse->byRef) {
continue;
}

if ($this->nodeComparator->areNodesEqual($subNodeUse->var, $variable)) {
return true;
}
}

return false;
});

if ($isFoundInClosure) {
return null;
}
}

$arrowFunction = new ArrowFunction(
[
'params' => $node->params,
Expand Down

0 comments on commit b4319dd

Please sign in to comment.