diff --git a/rules/DowngradePhp74/Rector/ArrowFunction/ArrowFunctionToAnonymousFunctionRector.php b/rules/DowngradePhp74/Rector/ArrowFunction/ArrowFunctionToAnonymousFunctionRector.php index fabfbd36..4d062c47 100644 --- a/rules/DowngradePhp74/Rector/ArrowFunction/ArrowFunctionToAnonymousFunctionRector.php +++ b/rules/DowngradePhp74/Rector/ArrowFunction/ArrowFunctionToAnonymousFunctionRector.php @@ -5,6 +5,7 @@ namespace Rector\DowngradePhp74\Rector\ArrowFunction; use PhpParser\Node; +use PhpParser\Node\Expr; use PhpParser\Node\Expr\ArrowFunction; use PhpParser\Node\Expr\Assign; use PhpParser\Node\Expr\Closure; @@ -12,8 +13,10 @@ use PhpParser\Node\Expr\Variable; use PhpParser\Node\Stmt\Return_; use PhpParser\Node\Stmt\Throw_; +use PHPStan\Analyser\Scope; use Rector\Core\PhpParser\Node\BetterNodeFinder; use Rector\Core\Rector\AbstractRector; +use Rector\NodeTypeResolver\Node\AttributeKey; use Rector\Php72\NodeFactory\AnonymousFunctionFactory; use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; @@ -114,6 +117,35 @@ public function refactor(Node $node): Closure return new Throw_($throw->expr); }); + $this->appendUsesFromInsertedVariable($node->expr, $anonymousFunctionFactory); + return $anonymousFunctionFactory; } + + private function appendUsesFromInsertedVariable(Expr $expr, Closure $anonymousFunctionFactory): void + { + $this->traverseNodesWithCallable($expr, function (Node $subNode) use ($anonymousFunctionFactory) { + if (! $subNode instanceof Variable) { + return null; + } + + $variableName = $this->getName($subNode); + if ($variableName === null) { + return null; + } + + if ($subNode->hasAttribute(AttributeKey::ORIGINAL_NODE)) { + return null; + } + + $scope = $subNode->getAttribute(AttributeKey::SCOPE); + if (! $scope instanceof Scope) { + return null; + } + + if (! $scope->hasVariableType($variableName)->yes()) { + $anonymousFunctionFactory->uses[] = new ClosureUse(new Variable($variableName)); + } + }); + } } diff --git a/tests/Issues/DowngradeArrayIsListArrowFunction/DowngradeArrayIsListArrowFunctionTest.php b/tests/Issues/DowngradeArrayIsListArrowFunction/DowngradeArrayIsListArrowFunctionTest.php new file mode 100644 index 00000000..0bc5eb5a --- /dev/null +++ b/tests/Issues/DowngradeArrayIsListArrowFunction/DowngradeArrayIsListArrowFunctionTest.php @@ -0,0 +1,28 @@ +doTestFile($filePath); + } + + public static function provideData(): Iterator + { + return self::yieldFilesFromDirectory(__DIR__ . '/Fixture'); + } + + public function provideConfigFilePath(): string + { + return __DIR__ . '/config/configured_rule.php'; + } +} diff --git a/tests/Issues/DowngradeArrayIsListArrowFunction/Fixture/fixture.php.inc b/tests/Issues/DowngradeArrayIsListArrowFunction/Fixture/fixture.php.inc new file mode 100644 index 00000000..c62bef6d --- /dev/null +++ b/tests/Issues/DowngradeArrayIsListArrowFunction/Fixture/fixture.php.inc @@ -0,0 +1,51 @@ + is_array($item) && !array_is_list($item)? (object) $this->convertToObject($item) : ($item), + $items + ); + } +} + +?> +----- + $noop) { + if ($key !== $current_key) { + return false; + } + ++$current_key; + } + return true; + }; + return array_map( + function ($item) use ($arrayIsListFunction) { + return is_array($item) && !$arrayIsListFunction($item)? (object) $this->convertToObject($item) : ($item); + }, + $items + ); + } +} + +?> diff --git a/tests/Issues/DowngradeArrayIsListArrowFunction/Fixture/fixture.phphd9Xuk b/tests/Issues/DowngradeArrayIsListArrowFunction/Fixture/fixture.phphd9Xuk new file mode 100644 index 00000000..aad960c6 --- /dev/null +++ b/tests/Issues/DowngradeArrayIsListArrowFunction/Fixture/fixture.phphd9Xuk @@ -0,0 +1,34 @@ + $noop) { + if ($key !== $current_key) { + return false; + } + ++$current_key; + } + return true; + }; + return array_map( + function ($item) use ($arrayIsListFunction, $arrayIsListFunction, $arrayIsListFunction, $arrayIsListFunction, $arrayIsListFunction, $arrayIsListFunction, $arrayIsListFunction, $arrayIsListFunction, $arrayIsListFunction, $arrayIsListFunction, $arrayIsListFunction, $arrayIsListFunction, $arrayIsListFunction, $arrayIsListFunction, $arrayIsListFunction, $arrayIsListFunction, $arrayIsListFunction, $arrayIsListFunction, $arrayIsListFunction, $arrayIsListFunction, $arrayIsListFunction, $arrayIsListFunction, $arrayIsListFunction, $arrayIsListFunction, $arrayIsListFunction, $arrayIsListFunction, $arrayIsListFunction, $arrayIsListFunction, $arrayIsListFunction, $arrayIsListFunction, $arrayIsListFunction, $arrayIsListFunction, $arrayIsListFunction, $arrayIsListFunction, $arrayIsListFunction, $arrayIsListFunction, $arrayIsListFunction, $arrayIsListFunction, $arrayIsListFunction, $arrayIsListFunction, $arrayIsListFunction, $arrayIsListFunction, $arrayIsListFunction, $arrayIsListFunction, $arrayIsListFunction, $arrayIsListFunction, $arrayIsListFunction, $arrayIsListFunction, $arrayIsListFunction, $arrayIsListFunction, $arrayIsListFunction, $arrayIsListFunction, $arrayIsListFunction, $arrayIsListFunction, $arrayIsListFunction, $arrayIsListFunction, $arrayIsListFunction, $arrayIsListFunction, $arrayIsListFunction, $arrayIsListFunction, $arrayIsListFunction, $arrayIsListFunction, $arrayIsListFunction, $arrayIsListFunction, $arrayIsListFunction, $arrayIsListFunction, $arrayIsListFunction, $arrayIsListFunction, $arrayIsListFunction, $arrayIsListFunction, $arrayIsListFunction, $arrayIsListFunction, $arrayIsListFunction, $arrayIsListFunction, $arrayIsListFunction, $arrayIsListFunction, $arrayIsListFunction, $arrayIsListFunction, $arrayIsListFunction, $arrayIsListFunction, $arrayIsListFunction, $arrayIsListFunction, $arrayIsListFunction, $arrayIsListFunction, $arrayIsListFunction, $arrayIsListFunction, $arrayIsListFunction, $arrayIsListFunction, $arrayIsListFunction, $arrayIsListFunction, $arrayIsListFunction, $arrayIsListFunction, $arrayIsListFunction, $arrayIsListFunction, $arrayIsListFunction, $arrayIsListFunction, $arrayIsListFunction, $arrayIsListFunction, $arrayIsListFunction, $arrayIsListFunction, $arrayIsListFunction, $arrayIsListFunction, $arrayIsListFunction, $arrayIsListFunction, $arrayIsListFunction, $arrayIsListFunction, $arrayIsListFunction, $arrayIsListFunction, $arrayIsListFunction, $arrayIsListFunction, $arrayIsListFunction, $arrayIsListFunction, $arrayIsListFunction, $arrayIsListFunction, $arrayIsListFunction, $arrayIsListFunction, $arrayIsListFunction, $arrayIsListFunction, $arrayIsListFunction, $arrayIsListFunction, $arrayIsListFunction, $arrayIsListFunction, $arrayIsListFunction, $arrayIsListFunction, $arrayIsListFunction, $arrayIsListFunction, $arrayIsListFunction, $arrayIsListFunction, $arrayIsListFunction, $arrayIsListFunction, $arrayIsListFunction, $arrayIsListFunction, $arrayIsListFunction, $arrayIsListFunction, $arrayIsListFunction, $arrayIsListFunction, $arrayIsListFunction, $arrayIsListFunction, $arrayIsListFunction, $arrayIsListFunction, $arrayIsListFunction, $arrayIsListFunction, $arrayIsListFunction, $arrayIsListFunction, $arrayIsListFunction, $arrayIsListFunction, $arrayIsListFunction, $arrayIsListFunction, $arrayIsListFunction, $arrayIsListFunction, $arrayIsListFunction, $arrayIsListFunction, $arrayIsListFunction, $arrayIsListFunction, $arrayIsListFunction, $arrayIsListFunction, $arrayIsListFunction, $arrayIsListFunction, $arrayIsListFunction, $arrayIsListFunction, $arrayIsListFunction, $arrayIsListFunction, $arrayIsListFunction, $arrayIsListFunction, $arrayIsListFunction, $arrayIsListFunction, $arrayIsListFunction, $arrayIsListFunction, $arrayIsListFunction, $arrayIsListFunction, $arrayIsListFunction, $arrayIsListFunction, $arrayIsListFunction, $arrayIsListFunction, $arrayIsListFunction, $arrayIsListFunction, $arrayIsListFunction) { + return is_array($item) && !$arrayIsListFunction($item)? (object) $this->convertToObject($item) : ($item); + }, + $items + ); + } +} + +?> diff --git a/tests/Issues/DowngradeArrayIsListArrowFunction/config/configured_rule.php b/tests/Issues/DowngradeArrayIsListArrowFunction/config/configured_rule.php new file mode 100644 index 00000000..619cf3ac --- /dev/null +++ b/tests/Issues/DowngradeArrayIsListArrowFunction/config/configured_rule.php @@ -0,0 +1,11 @@ +rules([DowngradeArrayIsListRector::class, ArrowFunctionToAnonymousFunctionRector::class]); +};