Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -18,36 +18,13 @@ class Fixture
}

/**
* @param array<int, string> $array
* @param array<int, Foo> $arrayTwo
*/
public function runTwo(array $array, array $arrayTwo)
{
return array_map(function ($value, $second) {
return get_class($second) . $value;
}, $array, $arrayTwo);
}

/**
* @param array<int, string> $array
* @param array<int, Foo|Bar> $arrayTwo
*/
public function runThree(array $array, array $arrayTwo)
{
return array_map(function ($value, $second) {
return get_class($second) . $value;
}, $array, $arrayTwo);
}

/**
* @param array<int, string> $array
* @param array<string> $arrayTwo
* @param array<int, Foo|Bar> $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);
}
}

Expand All @@ -73,36 +50,13 @@ class Fixture
}

/**
* @param array<int, string> $array
* @param array<int, Foo> $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<int, string> $array
* @param array<int, Foo|Bar> $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<int, string> $array
* @param array<string> $arrayTwo
* @param array<int, Foo|Bar> $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);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Rector\Tests\TypeDeclaration\Rector\FunctionLike\AddClosureParamTypeForArrayMapRector\Fixture;

class SkipArrayKeysAndValues
{
/**
* @param array<string, string> $tokens
*/
public function run(array $tokens): void
{
array_map(function ($token, $value) {
echo $token . $value;
}, array_keys($tokens), $tokens);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Rector\Tests\TypeDeclaration\Rector\FunctionLike\AddClosureParamTypeForArrayMapRector\Fixture;

final class SkipSeveralArrays
{
/**
* array_map() pads the shorter array with null
*
* @param array<int, string> $array
* @param array<string> $arrayTwo
*/
public function run(array $array, array $arrayTwo)
{
return array_map(function ($value, $second) {
return $second . $value;
}, $array, $arrayTwo);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Loading