forked from phpstan/phpstan-src
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbug-14550.php
More file actions
160 lines (135 loc) · 4.1 KB
/
bug-14550.php
File metadata and controls
160 lines (135 loc) · 4.1 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
148
149
150
151
152
153
154
155
156
157
158
159
160
<?php // lint >= 8.1
declare(strict_types = 1);
namespace Bug14550;
use function PHPStan\Testing\assertType;
// Standalone assignments trigger TypeSpecifier via NodeScopeResolver null-context call
function testArrayKeyFirstAssign(): void
{
$fn = array_key_first(...);
assertType('Closure(array): (int|string|null)', $fn);
}
function testArrayKeyLastAssign(): void
{
$fn = array_key_last(...);
assertType('Closure(array): (int|string|null)', $fn);
}
function testArrayRandAssign(): void
{
$fn = array_rand(...);
assertType('(Closure(non-empty-array): (int|string))|(Closure(non-empty-array, int<1, max>): (array<int, int|string>|int|string))', $fn);
}
function testCountMinusOneAssign(): void
{
$idx = count(...) - 1;
assertType('Closure(array|Countable, 0|1=): int<0, max>', count(...));
}
// array_search guard needs true context, so it must be in a condition
function testArraySearchInCondition(): void
{
if ($key = array_search(...)) {
assertType('Closure(mixed, array, bool=): (int|string|false)', $key);
}
}
// Comparison guards in TypeSpecifier (Smaller/SmallerOrEqual)
function testCountInComparisons(): void
{
if (count(...) < 1) {}
if (0 < count(...)) {}
assertType('Closure(array|Countable, 0|1=): int<0, max>', count(...));
}
function testSizeofInComparisons(): void
{
if (sizeof(...) < 1) {}
if (0 < sizeof(...)) {}
assertType('Closure(array|Countable, int=): int', sizeof(...));
}
function testCountMinusOneInComparison(): void
{
$i = 0;
if ($i < count(...) - 1) {}
assertType('Closure(array|Countable, 0|1=): int<0, max>', count(...));
}
function testStrlenInComparisons(): void
{
if (strlen(...) < 1) {}
if (0 < strlen(...)) {}
assertType('Closure(string): int<0, max>', strlen(...));
}
function testMbStrlenInComparisons(): void
{
if (mb_strlen(...) < 1) {}
if (0 < mb_strlen(...)) {}
assertType('Closure(string, string|null=): int<0, max>', mb_strlen(...));
}
function testPregMatchInComparisons(): void
{
if (preg_match(...) < 1) {}
if (0 < preg_match(...)) {}
assertType('Closure(string, string, array<string>|null=, TFlags=, int=): (0|1|false)', preg_match(...));
}
// Identical/NotIdentical guards in resolveNormalizedIdentical
function testCountIdentical(): void
{
if (count(...) === 0) {}
assertType('Closure(array|Countable, 0|1=): int<0, max>', count(...));
}
function testStrlenIdentical(): void
{
if (strlen(...) === 0) {}
if (mb_strlen(...) === 0) {}
assertType('Closure(string): int<0, max>', strlen(...));
}
function testArrayKeyFirstNullComparison(): void
{
if (array_key_first(...) !== null) {}
if (array_key_last(...) !== null) {}
assertType('Closure(array): (int|string|null)', array_key_first(...));
}
function testGetClassIdentical(): void
{
if (get_class(...) === 'stdClass') {}
if (get_debug_type(...) === 'string') {}
assertType('Closure(object=): class-string', get_class(...));
}
function testStringFuncIdentical(): void
{
if (strtolower(...) === 'test') {}
assertType('Closure(string): lowercase-string', strtolower(...));
}
// String equality guards in specifyTypesForConstantStringBinaryExpression
function testGettypeEquality(): void
{
if (gettype(...) === 'string') {}
if (gettype(...) == 'string') {}
assertType('Closure(mixed): string', gettype(...));
}
function testGetClassEquality(): void
{
if (get_class(...) == 'stdClass') {}
if (get_debug_type(...) == 'string') {}
assertType('Closure(object=): class-string', get_class(...));
}
function testGetParentClassEquality(): void
{
if (get_parent_class(...) === 'stdClass') {}
assertType('Closure(object|string=): (class-string|false)', get_parent_class(...));
}
function testTrimEquality(): void
{
if (trim(...) !== '') {}
if (ltrim(...) !== '') {}
if (rtrim(...) !== '') {}
assertType('Closure(string, string=): string', trim(...));
}
// NodeScopeResolver guards
function testArrayKeysInForeach(): void
{
foreach (array_keys(...) as $key) {}
assertType('Closure(array, mixed=, bool=): list<int|string>', array_keys(...));
}
function testCountInForLoop(): void
{
for ($i = 0; $i < count(...); $i++) {}
for ($i = 0; count(...) > $i; $i++) {}
assertType('Closure(array|Countable, 0|1=): int<0, max>', count(...));
}