forked from phpstan/phpstan-src
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbug-1311.php
More file actions
147 lines (111 loc) · 2.58 KB
/
bug-1311.php
File metadata and controls
147 lines (111 loc) · 2.58 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<?php
namespace Bug1311;
use function PHPStan\Testing\assertType;
class HelloWorld
{
/**
* @var array<int, string>
*/
private $list = [];
/**
* @param array<int, int> $temp
*/
public function convertList(array $temp): void
{
foreach ($temp as &$item) {
$item = (string) $item;
}
assertType('array<int, lowercase-string&numeric-string&uppercase-string>', $temp);
$this->list = $temp;
}
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);
}
public function constantArrayByRefSubElement(): void
{
$a = [
[
'one' => 'one',
'two' => 'two',
],
[
'one' => 'one',
],
];
foreach ($a as &$testArray) {
$testArray['two'] = 'two';
}
unset($testArray);
assertType("array{array{one: 'one', two: 'two'}, array{one: 'one', two: 'two'}}", $a);
$key = 'three';
foreach ($a as $offset => $testArray) {
$a[$offset][$key] = $key;
}
assertType("array{array{one: 'one', two: 'two', three: 'three'}, array{one: 'one', two: 'two', three: 'three'}}", $a);
foreach ($a as $testArray) {
assertType("array{one: 'one', two: 'two', three: 'three'}", $testArray);
$testArray['two'];
$testArray['three'];
}
}
}