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
|
|
|
* Whether or not traits will be recognized in PHPCS. |
20
|
|
|
* |
21
|
|
|
* @var bool |
22
|
|
|
*/ |
23
|
|
|
protected static $recognizesTraits = true; |
24
|
|
|
|
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Set up skip condition. |
28
|
|
|
*/ |
29
|
|
View Code Duplication |
public static function setUpBeforeClass() |
|
|
|
|
30
|
|
|
{ |
31
|
|
|
// When using PHPCS 1.x combined with PHP 5.3 or lower, traits are not recognized. |
32
|
|
|
if (version_compare(PHP_CodeSniffer::VERSION, '2.0', '<') && version_compare(phpversion(), '5.4', '<')) { |
33
|
|
|
self::$recognizesTraits = false; |
34
|
|
|
} |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Get the correct test file. |
40
|
|
|
* |
41
|
|
|
* (@internal |
42
|
|
|
* The test file has been split into two: |
43
|
|
|
* - one covering classes and interfaces |
44
|
|
|
* - one covering traits |
45
|
|
|
* |
46
|
|
|
* This is to avoid test failing because PHPCS 1.x gets confused about the scope |
47
|
|
|
* openers/closers when run on PHP 5.3 or lower. |
48
|
|
|
* In a 'normal' situation you won't often find classes, interfaces and traits all |
49
|
|
|
* mixed in one file anyway, so this issue for which this is a work-around, |
50
|
|
|
* should not cause real world issues anyway.}} |
51
|
|
|
* |
52
|
|
|
* @param bool $isTrait Whether to load the class/interface test file or the trait test file. |
53
|
|
|
* |
54
|
|
|
* @return PHP_CodeSniffer_File File object|false |
55
|
|
|
*/ |
56
|
|
|
protected function getTestFile($isTrait) { |
57
|
|
|
if ($isTrait === false) { |
58
|
|
|
return $this->sniffFile('sniff-examples/nonstatic_magic_methods.php'); |
59
|
|
|
} |
60
|
|
|
else { |
61
|
|
|
return $this->sniffFile('sniff-examples/nonstatic_magic_methods_traits.php'); |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* testCorrectImplementation |
67
|
|
|
* |
68
|
|
|
* @group MagicMethods |
69
|
|
|
* |
70
|
|
|
* @dataProvider dataCorrectImplementation |
71
|
|
|
* |
72
|
|
|
* @param int $line The line number. |
73
|
|
|
* @param bool $isTrait Whether to load the class/interface test file or the trait test file. |
74
|
|
|
* |
75
|
|
|
* @return void |
76
|
|
|
*/ |
77
|
|
|
public function testCorrectImplementation($line, $isTrait = false) |
78
|
|
|
{ |
79
|
|
|
if ($isTrait === true && self::$recognizesTraits === false) { |
80
|
|
|
$this->markTestSkipped(); |
81
|
|
|
return; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
$file = $this->getTestFile($isTrait); |
85
|
|
|
$this->assertNoViolation($file, $line); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Data provider. |
90
|
|
|
* |
91
|
|
|
* @see testCorrectImplementation() |
92
|
|
|
* |
93
|
|
|
* @return array |
94
|
|
|
*/ |
95
|
|
|
public function dataCorrectImplementation() |
96
|
|
|
{ |
97
|
|
|
return array( |
98
|
|
|
/* |
99
|
|
|
* nonstatic_magic_methods.php |
100
|
|
|
*/ |
101
|
|
|
// Plain class. |
102
|
|
|
array(5), |
103
|
|
|
array(6), |
104
|
|
|
array(7), |
105
|
|
|
array(8), |
106
|
|
|
array(9), |
107
|
|
|
array(10), |
108
|
|
|
array(11), |
109
|
|
|
array(12), |
110
|
|
|
array(13), |
111
|
|
|
// Normal class. |
112
|
|
|
array(18), |
113
|
|
|
array(19), |
114
|
|
|
array(20), |
115
|
|
|
array(21), |
116
|
|
|
array(22), |
117
|
|
|
array(23), |
118
|
|
|
array(24), |
119
|
|
|
array(25), |
120
|
|
|
array(26), |
121
|
|
|
array(27), |
122
|
|
|
|
123
|
|
|
// Alternative property order & stacked. |
124
|
|
|
array(58), |
125
|
|
|
|
126
|
|
|
// Plain interface. |
127
|
|
|
array(71), |
128
|
|
|
array(72), |
129
|
|
|
array(73), |
130
|
|
|
array(74), |
131
|
|
|
array(75), |
132
|
|
|
array(76), |
133
|
|
|
array(77), |
134
|
|
|
array(78), |
135
|
|
|
array(79), |
136
|
|
|
// Normal interface. |
137
|
|
|
array(84), |
138
|
|
|
array(85), |
139
|
|
|
array(86), |
140
|
|
|
array(87), |
141
|
|
|
array(88), |
142
|
|
|
array(89), |
143
|
|
|
array(90), |
144
|
|
|
array(91), |
145
|
|
|
array(92), |
146
|
|
|
array(93), |
147
|
|
|
|
148
|
|
|
/* |
149
|
|
|
* nonstatic_magic_methods_traits.php |
150
|
|
|
*/ |
151
|
|
|
// Plain trait. |
152
|
|
|
array(5, true), |
153
|
|
|
array(6, true), |
154
|
|
|
array(7, true), |
155
|
|
|
array(8, true), |
156
|
|
|
array(9, true), |
157
|
|
|
array(10, true), |
158
|
|
|
array(11, true), |
159
|
|
|
array(12, true), |
160
|
|
|
array(13, true), |
161
|
|
|
// Normal trait. |
162
|
|
|
array(18, true), |
163
|
|
|
array(19, true), |
164
|
|
|
array(20, true), |
165
|
|
|
array(21, true), |
166
|
|
|
array(22, true), |
167
|
|
|
array(23, true), |
168
|
|
|
array(24, true), |
169
|
|
|
array(25, true), |
170
|
|
|
array(26, true), |
171
|
|
|
array(27, true), |
172
|
|
|
|
173
|
|
|
); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* testWrongMethodVisibility |
179
|
|
|
* |
180
|
|
|
* @group MagicMethods |
181
|
|
|
* |
182
|
|
|
* @dataProvider dataWrongMethodVisibility |
183
|
|
|
* |
184
|
|
|
* @param string $methodName Method name. |
185
|
|
|
* @param string $desiredVisibility The visibility the method should have. |
186
|
|
|
* @param string $testVisibility The visibility the method actually has in the test. |
187
|
|
|
* @param int $line The line number. |
188
|
|
|
* @param bool $isTrait Whether the test relates to method in a trait. |
189
|
|
|
* |
190
|
|
|
* @return void |
191
|
|
|
*/ |
192
|
|
View Code Duplication |
public function testWrongMethodVisibility($methodName, $desiredVisibility, $testVisibility, $line, $isTrait = false) |
|
|
|
|
193
|
|
|
{ |
194
|
|
|
if ($isTrait === true && self::$recognizesTraits === false) { |
195
|
|
|
$this->markTestSkipped(); |
196
|
|
|
return; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
$file = $this->getTestFile($isTrait); |
200
|
|
|
$this->assertError($file, $line, "Visibility for magic method {$methodName} must be {$desiredVisibility}. Found: {$testVisibility}"); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* Data provider. |
205
|
|
|
* |
206
|
|
|
* @see testWrongMethodVisibility() |
207
|
|
|
* |
208
|
|
|
* @return array |
209
|
|
|
*/ |
210
|
|
|
public function dataWrongMethodVisibility() |
211
|
|
|
{ |
212
|
|
|
return array( |
213
|
|
|
/* |
214
|
|
|
* nonstatic_magic_methods.php |
215
|
|
|
*/ |
216
|
|
|
// Class. |
217
|
|
|
array('__get', 'public', 'private', 32), |
218
|
|
|
array('__set', 'public', 'protected', 33), |
219
|
|
|
array('__isset', 'public', 'private', 34), |
220
|
|
|
array('__unset', 'public', 'protected', 35), |
221
|
|
|
array('__call', 'public', 'private', 36), |
222
|
|
|
array('__callStatic', 'public', 'protected', 37), |
223
|
|
|
array('__sleep', 'public', 'private', 38), |
224
|
|
|
array('__toString', 'public', 'protected', 39), |
225
|
|
|
|
226
|
|
|
// Alternative property order & stacked. |
227
|
|
|
array('__set', 'public', 'protected', 56), |
228
|
|
|
array('__isset', 'public', 'private', 57), |
229
|
|
|
array('__get', 'public', 'private', 65), |
230
|
|
|
|
231
|
|
|
// Interface. |
232
|
|
|
array('__get', 'public', 'protected', 98), |
233
|
|
|
array('__set', 'public', 'private', 99), |
234
|
|
|
array('__isset', 'public', 'protected', 100), |
235
|
|
|
array('__unset', 'public', 'private', 101), |
236
|
|
|
array('__call', 'public', 'protected', 102), |
237
|
|
|
array('__callStatic', 'public', 'private', 103), |
238
|
|
|
array('__sleep', 'public', 'protected', 104), |
239
|
|
|
array('__toString', 'public', 'private', 105), |
240
|
|
|
|
241
|
|
|
/* |
242
|
|
|
* nonstatic_magic_methods_traits.php |
243
|
|
|
*/ |
244
|
|
|
// Trait. |
245
|
|
|
array('__get', 'public', 'private', 32, true), |
246
|
|
|
array('__set', 'public', 'protected', 33, true), |
247
|
|
|
array('__isset', 'public', 'private', 34, true), |
248
|
|
|
array('__unset', 'public', 'protected', 35, true), |
249
|
|
|
array('__call', 'public', 'private', 36, true), |
250
|
|
|
array('__callStatic', 'public', 'protected', 37, true), |
251
|
|
|
array('__sleep', 'public', 'private', 38, true), |
252
|
|
|
array('__toString', 'public', 'protected', 39, true), |
253
|
|
|
|
254
|
|
|
); |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
|
258
|
|
|
/** |
259
|
|
|
* testWrongStaticMethod |
260
|
|
|
* |
261
|
|
|
* @group MagicMethods |
262
|
|
|
* |
263
|
|
|
* @dataProvider dataWrongStaticMethod |
264
|
|
|
* |
265
|
|
|
* @param string $methodName Method name. |
266
|
|
|
* @param int $line The line number. |
267
|
|
|
* @param bool $isTrait Whether the test relates to a method in a trait. |
268
|
|
|
* |
269
|
|
|
* @return void |
270
|
|
|
*/ |
271
|
|
View Code Duplication |
public function testWrongStaticMethod($methodName, $line, $isTrait = false) |
|
|
|
|
272
|
|
|
{ |
273
|
|
|
if ($isTrait === true && self::$recognizesTraits === false) { |
274
|
|
|
$this->markTestSkipped(); |
275
|
|
|
return; |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
$file = $this->getTestFile($isTrait); |
279
|
|
|
$this->assertError($file, $line, "Magic method {$methodName} cannot be defined as static."); |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
/** |
283
|
|
|
* Data provider. |
284
|
|
|
* |
285
|
|
|
* @see testWrongStaticMethod() |
286
|
|
|
* |
287
|
|
|
* @return array |
288
|
|
|
*/ |
289
|
|
|
public function dataWrongStaticMethod() |
290
|
|
|
{ |
291
|
|
|
return array( |
292
|
|
|
/* |
293
|
|
|
* nonstatic_magic_methods.php |
294
|
|
|
*/ |
295
|
|
|
// Class. |
296
|
|
|
array('__get', 44), |
297
|
|
|
array('__set', 45), |
298
|
|
|
array('__isset', 46), |
299
|
|
|
array('__unset', 47), |
300
|
|
|
array('__call', 48), |
301
|
|
|
|
302
|
|
|
// Alternative property order & stacked. |
303
|
|
|
array('__get', 55), |
304
|
|
|
array('__set', 56), |
305
|
|
|
array('__isset', 57), |
306
|
|
|
array('__get', 65), |
307
|
|
|
|
308
|
|
|
// Interface. |
309
|
|
|
array('__get', 110), |
310
|
|
|
array('__set', 111), |
311
|
|
|
array('__isset', 112), |
312
|
|
|
array('__unset', 113), |
313
|
|
|
array('__call', 114), |
314
|
|
|
|
315
|
|
|
/* |
316
|
|
|
* nonstatic_magic_methods_traits.php |
317
|
|
|
*/ |
318
|
|
|
// Trait. |
319
|
|
|
array('__get', 44, true), |
320
|
|
|
array('__set', 45, true), |
321
|
|
|
array('__isset', 46, true), |
322
|
|
|
array('__unset', 47, true), |
323
|
|
|
array('__call', 48, true), |
324
|
|
|
|
325
|
|
|
); |
326
|
|
|
} |
327
|
|
|
|
328
|
|
|
|
329
|
|
|
/** |
330
|
|
|
* testWrongNonStaticMethod |
331
|
|
|
* |
332
|
|
|
* @group MagicMethods |
333
|
|
|
* |
334
|
|
|
* @dataProvider dataWrongNonStaticMethod |
335
|
|
|
* |
336
|
|
|
* @param string $methodName Method name. |
337
|
|
|
* @param int $line The line number. |
338
|
|
|
* @param bool $isTrait Whether the test relates to a method in a trait. |
339
|
|
|
* |
340
|
|
|
* @return void |
341
|
|
|
*/ |
342
|
|
View Code Duplication |
public function testWrongNonStaticMethod($methodName, $line, $isTrait = false) |
|
|
|
|
343
|
|
|
{ |
344
|
|
|
if ($isTrait === true && self::$recognizesTraits === false) { |
345
|
|
|
$this->markTestSkipped(); |
346
|
|
|
return; |
347
|
|
|
} |
348
|
|
|
|
349
|
|
|
$file = $this->getTestFile($isTrait); |
350
|
|
|
$this->assertError($file, $line, "Magic method {$methodName} must be defined as static."); |
351
|
|
|
} |
352
|
|
|
|
353
|
|
|
/** |
354
|
|
|
* Data provider. |
355
|
|
|
* |
356
|
|
|
* @see testWrongNonStaticMethod() |
357
|
|
|
* |
358
|
|
|
* @return array |
359
|
|
|
*/ |
360
|
|
|
public function dataWrongNonStaticMethod() |
361
|
|
|
{ |
362
|
|
|
return array( |
363
|
|
|
/* |
364
|
|
|
* nonstatic_magic_methods.php |
365
|
|
|
*/ |
366
|
|
|
// Class. |
367
|
|
|
array('__callStatic', 49), |
368
|
|
|
array('__set_state', 50), |
369
|
|
|
|
370
|
|
|
// Interface. |
371
|
|
|
array('__callStatic', 115), |
372
|
|
|
array('__set_state', 116), |
373
|
|
|
|
374
|
|
|
/* |
375
|
|
|
* nonstatic_magic_methods_traits.php |
376
|
|
|
*/ |
377
|
|
|
// Trait. |
378
|
|
|
array('__callStatic', 49, true), |
379
|
|
|
array('__set_state', 50, true), |
380
|
|
|
|
381
|
|
|
); |
382
|
|
|
} |
383
|
|
|
|
384
|
|
|
} |
385
|
|
|
|
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.