forked from phpstan/phpstan-src
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbug-13705.php
More file actions
129 lines (118 loc) · 3.67 KB
/
bug-13705.php
File metadata and controls
129 lines (118 loc) · 3.67 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
<?php declare(strict_types = 1);
namespace Bug13705;
use function PHPStan\Testing\assertType;
function whileLoop(): void
{
$quantity = random_int(1, 42);
$codes = [];
while (count($codes) < $quantity) {
assertType('list<non-empty-string>', $codes);
$code = random_bytes(16);
if (!in_array($code, $codes, true)) {
$codes[] = $code;
}
}
}
/**
* @param list<string> $arr
* @param int<2, 5> $boundedRange
* @param int<2, max> $unboundedMaxRange
* @param int<min, 5> $unboundedMinRange
*/
function countLessThanRange(array $arr, int $boundedRange, int $unboundedMaxRange, int $unboundedMinRange): void
{
// count($arr) < $range → inverted to NOT($range <= count($arr))
// Inner: orEqual=true, false context → falsey + max !== null + orEqual (branch 1)
// Else: orEqual=true, true context → truthy + min !== null + orEqual (branch 3)
if (count($arr) < $boundedRange) {
assertType('list<string>', $arr);
} else {
assertType('non-empty-list<string>&hasOffsetValue(1, string)', $arr);
}
// count($arr) < unbounded max range → falsey + max is null → fallback via min (branch 3/4)
if (count($arr) < $unboundedMaxRange) {
assertType('list<string>', $arr);
} else {
assertType('non-empty-list<string>&hasOffsetValue(1, string)', $arr);
}
// count($arr) < unbounded min range → fallback branch (min is null)
if (count($arr) < $unboundedMinRange) {
assertType('list<string>', $arr);
} else {
assertType('list<string>', $arr);
}
}
/**
* @param list<string> $arr
* @param int<2, 5> $boundedRange
*/
function countLessThanOrEqualRange(array $arr, int $boundedRange): void
{
// count($arr) <= $range → inverted to NOT($range < count($arr))
// Inner: orEqual=false, false context → falsey + max !== null + !orEqual (branch 2)
// Else: orEqual=false, true context → truthy + min !== null + !orEqual (branch 4)
if (count($arr) <= $boundedRange) {
assertType('list<string>', $arr);
} else {
assertType('non-empty-list<string>&hasOffsetValue(1, string)&hasOffsetValue(2, string)', $arr);
}
}
/**
* @param list<string> $arr
* @param int<2, 5> $boundedRange
*/
function rangeGreaterThanOrEqualCount(array $arr, int $boundedRange): void
{
// $range >= count($arr) → same as count($arr) <= $range
if ($boundedRange >= count($arr)) {
assertType('list<string>', $arr);
} else {
assertType('non-empty-list<string>&hasOffsetValue(1, string)&hasOffsetValue(2, string)', $arr);
}
}
/**
* @param list<string> $arr
* @param int<2, 5> $boundedRange
*/
function rangeLessThanOrEqualCount(array $arr, int $boundedRange): void
{
// $range <= count($arr) → direct, orEqual=true
// True context: truthy + orEqual + min !== null (branch 3)
// False context: falsey + orEqual + max !== null (branch 1)
if ($boundedRange <= count($arr)) {
assertType('non-empty-list<string>&hasOffsetValue(1, string)', $arr);
} else {
assertType('list<string>', $arr);
}
}
/**
* @param list<string> $arr
* @param int<2, 5> $boundedRange
*/
function rangeLessThanCount(array $arr, int $boundedRange): void
{
// $range < count($arr) → direct, orEqual=false
// True context: truthy + !orEqual + min !== null (branch 4)
// False context: falsey + !orEqual + max !== null (branch 2)
if ($boundedRange < count($arr)) {
assertType('non-empty-list<string>&hasOffsetValue(1, string)&hasOffsetValue(2, string)', $arr);
} else {
assertType('list<string>', $arr);
}
}
function whileLoopOriginal(int $length, int $quantity): void
{
if ($length < 8) {
throw new \InvalidArgumentException();
}
$codes = [];
while ($quantity >= 1 && count($codes) < $quantity) {
$code = '';
for ($i = 0; $i < $length; $i++) {
$code .= 'x';
}
if (!in_array($code, $codes, true)) {
$codes[] = $code;
}
}
}