diff --git a/rules-tests/TypeDeclaration/Rector/FunctionLike/AddClosureParamTypeForArrayMapRector/Fixture/array_keys_and_values.php.inc b/rules-tests/TypeDeclaration/Rector/FunctionLike/AddClosureParamTypeForArrayMapRector/Fixture/array_keys_and_values.php.inc deleted file mode 100644 index 547eaa288c1..00000000000 --- a/rules-tests/TypeDeclaration/Rector/FunctionLike/AddClosureParamTypeForArrayMapRector/Fixture/array_keys_and_values.php.inc +++ /dev/null @@ -1,37 +0,0 @@ - $tokens - */ - public function run(array $tokens): void - { - array_map(function ($token, $value) { - echo $token . $value; - }, array_keys($tokens), $tokens); - } -} - -?> ------ - $tokens - */ - public function run(array $tokens): void - { - array_map(function (string $token, string $value) { - echo $token . $value; - }, array_keys($tokens), $tokens); - } -} - -?> diff --git a/rules-tests/TypeDeclaration/Rector/FunctionLike/AddClosureParamTypeForArrayMapRector/Fixture/fixture.php.inc b/rules-tests/TypeDeclaration/Rector/FunctionLike/AddClosureParamTypeForArrayMapRector/Fixture/fixture.php.inc index fc537a16056..c6c3c02d782 100644 --- a/rules-tests/TypeDeclaration/Rector/FunctionLike/AddClosureParamTypeForArrayMapRector/Fixture/fixture.php.inc +++ b/rules-tests/TypeDeclaration/Rector/FunctionLike/AddClosureParamTypeForArrayMapRector/Fixture/fixture.php.inc @@ -18,36 +18,13 @@ class Fixture } /** - * @param array $array - * @param array $arrayTwo - */ - public function runTwo(array $array, array $arrayTwo) - { - return array_map(function ($value, $second) { - return get_class($second) . $value; - }, $array, $arrayTwo); - } - - /** - * @param array $array - * @param array $arrayTwo - */ - public function runThree(array $array, array $arrayTwo) - { - return array_map(function ($value, $second) { - return get_class($second) . $value; - }, $array, $arrayTwo); - } - - /** - * @param array $array - * @param array $arrayTwo + * @param array $objects */ - public function runFour(array $array, array $arrayTwo) + public function runTwo(array $objects) { - return array_map(function ($value, $second) { - return $second . $value; - }, $array, $arrayTwo); + return array_map(function ($object) { + return get_class($object); + }, $objects); } } @@ -73,36 +50,13 @@ class Fixture } /** - * @param array $array - * @param array $arrayTwo - */ - public function runTwo(array $array, array $arrayTwo) - { - return array_map(function (string $value, \Rector\Tests\TypeDeclaration\Rector\FunctionLike\AddClosureParamTypeForArrayMapRector\Source\Foo $second) { - return get_class($second) . $value; - }, $array, $arrayTwo); - } - - /** - * @param array $array - * @param array $arrayTwo - */ - public function runThree(array $array, array $arrayTwo) - { - return array_map(function (string $value, \Rector\Tests\TypeDeclaration\Rector\FunctionLike\AddClosureParamTypeForArrayMapRector\Source\Foo|\Rector\Tests\TypeDeclaration\Rector\FunctionLike\AddClosureParamTypeForArrayMapRector\Source\Bar $second) { - return get_class($second) . $value; - }, $array, $arrayTwo); - } - - /** - * @param array $array - * @param array $arrayTwo + * @param array $objects */ - public function runFour(array $array, array $arrayTwo) + public function runTwo(array $objects) { - return array_map(function (string $value, string $second) { - return $second . $value; - }, $array, $arrayTwo); + return array_map(function (\Rector\Tests\TypeDeclaration\Rector\FunctionLike\AddClosureParamTypeForArrayMapRector\Source\Foo|\Rector\Tests\TypeDeclaration\Rector\FunctionLike\AddClosureParamTypeForArrayMapRector\Source\Bar $object) { + return get_class($object); + }, $objects); } } diff --git a/rules-tests/TypeDeclaration/Rector/FunctionLike/AddClosureParamTypeForArrayMapRector/Fixture/skip_array_keys_and_values.php.inc b/rules-tests/TypeDeclaration/Rector/FunctionLike/AddClosureParamTypeForArrayMapRector/Fixture/skip_array_keys_and_values.php.inc new file mode 100644 index 00000000000..9d22970f93a --- /dev/null +++ b/rules-tests/TypeDeclaration/Rector/FunctionLike/AddClosureParamTypeForArrayMapRector/Fixture/skip_array_keys_and_values.php.inc @@ -0,0 +1,16 @@ + $tokens + */ + public function run(array $tokens): void + { + array_map(function ($token, $value) { + echo $token . $value; + }, array_keys($tokens), $tokens); + } +} diff --git a/rules-tests/TypeDeclaration/Rector/FunctionLike/AddClosureParamTypeForArrayMapRector/Fixture/skip_several_arrays.php.inc b/rules-tests/TypeDeclaration/Rector/FunctionLike/AddClosureParamTypeForArrayMapRector/Fixture/skip_several_arrays.php.inc new file mode 100644 index 00000000000..700cfbed989 --- /dev/null +++ b/rules-tests/TypeDeclaration/Rector/FunctionLike/AddClosureParamTypeForArrayMapRector/Fixture/skip_several_arrays.php.inc @@ -0,0 +1,19 @@ + $array + * @param array $arrayTwo + */ + public function run(array $array, array $arrayTwo) + { + return array_map(function ($value, $second) { + return $second . $value; + }, $array, $arrayTwo); + } +} diff --git a/rules/TypeDeclaration/Rector/FunctionLike/AddClosureParamTypeForArrayMapRector.php b/rules/TypeDeclaration/Rector/FunctionLike/AddClosureParamTypeForArrayMapRector.php index 5d09f00f313..23c69c2fe2e 100644 --- a/rules/TypeDeclaration/Rector/FunctionLike/AddClosureParamTypeForArrayMapRector.php +++ b/rules/TypeDeclaration/Rector/FunctionLike/AddClosureParamTypeForArrayMapRector.php @@ -84,9 +84,11 @@ public function refactor(Node $node): ?Node return null; } - // array_map passes the value of each array positionally to the closure: - // the i-th closure param receives values from the i-th array argument. + // with several arrays, array_map() pads the shorter ones with null $arrayArgs = array_slice($node->args, 1); + if (count($arrayArgs) !== 1) { + return null; + } $closure = $args[0]->value; $changed = false;