diff --git a/rules-tests/DowngradePhp74/Rector/ArrowFunction/ArrowFunctionToAnonymousFunctionRector/Fixture/fn_inside_fn.php.inc b/rules-tests/DowngradePhp74/Rector/ArrowFunction/ArrowFunctionToAnonymousFunctionRector/Fixture/fn_inside_fn.php.inc new file mode 100644 index 00000000000..a2b6342c8b5 --- /dev/null +++ b/rules-tests/DowngradePhp74/Rector/ArrowFunction/ArrowFunctionToAnonymousFunctionRector/Fixture/fn_inside_fn.php.inc @@ -0,0 +1,47 @@ + array_udiff( + $serviceObjectList, + $object->getServiceObjects(), + fn ($object1, $object2) => get_class($object1) <=> get_class($object2), + ) === [], + ); + } +} +?> +----- +getServiceObjects(), + function ($object1, $object2) { + return get_class($object1) <=> get_class($object2); + }, + ) === []; + }, + ); + } +} +?> diff --git a/rules/Php72/NodeFactory/AnonymousFunctionFactory.php b/rules/Php72/NodeFactory/AnonymousFunctionFactory.php index 099c5094249..55f5335ab66 100644 --- a/rules/Php72/NodeFactory/AnonymousFunctionFactory.php +++ b/rules/Php72/NodeFactory/AnonymousFunctionFactory.php @@ -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; @@ -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 = []; @@ -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[]