|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types = 1); |
| 4 | + |
| 5 | +namespace Bug13857; |
| 6 | + |
| 7 | +use function PHPStan\Testing\assertType; |
| 8 | + |
| 9 | +/** |
| 10 | + * @param array<int, array{state: string}> $array |
| 11 | + */ |
| 12 | +function test(array $array, int $id): void { |
| 13 | + $array[$id]['state'] = 'foo'; |
| 14 | + // only one element was set to 'foo', not all of them. |
| 15 | + assertType("non-empty-array<int, array{state: string}>", $array); |
| 16 | +} |
| 17 | + |
| 18 | +/** |
| 19 | + * @param array<int, array{state?: string}> $array |
| 20 | + */ |
| 21 | +function testMaybe(array $array, int $id): void { |
| 22 | + $array[$id]['state'] = 'foo'; |
| 23 | + // only one element was set to 'foo', not all of them. |
| 24 | + assertType("non-empty-array<int, array{state?: string}>", $array); |
| 25 | +} |
| 26 | + |
| 27 | +/** |
| 28 | + * @param array<int, array{state: string|bool}> $array |
| 29 | + */ |
| 30 | +function testUnionValue(array $array, int $id): void { |
| 31 | + $array[$id]['state'] = 'foo'; |
| 32 | + // only one element was set to 'foo', not all of them. |
| 33 | + assertType("non-empty-array<int, array{state: bool|string}>", $array); |
| 34 | +} |
| 35 | + |
| 36 | +/** |
| 37 | + * @param array<int, array{state: string}|array{foo: int}> $array |
| 38 | + */ |
| 39 | +function testUnionArray(array $array, int $id): void { |
| 40 | + $array[$id]['state'] = 'foo'; |
| 41 | + // only one element was set to 'foo', not all of them. |
| 42 | + assertType("non-empty-array<int, non-empty-array{foo?: int, state?: string}>", $array); |
| 43 | +} |
| 44 | + |
| 45 | +/** |
| 46 | + * @param array<int, array{state: string}|array{foo: int}> $array |
| 47 | + */ |
| 48 | +function testUnionArrayDifferentType(array $array, int $id): void { |
| 49 | + $array[$id]['state'] = true; |
| 50 | + assertType("non-empty-array<int, array{state: string}|non-empty-array{foo?: int, state?: true}>", $array); |
| 51 | +} |
| 52 | + |
| 53 | +/** |
| 54 | + * @param array<int, array{state: 'foo'}> $array |
| 55 | + */ |
| 56 | +function testConstantArray(array $array, int $id): void { |
| 57 | + $array[$id]['state'] = 'bar'; |
| 58 | + assertType("non-empty-array<int, array{state: 'bar'}|array{state: 'foo'}>", $array); |
| 59 | +} |
| 60 | + |
| 61 | +/** |
| 62 | + * @param array<int, array{state: 'foo'}> $array |
| 63 | + */ |
| 64 | +function testConstantArrayNonScalarAssign(array $array, int $id, bool $b): void { |
| 65 | + $array[$id]['state'] = $b; |
| 66 | + assertType("non-empty-array<int, array{state: 'foo'}|array{state: bool}>", $array); |
| 67 | +} |
0 commit comments