forked from phpstan/phpstan-src
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
array_map - understand call with multiple arrays
- Loading branch information
1 parent
398afe6
commit ef53630
Showing
8 changed files
with
162 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
namespace ArrayMapMultiple; | ||
|
||
use function PHPStan\Testing\assertType; | ||
|
||
class Foo | ||
{ | ||
|
||
public function doFoo(int $i, string $s): void | ||
{ | ||
$result = array_map(function ($a, $b) { | ||
assertType('int', $a); | ||
assertType('string', $b); | ||
|
||
return rand(0, 1) ? $a : $b; | ||
}, ['foo' => $i], ['bar' => $s]); | ||
assertType('array<int, int|string>&nonEmpty', $result); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
<?php | ||
|
||
namespace ArrayMapMultipleCallTest; | ||
|
||
/** | ||
* @template TKey as array-key | ||
* @template TValue | ||
*/ | ||
class Collection | ||
{ | ||
/** | ||
* @var array<TKey, TValue> | ||
*/ | ||
protected $items = []; | ||
|
||
/** | ||
* @param array<TKey, TValue> $items | ||
* @return void | ||
*/ | ||
public function __construct($items) | ||
{ | ||
$this->items = $items; | ||
} | ||
|
||
/** | ||
* @template TMapValue | ||
* | ||
* @param callable(TValue, TKey): TMapValue $callback | ||
* @return self<TKey, TMapValue> | ||
*/ | ||
public function map(callable $callback) | ||
{ | ||
$keys = array_keys($this->items); | ||
|
||
$items = array_map($callback, $this->items, $keys); | ||
|
||
return new self(array_combine($keys, $items)); | ||
} | ||
|
||
/** | ||
* @return array<TKey, TValue> | ||
*/ | ||
public function all() | ||
{ | ||
return $this->items; | ||
} | ||
} | ||
|
||
class Foo | ||
{ | ||
|
||
public function doFoo(): void | ||
{ | ||
array_map(function (int $a, string $b) { | ||
|
||
}, [1, 2], ['foo', 'bar']); | ||
|
||
array_map(function (int $a, int $b) { | ||
|
||
}, [1, 2], ['foo', 'bar']); | ||
} | ||
|
||
} |