Skip to content

Commit

Permalink
[DowngradePhp74] Handle fn inside fn on ArrowFunctionToAnonymousFunct…
Browse files Browse the repository at this point in the history
…ionRector (#967)

* Add failing test fixture for ArrowFunctionToAnonymousFunctionRector

# Failing Test for ArrowFunctionToAnonymousFunctionRector

Based on https://getrector.org/demo/1ec26a84-7939-6dfc-82c4-1f4ae14adb2e

Issue: rectorphp/rector#6730

* Renamed test file

* [DowngradePhp74] Handle fn inside fn

* fix

Co-authored-by: Leonardo Losoviz <leo@getpop.org>
  • Loading branch information
samsonasik and leoloso committed Oct 7, 2021
1 parent 97a8d8e commit cddcebf
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace Rector\Tests\DowngradePhp74\Rector\ArrowFunction\ArrowFunctionToAnonymousFunctionRector\Fixture;

final class DemoFile
{
public function run()
{
$inputObjectList = [/* ... */];
$serviceObjectList = [/* ... */];
$containsAllServiceObjectList = array_filter(
$inputObjectList,
fn ($object) => array_udiff(
$serviceObjectList,
$object->getServiceObjects(),
fn ($object1, $object2) => get_class($object1) <=> get_class($object2),
) === [],
);
}
}
?>
-----
<?php

namespace Rector\Tests\DowngradePhp74\Rector\ArrowFunction\ArrowFunctionToAnonymousFunctionRector\Fixture;

final class DemoFile
{
public function run()
{
$inputObjectList = [/* ... */];
$serviceObjectList = [/* ... */];
$containsAllServiceObjectList = array_filter(
$inputObjectList,
function ($object) use ($serviceObjectList) {
return array_udiff(
$serviceObjectList,
$object->getServiceObjects(),
function ($object1, $object2) {
return get_class($object1) <=> get_class($object2);
},
) === [];
},
);
}
}
?>
23 changes: 22 additions & 1 deletion rules/Php72/NodeFactory/AnonymousFunctionFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use PhpParser\Node\ComplexType;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\ArrayDimFetch;
use PhpParser\Node\Expr\ArrowFunction;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\ClassConstFetch;
use PhpParser\Node\Expr\Closure;
Expand Down Expand Up @@ -177,7 +178,7 @@ private function createUseVariablesFromParams(array $nodes, array $paramNodes):
$paramNames[] = $this->nodeNameResolver->getName($paramNode);
}

$variableNodes = $this->betterNodeFinder->findInstanceOf($nodes, Variable::class);
$variableNodes = $this->resolveVariableNodes($nodes);

/** @var Variable[] $filteredVariables */
$filteredVariables = [];
Expand Down Expand Up @@ -212,6 +213,26 @@ private function createUseVariablesFromParams(array $nodes, array $paramNodes):
return $filteredVariables;
}

/**
* @param Node[] $nodes
* @return Variable[]
*/
private function resolveVariableNodes(array $nodes): array
{
return $this->betterNodeFinder->find($nodes, function (Node $subNode): bool {
if (! $subNode instanceof Variable) {
return false;
}

$parentArrowFunction = $this->betterNodeFinder->findParentType($subNode, ArrowFunction::class);
if ($parentArrowFunction instanceof ArrowFunction) {
return ! (bool) $this->betterNodeFinder->findParentType($parentArrowFunction, ArrowFunction::class);
}

return true;
});
}

/**
* @param ParameterReflection[] $parameterReflections
* @return Param[]
Expand Down

0 comments on commit cddcebf

Please sign in to comment.