forked from phpstan/phpstan-src
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbug-1311-constant-array.php
More file actions
99 lines (73 loc) · 1.64 KB
/
bug-1311-constant-array.php
File metadata and controls
99 lines (73 loc) · 1.64 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
<?php
namespace Bug1311ConstantArray;
use function PHPStan\Testing\assertType;
class HelloWorld
{
/**
* @var array<int, string>
*/
private $list = [];
public function convertListByRefWithoutKey(): void
{
$temp = [1, 2, 3];
foreach ($temp as &$item) {
$item = (string) $item;
}
assertType("array{'1'|'2'|'3', '1'|'2'|'3', '1'|'2'|'3'}", $temp);
$this->list = $temp;
}
public function convertListByRefWithKey(): void
{
$temp = [1, 2, 3];
foreach ($temp as $k => &$item) {
$item = (string) $item;
}
assertType("array{'1'|'2'|'3', '1'|'2'|'3', '1'|'2'|'3'}", $temp);
$this->list = $temp;
}
public function byRefConstantArrayConditional(): void
{
$temp = [1, 2, 3];
foreach ($temp as &$item) {
if (rand(0, 1)) {
$item = (string) $item;
}
}
assertType("array{1|2|3|'1'|'2'|'3', 1|2|3|'1'|'2'|'3', 1|2|3|'1'|'2'|'3'}", $temp);
}
public function byRefConstantArrayWithBreak(): void
{
$temp = [1, 2, 3];
foreach ($temp as &$item) {
$item = (string) $item;
if (rand(0, 1)) {
break;
}
}
assertType('array{1, 2, 3}', $temp);
}
public function byRefConstantArrayIntval(): void
{
$temp = ['a', 'b', 'c'];
foreach ($temp as &$item) {
$item = strlen($item);
}
assertType('array{1, 1, 1}', $temp);
}
public function byRefConstantArrayStringKeys(): void
{
$temp = ['x' => 1, 'y' => 2];
foreach ($temp as &$v) {
$v = (string) $v;
}
assertType("array{x: '1'|'2', y: '1'|'2'}", $temp);
}
public function byRefConstantArrayNoOverwrite(): void
{
$temp = [1, 2, 3];
foreach ($temp as &$item) {
echo $item;
}
assertType('array{1, 2, 3}', $temp);
}
}