|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types = 1); |
| 4 | + |
| 5 | +namespace Bug15391; |
| 6 | + |
| 7 | +use function PHPStan\Testing\assertType; |
| 8 | + |
| 9 | +/** |
| 10 | + * @param list{0: 'x', 1?: 'a'|'b', 2?: 'a'|'b'} $l |
| 11 | + */ |
| 12 | +function narrow(array $l): void |
| 13 | +{ |
| 14 | + if (!in_array('b', $l, true)) { |
| 15 | + assertType("list{0: 'x', 1?: 'a', 2?: 'a'}", $l); |
| 16 | + } else { |
| 17 | + assertType("list{0: 'x', 1?: 'a'|'b', 2?: 'a'|'b'}", $l); |
| 18 | + } |
| 19 | + |
| 20 | + if (in_array('a', $l, true)) { |
| 21 | + assertType("list{0: 'x', 1?: 'a'|'b', 2?: 'a'|'b'}", $l); |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +/** |
| 26 | + * @param list{0: 'x', 1?: 'a'|'b', 2?: 'a'|'b'}&array<mixed, 'a'|'x'> $a |
| 27 | + * @param array{0: 'x', 1?: 'a'|'b', 2?: 'a'|'b'}&array<mixed, 'a'|'x'> $b |
| 28 | + */ |
| 29 | +function intersect(array $a, array $b): void |
| 30 | +{ |
| 31 | + assertType("list{0: 'x', 1?: 'a', 2?: 'a'}", $a); |
| 32 | + assertType("array{0: 'x', 1?: 'a', 2?: 'a'}", $b); |
| 33 | +} |
| 34 | + |
| 35 | +function guardedAppendsInLoops(bool $c): void |
| 36 | +{ |
| 37 | + $l = ['x']; |
| 38 | + foreach ([1, 2] as $_) { |
| 39 | + if ($c && !in_array('a', $l, true)) { |
| 40 | + $l[] = 'a'; |
| 41 | + } |
| 42 | + } |
| 43 | + foreach ([1, 2] as $_) { |
| 44 | + if (!in_array('b', $l, true)) { |
| 45 | + $l[] = 'b'; |
| 46 | + } |
| 47 | + } |
| 48 | + assertType("list{0: 'x', 1: 'a'|'b', 2?: 'a'|'b', 3?: 'b', 4?: 'b'}", $l); |
| 49 | +} |
0 commit comments