forked from phpstan/phpstan-src
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharray_values.php
More file actions
48 lines (42 loc) · 1.02 KB
/
array_values.php
File metadata and controls
48 lines (42 loc) · 1.02 KB
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
<?php // lint >= 8.0
namespace ArrayValues;
use function PHPStan\Testing\assertType;
class HelloWorld
{
public function foo1($mixed): void
{
if(is_array($mixed)) {
assertType('list<mixed>', array_values($mixed));
} else {
assertType('mixed~array<mixed, mixed>', $mixed);
assertType('*NEVER*', array_values($mixed));
}
}
/**
* @param list<string> $list
*/
public function foo2($list): void
{
if(is_array($list)) {
assertType('list<string>', array_values($list));
} else {
assertType('*NEVER*', $list);
assertType('*NEVER*', array_values($list));
}
}
public function constantArrayType(): void
{
$numbers = array_filter(
[1 => 'a', 2 => 'b', 3 => 'c'],
static fn ($value) => mt_rand(0, 1) === 0,
);
assertType("list{0?: 'a'|'b'|'c', 1?: 'b'|'c', 2?: 'c'}", array_values($numbers));
}
/**
* @param array<string, non-empty-array<string, int>> $a
*/
public function arrayMap(array $a): void
{
assertType('array<string, non-empty-list<int>>', array_map(array_values(...), $a));
}
}