forked from phpstan/phpstan-src
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbug-7280.php
More file actions
59 lines (50 loc) · 1.35 KB
/
bug-7280.php
File metadata and controls
59 lines (50 loc) · 1.35 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
<?php // lint >= 8.0
declare(strict_types = 1);
namespace Bug7280;
use function PHPStan\Testing\assertType;
// Test 1: Precise carry type from initial argument with constant array shape
$result1 = array_reduce(
['test1', 'test2'],
static function (array $carry, string $value): array {
assertType("array{starts: array{}, ends: array{}}", $carry);
$carry['starts'][] = $value;
$carry['ends'][] = $value;
return $carry;
},
initial: ['starts' => [], 'ends' => []],
);
// Test 2: Arrow function with precise carry type
$result2 = array_reduce(
[1, 2, 3],
static fn (int $carry, int $value): int => $carry + $value,
0,
);
assertType('int', $result2);
// Test 3: Carry type with no initial (defaults to null)
$result3 = array_reduce(
[1, 2, 3],
static function (?int $carry, int $value): int {
assertType('null', $carry);
return ($carry ?? 0) + $value;
},
);
// Test 4: Initial value type narrows carry - literal string initial
$result4 = array_reduce(
['a', 'b', 'c'],
static function (string $carry, string $value): string {
assertType('string', $carry);
return $carry . $value;
},
'',
);
assertType('string', $result4);
// Test 5: Carry type with literal int initial
$result5 = array_reduce(
[1, 2, 3],
static function (int $carry, int $value): int {
assertType('int', $carry);
return $carry + $value;
},
0,
);
assertType('int', $result5);