Skip to content

Commit

Permalink
[DowngradePhp74] fix uses for nested arrow functions with same param …
Browse files Browse the repository at this point in the history
…names (#1090)
  • Loading branch information
Lctrs committed Oct 28, 2021
1 parent 6a6dc6b commit e2bbc2f
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,17 @@ final class NestedStaticFnUses

public function mixed()
{
return fn($foo) => fn ($bar) => function () use ($foo, $bar) {
return fn ($foo) => fn ($bar) => function () use ($foo, $bar) {
return [$foo, $bar];
};
}

public function sameParamName()
{
return fn($foo) => fn($foo) => fn($bar) => $foo === $bar;
}
}

?>
-----
<?php
Expand Down Expand Up @@ -83,5 +89,17 @@ final class NestedStaticFnUses
};
};
}

public function sameParamName()
{
return function ($foo) {
return function ($foo) {
return function ($bar) use ($foo) {
return $foo === $bar;
};
};
};
}
}

?>
12 changes: 10 additions & 2 deletions rules/Php72/NodeFactory/AnonymousFunctionFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,17 +189,25 @@ private function cleanClosureUses(array $uses): array
$variableNames = array_unique($variableNames);

return array_map(
fn ($variableName): ClosureUse => new ClosureUse(new Variable($variableName)),
static fn ($variableName): ClosureUse => new ClosureUse(new Variable($variableName)),
$variableNames,
[]
);
}

private function applyNestedUses(Closure $anonymousFunctionNode, Variable $useVariable): Closure
{
$anonymousFunctionNode = clone $anonymousFunctionNode;
$parent = $this->betterNodeFinder->findParentType($useVariable, Closure::class);

if ($parent instanceof Closure) {
$paramNames = $this->nodeNameResolver->getNames($parent->params);

if ($this->nodeNameResolver->isNames($useVariable, $paramNames)) {
return $anonymousFunctionNode;
}
}

$anonymousFunctionNode = clone $anonymousFunctionNode;
while ($parent instanceof Closure) {
$parentOfParent = $this->betterNodeFinder->findParentType($parent, Closure::class);

Expand Down

0 comments on commit e2bbc2f

Please sign in to comment.