forked from phpstan/phpstan-src
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
math.php
142 lines (113 loc) · 3.46 KB
/
math.php
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
<?php
namespace MathTests;
use function PHPStan\Testing\assertType;
class Foo
{
const MAX_TOTAL_PRODUCTS = 22;
public function doFoo(array $excluded, int $i): void
{
assertType('int<min, 22>', self::MAX_TOTAL_PRODUCTS - count($excluded));
assertType('int', self::MAX_TOTAL_PRODUCTS - $i);
$maxOrPlusOne = self::MAX_TOTAL_PRODUCTS;
if (rand(0, 1)) {
$maxOrPlusOne++;
}
assertType('22|23', $maxOrPlusOne);
assertType('int<min, 23>', $maxOrPlusOne - count($excluded));
}
public function doBar(int $notZero): void
{
if ($notZero === 0) {
return;
}
assertType('int<min, 0>|int<2, max>', $notZero + 1);
}
/**
* @param int<-5, 5> $rangeFiveBoth
* @param int<-5, max> $rangeFiveLeft
* @param int<min, 5> $rangeFiveRight
*/
public function doBaz(int $rangeFiveBoth, int $rangeFiveLeft, int $rangeFiveRight): void
{
assertType('int<-4, 6>', $rangeFiveBoth + 1);
assertType('int<-4, max>', $rangeFiveLeft + 1);
assertType('int<-6, max>', $rangeFiveLeft - 1);
assertType('int<min, 6>', $rangeFiveRight + 1);
assertType('int<min, 4>', $rangeFiveRight - 1);
assertType('int', $rangeFiveLeft + $rangeFiveRight);
assertType('int', $rangeFiveLeft - $rangeFiveRight);
assertType('int', $rangeFiveRight + $rangeFiveLeft);
assertType('int', $rangeFiveRight - $rangeFiveLeft);
assertType('int<-10, 10>', $rangeFiveBoth + $rangeFiveBoth);
assertType('int<-10, 10>', $rangeFiveBoth - $rangeFiveBoth);
assertType('int<-10, max>', $rangeFiveBoth + $rangeFiveLeft);
assertType('int', $rangeFiveBoth - $rangeFiveLeft);
assertType('int<min, 10>', $rangeFiveBoth + $rangeFiveRight);
assertType('int<-10, max>', $rangeFiveBoth - $rangeFiveRight);
assertType('int<-10, max>', $rangeFiveLeft + $rangeFiveBoth);
assertType('int<-10, max>', $rangeFiveLeft - $rangeFiveBoth);
assertType('int<min, 10>', $rangeFiveRight + $rangeFiveBoth);
assertType('int<min, 10>', $rangeFiveRight - $rangeFiveBoth);
}
public function doLorem($a, $b): void
{
$nullsReverse = rand(0, 1) ? 1 : -1;
$comparison = $a <=> $b;
assertType('int<-1, 1>', $comparison);
assertType('-1|1', $nullsReverse);
assertType('int<-1, 1>', $comparison * $nullsReverse);
}
public function doIpsum(int $newLevel): void
{
$min = min(30, $newLevel);
assertType('int<min, 30>', $min);
$minDivFive = $min / 5;
assertType('float|int<min, 6>', $minDivFive);
$volume = 0x10000000 * $minDivFive;
assertType('float|int<min, 1610612736>', $volume);
}
public function doDolor(int $i): void
{
$chunks = min(200, $i);
assertType('int<min, 200>', $chunks);
$divThirty = $chunks / 30;
assertType('float|int<min, 6>', $divThirty);
assertType('float|int<min, 9>', $divThirty + 3);
}
public function doSit(int $i, int $j): void
{
if ($i < 0) {
return;
}
if ($j < 1) {
return;
}
assertType('int<0, max>', $i);
assertType('int<1, max>', $j);
assertType('int', $i - $j);
}
/**
* @param int<-5, 5> $range
*/
public function multiplyZero(int $i, float $f, $range): void
{
assertType('0', $i * false);
assertType('0.0', $f * false);
assertType('0', $range * false);
assertType('0', $i * '0');
assertType('0.0', $f * '0');
assertType('0', $range * '0');
assertType('0', $i * 0);
assertType('0.0', $f * 0);
assertType('0', $range * 0);
assertType('0', 0 * $i);
assertType('0.0', 0 * $f);
assertType('0', 0 * $range);
$i *= 0;
$f *= 0;
$range *= 0;
assertType('0', $i);
assertType('0.0', $f);
assertType('0', $range);
}
}