forked from phpstan/phpstan-src
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPureFunctionRuleTest.php
More file actions
222 lines (209 loc) · 5.84 KB
/
PureFunctionRuleTest.php
File metadata and controls
222 lines (209 loc) · 5.84 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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
<?php declare(strict_types = 1);
namespace PHPStan\Rules\Pure;
use PHPStan\Rules\Rule;
use PHPStan\Testing\RuleTestCase;
use PHPUnit\Framework\Attributes\RequiresPhp;
/**
* @extends RuleTestCase<PureFunctionRule>
*/
class PureFunctionRuleTest extends RuleTestCase
{
public function getRule(): Rule
{
return new PureFunctionRule(new FunctionPurityCheck());
}
public function testRule(): void
{
$this->analyse([__DIR__ . '/data/pure-function.php'], [
[
'Function PureFunction\doFoo() is marked as pure but parameter $p is passed by reference.',
8,
],
[
'Impure echo in pure function PureFunction\doFoo().',
10,
],
[
'Function PureFunction\doFoo2() is marked as pure but returns void.',
16,
],
[
'Impure exit in pure function PureFunction\doFoo2().',
18,
],
[
'Impure property assignment in pure function PureFunction\doFoo3().',
26,
],
[
'Possibly impure call to a callable in pure function PureFunction\testThese().',
60,
],
[
'Possibly impure call to a callable in pure function PureFunction\testThese().',
61,
],
[
'Impure call to function PureFunction\impureFunction() in pure function PureFunction\testThese().',
63,
],
[
'Impure call to function PureFunction\voidFunction() in pure function PureFunction\testThese().',
64,
],
[
'Possibly impure call to function PureFunction\possiblyImpureFunction() in pure function PureFunction\testThese().',
65,
],
[
'Possibly impure call to unknown function in pure function PureFunction\testThese().',
66,
],
[
'Function PureFunction\actuallyPure() is marked as impure but does not have any side effects.',
72,
],
[
'Function PureFunction\emptyVoidFunction() returns void but does not have any side effects.',
84,
],
[
'Impure access to superglobal variable in pure function PureFunction\pureButAccessSuperGlobal().',
102,
],
[
'Impure access to superglobal variable in pure function PureFunction\pureButAccessSuperGlobal().',
103,
],
[
'Impure access to superglobal variable in pure function PureFunction\pureButAccessSuperGlobal().',
105,
],
[
'Impure global variable in pure function PureFunction\functionWithGlobal().',
118,
],
[
'Impure static variable in pure function PureFunction\functionWithStaticVariable().',
128,
],
[
'Possibly impure call to a Closure in pure function PureFunction\callsClosures().',
139,
],
[
'Possibly impure call to a Closure in pure function PureFunction\callsClosures().',
140,
],
[
'Impure output between PHP opening and closing tags in pure function PureFunction\justContainsInlineHtml().',
160,
],
[
'Impure call to function array_push() in pure function PureFunction\bug13288().',
171,
],
[
'Impure call to function array_push() in pure function PureFunction\bug13288().',
175,
],
[
'Impure call to function array_push() in pure function PureFunction\bug13288().',
182,
],
[
'Impure exit in pure function PureFunction\bug13288b().',
200,
],
[
'Impure exit in pure function PureFunction\bug13288c().',
217,
],
[
'Impure exit in pure function PureFunction\bug13288d().',
230,
],
]);
}
#[RequiresPhp('>= 8.1.0')]
public function testFirstClassCallable(): void
{
$this->analyse([__DIR__ . '/data/first-class-callable-pure-function.php'], [
[
'Impure call to method FirstClassCallablePureFunction\Foo::impureFunction() in pure function FirstClassCallablePureFunction\testThese().',
61,
],
[
'Impure call to method FirstClassCallablePureFunction\Foo::voidFunction() in pure function FirstClassCallablePureFunction\testThese().',
64,
],
[
'Impure call to function FirstClassCallablePureFunction\impureFunction() in pure function FirstClassCallablePureFunction\testThese().',
70,
],
[
'Impure call to function FirstClassCallablePureFunction\voidFunction() in pure function FirstClassCallablePureFunction\testThese().',
73,
],
[
'Impure call to function FirstClassCallablePureFunction\voidFunction() in pure function FirstClassCallablePureFunction\testThese().',
75,
],
[
'Impure call to function FirstClassCallablePureFunction\impureFunction() in pure function FirstClassCallablePureFunction\testThese().',
81,
],
[
'Impure call to function FirstClassCallablePureFunction\voidFunction() in pure function FirstClassCallablePureFunction\testThese().',
84,
],
[
'Impure call to method FirstClassCallablePureFunction\Foo::impureFunction() in pure function FirstClassCallablePureFunction\testThese().',
90,
],
[
'Impure call to method FirstClassCallablePureFunction\Foo::voidFunction() in pure function FirstClassCallablePureFunction\testThese().',
93,
],
[
'Possibly impure call to a callable in pure function FirstClassCallablePureFunction\callCallbackImmediately().',
102,
],
]);
}
public function testBug11361(): void
{
$this->analyse([__DIR__ . '/data/bug-11361-pure.php'], [
[
'Impure call to a Closure with by-ref parameter in pure function Bug11361Pure\foo().',
14,
],
]);
}
public function testBug12224(): void
{
$this->analyse([__DIR__ . '/data/bug-12224.php'], [
[
'Function PHPStan\Rules\Pure\data\pureWithThrowsVoid() is marked as pure but returns void.',
18,
],
]);
}
#[RequiresPhp('>= 8.1.0')]
public function testBug13201(): void
{
$this->analyse([__DIR__ . '/data/bug-13201.php'], []);
}
public function testBug12119(): void
{
$this->analyse([__DIR__ . '/data/bug-12119.php'], []);
}
public function testBug14504(): void
{
$this->analyse([__DIR__ . '/data/bug-14504.php'], []);
}
public function testBug14511(): void
{
$this->analyse([__DIR__ . '/data/bug-14511.php'], []);
}
}