forked from phpstan/phpstan-src
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbug-14407.php
More file actions
100 lines (81 loc) · 1.7 KB
/
bug-14407.php
File metadata and controls
100 lines (81 loc) · 1.7 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
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
<?php // lint >= 8.1
declare(strict_types = 1);
namespace Bug14407;
use function PHPStan\Testing\assertType;
enum SomeEnum {
case A;
case B;
}
function () {
$arr = [];
if (rand(0, 1) === 0) {
$arr[] = SomeEnum::A;
} else {
$arr[] = SomeEnum::B;
}
if (rand(0, 1) === 0) {
$x = SomeEnum::A;
} else {
$x = SomeEnum::B;
}
if (!in_array($x, $arr)) {
// either $x=A, $arr=[B] or $x=B, $arr=[A]
assertType('Bug14407\SomeEnum::A|Bug14407\SomeEnum::B', $x);
}
if (!in_array($x, $arr) && $x === SomeEnum::A) {
// $x=A, $arr=[B]
assertType('Bug14407\SomeEnum::A', $x);
}
};
function () {
$arr = [SomeEnum::A, SomeEnum::B];
if (rand(0, 1) === 0) {
$x = SomeEnum::A;
} else {
$x = SomeEnum::B;
}
if (!in_array($x, $arr)) {
// array always contains both A and B, so this is correctly *NEVER*
assertType('*NEVER*', $x);
}
};
function () {
$arr = [];
$r = rand(0, 2);
if ($r === 0) {
$arr[] = SomeEnum::A;
} elseif ($r === 1) {
$arr[] = SomeEnum::B;
}
if (rand(0, 1) === 0) {
$x = SomeEnum::A;
} else {
$x = SomeEnum::B;
}
// arr might be empty, so no narrowing possible
if (!in_array($x, $arr) && $x === SomeEnum::A) {
assertType('Bug14407\SomeEnum::A', $x);
}
};
/**
* @param 'a'|'b'|'c' $x
* @param array{a: 'a', c: 'c'}|array{a?:'a', b: 'b'} $a
*/
function testUnionWithOptionalKeys($x, $a): void
{
assertType("array{a: 'a', c: 'c'}|array{a?: 'a', b: 'b'}", $a);
if (!\in_array($x, $a, true)) {
assertType("'a'|'b'|'c'", $x);
}
};
/**
* @param 'a'|'b'|'c' $x
* @param non-empty-array<'a'|'b'> $a
*/
function testNonConstantArray($x, $a): void
{
assertType("non-empty-array<'a'|'b'>", $a);
if (!\in_array($x, $a, true)) {
assertType("'a'|'b'|'c'", $x);
}
};