1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Non Static Magic Sniff test file |
4
|
|
|
* |
5
|
|
|
* @package PHPCompatibility |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Non Static Magic Sniff tests |
11
|
|
|
* |
12
|
|
|
* @uses BaseSniffTest |
13
|
|
|
* @package PHPCompatibility |
14
|
|
|
* @author Jansen Price <[email protected]> |
15
|
|
|
*/ |
16
|
|
|
class NonStaticMagicMethodsSniffTest extends BaseSniffTest |
|
|
|
|
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* Sniffed file |
20
|
|
|
* |
21
|
|
|
* @var PHP_CodeSniffer_File |
22
|
|
|
*/ |
23
|
|
|
protected $_sniffFile; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* setUp |
27
|
|
|
* |
28
|
|
|
* @return void |
29
|
|
|
*/ |
30
|
|
|
public function setUp() |
31
|
|
|
{ |
32
|
|
|
parent::setUp(); |
33
|
|
|
|
34
|
|
|
$this->_sniffFile = $this->sniffFile('sniff-examples/nonstatic_magic_methods.php'); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* testCorrectImplementation |
40
|
|
|
* |
41
|
|
|
* @group MagicMethods |
42
|
|
|
* |
43
|
|
|
* @dataProvider dataCorrectImplementation |
44
|
|
|
* |
45
|
|
|
* @param int $line The line number. |
46
|
|
|
* |
47
|
|
|
* @return void |
48
|
|
|
*/ |
49
|
|
|
public function testCorrectImplementation($line) |
50
|
|
|
{ |
51
|
|
|
$this->assertNoViolation($this->_sniffFile, $line); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Data provider. |
56
|
|
|
* |
57
|
|
|
* @see testCorrectImplementation() |
58
|
|
|
* |
59
|
|
|
* @return array |
60
|
|
|
*/ |
61
|
|
|
public function dataCorrectImplementation() |
62
|
|
|
{ |
63
|
|
|
return array( |
64
|
|
|
// Plain class. |
65
|
|
|
array(5), |
66
|
|
|
array(6), |
67
|
|
|
array(7), |
68
|
|
|
array(8), |
69
|
|
|
array(9), |
70
|
|
|
array(10), |
71
|
|
|
array(11), |
72
|
|
|
array(12), |
73
|
|
|
array(13), |
74
|
|
|
// Normal class. |
75
|
|
|
array(18), |
76
|
|
|
array(19), |
77
|
|
|
array(20), |
78
|
|
|
array(21), |
79
|
|
|
array(22), |
80
|
|
|
array(23), |
81
|
|
|
array(24), |
82
|
|
|
array(25), |
83
|
|
|
array(26), |
84
|
|
|
array(27), |
85
|
|
|
|
86
|
|
|
// Alternative property order & stacked. |
87
|
|
|
array(58), |
88
|
|
|
|
89
|
|
|
// Plain interface. |
90
|
|
|
array(71), |
91
|
|
|
array(72), |
92
|
|
|
array(73), |
93
|
|
|
array(74), |
94
|
|
|
array(75), |
95
|
|
|
array(76), |
96
|
|
|
array(77), |
97
|
|
|
array(78), |
98
|
|
|
array(79), |
99
|
|
|
// Normal interface. |
100
|
|
|
array(84), |
101
|
|
|
array(85), |
102
|
|
|
array(86), |
103
|
|
|
array(87), |
104
|
|
|
array(88), |
105
|
|
|
array(89), |
106
|
|
|
array(90), |
107
|
|
|
array(91), |
108
|
|
|
array(92), |
109
|
|
|
array(93), |
110
|
|
|
|
111
|
|
|
// Plain trait. |
112
|
|
|
array(121), |
113
|
|
|
array(122), |
114
|
|
|
array(123), |
115
|
|
|
array(124), |
116
|
|
|
array(125), |
117
|
|
|
array(126), |
118
|
|
|
array(127), |
119
|
|
|
array(128), |
120
|
|
|
array(129), |
121
|
|
|
// Normal trait. |
122
|
|
|
array(134), |
123
|
|
|
array(135), |
124
|
|
|
array(136), |
125
|
|
|
array(137), |
126
|
|
|
array(138), |
127
|
|
|
array(139), |
128
|
|
|
array(140), |
129
|
|
|
array(141), |
130
|
|
|
array(142), |
131
|
|
|
array(143), |
132
|
|
|
|
133
|
|
|
// Normal Soap. |
134
|
|
|
array(171), |
135
|
|
|
array(172), |
136
|
|
|
array(173), |
137
|
|
|
array(174), |
138
|
|
|
array(175), |
139
|
|
|
array(176), |
140
|
|
|
array(177), |
141
|
|
|
array(178), |
142
|
|
|
array(179), |
143
|
|
|
array(180), |
144
|
|
|
array(181), |
145
|
|
|
); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* testWrongMethodVisibility |
151
|
|
|
* |
152
|
|
|
* @group MagicMethods |
153
|
|
|
* |
154
|
|
|
* @dataProvider dataWrongMethodVisibility |
155
|
|
|
* |
156
|
|
|
* @param int $line The line number. |
157
|
|
|
* |
158
|
|
|
* @return void |
159
|
|
|
*/ |
160
|
|
|
public function testWrongMethodVisibility($methodName, $desiredVisibility, $testVisibility, $line) |
161
|
|
|
{ |
162
|
|
|
$this->assertError($this->_sniffFile, $line, "Visibility for magic method {$methodName} must be {$desiredVisibility}. Found: {$testVisibility}"); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Data provider. |
167
|
|
|
* |
168
|
|
|
* @see testWrongMethodVisibility() |
169
|
|
|
* |
170
|
|
|
* @return array |
171
|
|
|
*/ |
172
|
|
|
public function dataWrongMethodVisibility() |
173
|
|
|
{ |
174
|
|
|
return array( |
175
|
|
|
// Class. |
176
|
|
|
array('__get', 'public', 'private', 32), |
177
|
|
|
array('__set', 'public', 'protected', 33), |
178
|
|
|
array('__isset', 'public', 'private', 34), |
179
|
|
|
array('__unset', 'public', 'protected', 35), |
180
|
|
|
array('__call', 'public', 'private', 36), |
181
|
|
|
array('__callStatic', 'public', 'protected', 37), |
182
|
|
|
array('__sleep', 'public', 'private', 38), |
183
|
|
|
array('__toString', 'public', 'protected', 39), |
184
|
|
|
|
185
|
|
|
// Alternative property order & stacked. |
186
|
|
|
array('__set', 'public', 'protected', 56), |
187
|
|
|
array('__isset', 'public', 'private', 57), |
188
|
|
|
array('__get', 'public', 'private', 65), |
189
|
|
|
|
190
|
|
|
// Interface. |
191
|
|
|
array('__get', 'public', 'protected', 98), |
192
|
|
|
array('__set', 'public', 'private', 99), |
193
|
|
|
array('__isset', 'public', 'protected', 100), |
194
|
|
|
array('__unset', 'public', 'private', 101), |
195
|
|
|
array('__call', 'public', 'protected', 102), |
196
|
|
|
array('__callStatic', 'public', 'private', 103), |
197
|
|
|
array('__sleep', 'public', 'protected', 104), |
198
|
|
|
array('__toString', 'public', 'private', 105), |
199
|
|
|
|
200
|
|
|
// Trait. |
201
|
|
|
array('__get', 'public', 'private', 148), |
202
|
|
|
array('__set', 'public', 'protected', 149), |
203
|
|
|
array('__isset', 'public', 'private', 150), |
204
|
|
|
array('__unset', 'public', 'protected', 151), |
205
|
|
|
array('__call', 'public', 'private', 152), |
206
|
|
|
array('__callStatic', 'public', 'protected', 153), |
207
|
|
|
array('__sleep', 'public', 'private', 154), |
208
|
|
|
array('__toString', 'public', 'protected', 155), |
209
|
|
|
|
210
|
|
|
// Soap methods. |
211
|
|
|
array('__doRequest', 'public', 'protected', 186), |
212
|
|
|
array('__getFunctions', 'public', 'private', 187), |
213
|
|
|
array('__getLastRequest', 'public', 'protected', 188), |
214
|
|
|
array('__getLastRequestHeaders', 'public', 'private', 189), |
215
|
|
|
array('__getLastResponse', 'public', 'protected', 190), |
216
|
|
|
array('__getLastResponseHeaders', 'public', 'private', 191), |
217
|
|
|
array('__getTypes', 'public', 'protected', 192), |
218
|
|
|
array('__setCookie', 'public', 'private', 193), |
219
|
|
|
array('__setLocation', 'public', 'protected', 194), |
220
|
|
|
array('__setSoapHeaders', 'public', 'private', 195), |
221
|
|
|
array('__soapCall', 'public', 'protected', 196), |
222
|
|
|
|
223
|
|
|
); |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* testWrongStaticMethod |
229
|
|
|
* |
230
|
|
|
* @group MagicMethods |
231
|
|
|
* |
232
|
|
|
* @dataProvider dataWrongStaticMethod |
233
|
|
|
* |
234
|
|
|
* @param int $line The line number. |
235
|
|
|
* |
236
|
|
|
* @return void |
237
|
|
|
*/ |
238
|
|
|
public function testWrongStaticMethod($methodName, $line) |
239
|
|
|
{ |
240
|
|
|
$this->assertError($this->_sniffFile, $line, "Magic method {$methodName} cannot be defined as static."); |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
/** |
244
|
|
|
* Data provider. |
245
|
|
|
* |
246
|
|
|
* @see testWrongStaticMethod() |
247
|
|
|
* |
248
|
|
|
* @return array |
249
|
|
|
*/ |
250
|
|
|
public function dataWrongStaticMethod() |
251
|
|
|
{ |
252
|
|
|
return array( |
253
|
|
|
// Class. |
254
|
|
|
array('__get', 44), |
255
|
|
|
array('__set', 45), |
256
|
|
|
array('__isset', 46), |
257
|
|
|
array('__unset', 47), |
258
|
|
|
array('__call', 48), |
259
|
|
|
|
260
|
|
|
// Alternative property order & stacked. |
261
|
|
|
array('__get', 55), |
262
|
|
|
array('__set', 56), |
263
|
|
|
array('__isset', 57), |
264
|
|
|
array('__get', 65), |
265
|
|
|
|
266
|
|
|
// Interface. |
267
|
|
|
array('__get', 110), |
268
|
|
|
array('__set', 111), |
269
|
|
|
array('__isset', 112), |
270
|
|
|
array('__unset', 113), |
271
|
|
|
array('__call', 114), |
272
|
|
|
|
273
|
|
|
// Trait. |
274
|
|
|
array('__get', 160), |
275
|
|
|
array('__set', 161), |
276
|
|
|
array('__isset', 162), |
277
|
|
|
array('__unset', 163), |
278
|
|
|
array('__call', 164), |
279
|
|
|
|
280
|
|
|
); |
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
|
284
|
|
|
/** |
285
|
|
|
* testWrongNonStaticMethod |
286
|
|
|
* |
287
|
|
|
* @group MagicMethods |
288
|
|
|
* |
289
|
|
|
* @dataProvider dataWrongNonStaticMethod |
290
|
|
|
* |
291
|
|
|
* @param int $line The line number. |
292
|
|
|
* |
293
|
|
|
* @return void |
294
|
|
|
*/ |
295
|
|
|
public function testWrongNonStaticMethod($methodName, $line) |
296
|
|
|
{ |
297
|
|
|
$this->assertError($this->_sniffFile, $line, "Magic method {$methodName} must be defined as static."); |
298
|
|
|
} |
299
|
|
|
|
300
|
|
|
/** |
301
|
|
|
* Data provider. |
302
|
|
|
* |
303
|
|
|
* @see testWrongNonStaticMethod() |
304
|
|
|
* |
305
|
|
|
* @return array |
306
|
|
|
*/ |
307
|
|
|
public function dataWrongNonStaticMethod() |
308
|
|
|
{ |
309
|
|
|
return array( |
310
|
|
|
// Class. |
311
|
|
|
array('__callStatic', 49), |
312
|
|
|
array('__set_state', 50), |
313
|
|
|
|
314
|
|
|
// Interface. |
315
|
|
|
array('__callStatic', 115), |
316
|
|
|
array('__set_state', 116), |
317
|
|
|
|
318
|
|
|
// Trait. |
319
|
|
|
array('__callStatic', 165), |
320
|
|
|
array('__set_state', 166), |
321
|
|
|
|
322
|
|
|
); |
323
|
|
|
} |
324
|
|
|
|
325
|
|
|
} |
326
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.