-
Notifications
You must be signed in to change notification settings - Fork 428
/
bug-5372.php
75 lines (61 loc) · 1.53 KB
/
bug-5372.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<?php // lint >= 7.4
namespace Bug5372;
use function PHPStan\Testing\assertType;
/**
* @template TKey of array-key
* @template T
*/
class Collection
{
/** @var array<TKey, T> */
private $values;
/**
* @param array<TKey, T> $values
*/
public function __construct(array $values)
{
$this->values = $values;
}
/**
* @template V
*
* @param callable(T): V $callback
*
* @return self<TKey, V>
*/
public function map(callable $callback): self {
return new self(array_map($callback, $this->values));
}
/**
* @template V of string
*
* @param callable(T): V $callback
*
* @return self<TKey, V>
*/
public function map2(callable $callback): self {
return new self(array_map($callback, $this->values));
}
}
class Foo
{
/** @param Collection<int, string> $list */
function takesStrings(Collection $list): void {
echo serialize($list);
}
/** @param class-string $classString */
public function doFoo(string $classString)
{
$col = new Collection(['foo', 'bar']);
assertType('Bug5372\Collection<int, string>', $col);
$newCol = $col->map(static fn(string $var): string => $var . 'bar');
assertType('Bug5372\Collection<int, string>', $newCol);
$this->takesStrings($newCol);
$newCol = $col->map(static fn(string $var): string => $classString);
assertType('Bug5372\Collection<int, string>', $newCol);
$this->takesStrings($newCol);
$newCol = $col->map2(static fn(string $var): string => $classString);
assertType('Bug5372\Collection<int, class-string>', $newCol);
$this->takesStrings($newCol);
}
}