|
| 1 | +<?php // lint >= 8.1 |
| 2 | + |
| 3 | +declare(strict_types = 1); |
| 4 | + |
| 5 | +namespace Bug14708; |
| 6 | + |
| 7 | +use function PHPStan\Testing\assertType; |
| 8 | + |
| 9 | +/** @return array{test: bool, spread?: true} */ |
| 10 | +function test1(bool $spread): array { |
| 11 | + $result = [ |
| 12 | + 'test' => $spread, |
| 13 | + ...($spread ? ['spread' => true] : []), |
| 14 | + ]; |
| 15 | + assertType('array{test: bool, spread?: true}', $result); |
| 16 | + return $result; |
| 17 | +} |
| 18 | + |
| 19 | +/** @return array{test: bool, spread?: true} */ |
| 20 | +function test2(bool $spread): array { |
| 21 | + $return1 = ['test' => $spread]; |
| 22 | + $return2 = $spread ? ['spread' => true] : []; |
| 23 | + |
| 24 | + $result = [...$return1, ...$return2]; |
| 25 | + assertType('array{test: bool, spread?: true}', $result); |
| 26 | + return $result; |
| 27 | +} |
| 28 | + |
| 29 | +/** @return array{test: bool, spread?: true} */ |
| 30 | +function test3(bool $spread): array { |
| 31 | + $return = ['test' => $spread]; |
| 32 | + if ($spread) { |
| 33 | + $return['spread'] = true; |
| 34 | + } |
| 35 | + |
| 36 | + assertType('array{test: bool, spread?: true}', $return); |
| 37 | + return $return; |
| 38 | +} |
| 39 | + |
| 40 | +function testMultipleOptionalKeys(bool $a, bool $b): void { |
| 41 | + $result = [ |
| 42 | + 'base' => 1, |
| 43 | + ...($a ? ['x' => 'hello'] : []), |
| 44 | + ...($b ? ['y' => 42] : []), |
| 45 | + ]; |
| 46 | + assertType("array{base: 1, x?: 'hello', y?: 42}", $result); |
| 47 | +} |
| 48 | + |
| 49 | +function testOverlappingKeys(bool $flag): void { |
| 50 | + $result = [ |
| 51 | + 'a' => 1, |
| 52 | + ...($flag ? ['a' => 2, 'b' => 3] : ['b' => 4]), |
| 53 | + ]; |
| 54 | + assertType('array{a: 1|2, b: 3|4}', $result); |
| 55 | +} |
| 56 | + |
| 57 | +function testIntegerKeysUnion(bool $flag): void { |
| 58 | + $result = [ |
| 59 | + 'start' => 0, |
| 60 | + ...($flag ? [1, 2] : [3]), |
| 61 | + ]; |
| 62 | + assertType('array{start: 0, 0: 1|3, 1?: 2}', $result); |
| 63 | +} |
| 64 | + |
| 65 | +function testAllBranchesSameKeys(bool $flag): void { |
| 66 | + $result = [ |
| 67 | + ...($flag ? ['a' => 1, 'b' => 2] : ['a' => 3, 'b' => 4]), |
| 68 | + ]; |
| 69 | + assertType('array{a: 1|3, b: 2|4}', $result); |
| 70 | +} |
| 71 | + |
| 72 | +/** @param 'x'|'y'|'z' $variant */ |
| 73 | +function testThreeBranchUnion(string $variant): void { |
| 74 | + if ($variant === 'x') { |
| 75 | + $extra = ['x' => 1]; |
| 76 | + } elseif ($variant === 'y') { |
| 77 | + $extra = ['y' => 2]; |
| 78 | + } else { |
| 79 | + $extra = []; |
| 80 | + } |
| 81 | + $result = ['base' => true, ...$extra]; |
| 82 | + assertType('array{base: true, y?: 2, x?: 1}', $result); |
| 83 | +} |
| 84 | + |
| 85 | +function testIntegerOnlyUnion(bool $flag): void { |
| 86 | + $result = [ |
| 87 | + ...($flag ? [1, 2, 3] : [4, 5]), |
| 88 | + ]; |
| 89 | + assertType('array{0: 1|4, 1: 2|5, 2?: 3}', $result); |
| 90 | +} |
| 91 | + |
| 92 | +function testEmptyVsNonEmpty(bool $flag): void { |
| 93 | + $result = [ |
| 94 | + ...($flag ? ['key' => 'value'] : []), |
| 95 | + ]; |
| 96 | + assertType("array{key?: 'value'}", $result); |
| 97 | +} |
| 98 | + |
| 99 | +function testStringKeyBranchAndIntegerKeyBranch(bool $flag): void { |
| 100 | + // integer keys are renumbered, string keys are kept |
| 101 | + $result = [ |
| 102 | + 9, |
| 103 | + ...($flag ? ['a' => 1] : [5]), |
| 104 | + ]; |
| 105 | + assertType('array{0: 9, a?: 1, 1?: 5}', $result); |
| 106 | +} |
| 107 | + |
| 108 | +function testMixedKeysInBothBranches(bool $flag): void { |
| 109 | + $result = [ |
| 110 | + ...($flag ? ['a' => 1, 7] : [5, 'a' => 2]), |
| 111 | + ]; |
| 112 | + assertType('array{a: 1|2, 0: 5|7}', $result); |
| 113 | +} |
0 commit comments