|
| 1 | +<?php // lint >= 8.0 |
| 2 | + |
| 3 | +namespace Bug13552; |
| 4 | + |
| 5 | +use function PHPStan\Testing\assertType; |
| 6 | + |
| 7 | +interface MyInterface { |
| 8 | + public function doThing(): bool; |
| 9 | + |
| 10 | + /** |
| 11 | + * @return array<string, string> |
| 12 | + */ |
| 13 | + public function getArray(): array; |
| 14 | +} |
| 15 | + |
| 16 | +function test_addition(MyInterface $i): void { |
| 17 | + $x = $i->doThing() ? ['thing' => 'do'] : []; |
| 18 | + assertType("array{}|array{thing: 'do'}", $x); |
| 19 | + |
| 20 | + $x += $i->getArray(); |
| 21 | + assertType('array<string, string>', $x); |
| 22 | + |
| 23 | + $x = $x ?: ['test' => 'string']; |
| 24 | +} |
| 25 | + |
| 26 | +function more_test(MyInterface $i): void { |
| 27 | + $x = $i->doThing() ? ['thing' => 'do', 'always_here' => true] : ['always_here' => 42]; |
| 28 | + assertType("array{always_here: 42}|array{thing: 'do', always_here: true}", $x); |
| 29 | + |
| 30 | + $a = $i->getArray() + $x; |
| 31 | + assertType("non-empty-array<string, 42|string|true>&hasOffsetValue('always_here', 42|string|true)", $a); |
| 32 | + assertType('true', isset($a['always_here'])); |
| 33 | + |
| 34 | + $b = $x + $i->getArray(); |
| 35 | + assertType("non-empty-array<string, 42|string|true>&hasOffsetValue('always_here', 42|true)", $b); |
| 36 | + assertType('true', isset($b['always_here'])); |
| 37 | +} |
| 38 | + |
| 39 | +/** |
| 40 | + * @param array{thing?: 'do', always_here: 42|true} $x |
| 41 | + */ |
| 42 | +function more_test_2(MyInterface $i, array $x): void { |
| 43 | + $a = $i->getArray() + $x; |
| 44 | + assertType("non-empty-array<string, 42|string|true>&hasOffsetValue('always_here', 42|string|true)", $a); |
| 45 | + assertType('true', isset($a['always_here'])); |
| 46 | + |
| 47 | + $b = $x + $i->getArray(); |
| 48 | + assertType("non-empty-array<string, 42|string|true>&hasOffsetValue('always_here', 42|true)", $b); |
| 49 | + assertType('true', isset($b['always_here'])); |
| 50 | +} |
0 commit comments