Completed
Pull Request — master (#174)
by Juliette
02:35
created

dataWrongMethodVisibility()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 59
Code Lines 40

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 59
rs 9.597
cc 1
eloc 40
nc 1
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
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
    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.1.php');
59
        }
60
        else {
61
            return $this->sniffFile('sniff-examples/nonstatic_magic_methods.2.php');
62
        }
63
    }
64
65
    /**
66
     * testCorrectImplementation
67
     *
68
     * @group MagicMethods
69
     *
70
     * @dataProvider dataCorrectImplementation
71
     *
72
     * @param int $line The line number.
73
     *
74
     * @return void
75
     */
76
    public function testCorrectImplementation($line, $isTrait = false)
77
    {
78
        if ($isTrait === true && self::$recognizesTraits === false) {
79
            $this->markTestSkipped();
80
            return;
81
        }
82
83
        $file = $this->getTestFile($isTrait);
84
        $this->assertNoViolation($file, $line);
85
    }
86
87
    /**
88
     * Data provider.
89
     *
90
     * @see testCorrectImplementation()
91
     *
92
     * @return array
93
     */
94
    public function dataCorrectImplementation()
95
    {
96
        return array(
97
            /*
98
             * nonstatic_magic_methods.1.php
99
             */
100
            // Plain class.
101
            array(5),
102
            array(6),
103
            array(7),
104
            array(8),
105
            array(9),
106
            array(10),
107
            array(11),
108
            array(12),
109
            array(13),
110
            // Normal class.
111
            array(18),
112
            array(19),
113
            array(20),
114
            array(21),
115
            array(22),
116
            array(23),
117
            array(24),
118
            array(25),
119
            array(26),
120
            array(27),
121
122
            // Alternative property order & stacked.
123
            array(58),
124
125
            // Plain interface.
126
            array(71),
127
            array(72),
128
            array(73),
129
            array(74),
130
            array(75),
131
            array(76),
132
            array(77),
133
            array(78),
134
            array(79),
135
            // Normal interface.
136
            array(84),
137
            array(85),
138
            array(86),
139
            array(87),
140
            array(88),
141
            array(89),
142
            array(90),
143
            array(91),
144
            array(92),
145
            array(93),
146
147
            // Normal Soap.
148
            array(121),
149
            array(122),
150
            array(123),
151
            array(124),
152
            array(125),
153
            array(126),
154
            array(127),
155
            array(128),
156
            array(129),
157
            array(130),
158
            array(131),
159
160
            /*
161
             * nonstatic_magic_methods.2.php
162
             */
163
            // Plain trait.
164
            array(5, true),
165
            array(6, true),
166
            array(7, true),
167
            array(8, true),
168
            array(9, true),
169
            array(10, true),
170
            array(11, true),
171
            array(12, true),
172
            array(13, true),
173
            // Normal trait.
174
            array(18, true),
175
            array(19, true),
176
            array(20, true),
177
            array(21, true),
178
            array(22, true),
179
            array(23, true),
180
            array(24, true),
181
            array(25, true),
182
            array(26, true),
183
            array(27, true),
184
185
        );
186
    }
187
188
189
    /**
190
     * testWrongMethodVisibility
191
     *
192
     * @group MagicMethods
193
     *
194
     * @dataProvider dataWrongMethodVisibility
195
     *
196
     * @param string $methodName        Method name.
197
     * @param string $desiredVisibility The visibility the method should have.
198
     * @param string $testVisibility    The visibility the method actually has in the test.
199
     * @param int    $line              The line number.
200
     * @param bool   $isTrait           Whether the test relates to method in a trait.
201
     *
202
     *
203
     * @return void
204
     */
205 View Code Duplication
    public function testWrongMethodVisibility($methodName, $desiredVisibility, $testVisibility, $line, $isTrait = false)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
206
    {
207
        if ($isTrait === true && self::$recognizesTraits === false) {
208
            $this->markTestSkipped();
209
            return;
210
        }
211
212
        $file = $this->getTestFile($isTrait);
213
        $this->assertError($file, $line, "Visibility for magic method {$methodName} must be {$desiredVisibility}. Found: {$testVisibility}");
214
    }
215
216
    /**
217
     * Data provider.
218
     *
219
     * @see testWrongMethodVisibility()
220
     *
221
     * @return array
222
     */
223
    public function dataWrongMethodVisibility()
224
    {
225
        return array(
226
            /*
227
             * nonstatic_magic_methods.1.php
228
             */
229
            // Class.
230
            array('__get', 'public', 'private', 32),
231
            array('__set', 'public', 'protected', 33),
232
            array('__isset', 'public', 'private', 34),
233
            array('__unset', 'public', 'protected', 35),
234
            array('__call', 'public', 'private', 36),
235
            array('__callStatic', 'public', 'protected', 37),
236
            array('__sleep', 'public', 'private', 38),
237
            array('__toString', 'public', 'protected', 39),
238
239
            // Alternative property order & stacked.
240
            array('__set', 'public', 'protected', 56),
241
            array('__isset', 'public', 'private', 57),
242
            array('__get', 'public', 'private', 65),
243
244
            // Interface.
245
            array('__get', 'public', 'protected', 98),
246
            array('__set', 'public', 'private', 99),
247
            array('__isset', 'public', 'protected', 100),
248
            array('__unset', 'public', 'private', 101),
249
            array('__call', 'public', 'protected', 102),
250
            array('__callStatic', 'public', 'private', 103),
251
            array('__sleep', 'public', 'protected', 104),
252
            array('__toString', 'public', 'private', 105),
253
254
            // Soap methods.
255
            array('__doRequest', 'public', 'protected', 136),
256
            array('__getFunctions', 'public', 'private', 137),
257
            array('__getLastRequest', 'public', 'protected', 138),
258
            array('__getLastRequestHeaders', 'public', 'private', 139),
259
            array('__getLastResponse', 'public', 'protected', 140),
260
            array('__getLastResponseHeaders', 'public', 'private', 141),
261
            array('__getTypes', 'public', 'protected', 142),
262
            array('__setCookie', 'public', 'private', 143),
263
            array('__setLocation', 'public', 'protected', 144),
264
            array('__setSoapHeaders', 'public', 'private', 145),
265
            array('__soapCall', 'public', 'protected', 146),
266
267
            /*
268
             * nonstatic_magic_methods.2.php
269
             */
270
            // Trait.
271
            array('__get', 'public', 'private', 32, true),
272
            array('__set', 'public', 'protected', 33, true),
273
            array('__isset', 'public', 'private', 34, true),
274
            array('__unset', 'public', 'protected', 35, true),
275
            array('__call', 'public', 'private', 36, true),
276
            array('__callStatic', 'public', 'protected', 37, true),
277
            array('__sleep', 'public', 'private', 38, true),
278
            array('__toString', 'public', 'protected', 39, true),
279
280
        );
281
    }
282
283
284
    /**
285
     * testWrongStaticMethod
286
     *
287
     * @group MagicMethods
288
     *
289
     * @dataProvider dataWrongStaticMethod
290
     *
291
     * @param string $methodName Method name.
292
     * @param int    $line       The line number.
293
     * @param bool   $isTrait    Whether the test relates to a method in a trait.
294
     *
295
     * @return void
296
     */
297 View Code Duplication
    public function testWrongStaticMethod($methodName, $line, $isTrait = false)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
298
    {
299
        if ($isTrait === true && self::$recognizesTraits === false) {
300
            $this->markTestSkipped();
301
            return;
302
        }
303
304
        $file = $this->getTestFile($isTrait);
305
        $this->assertError($file, $line, "Magic method {$methodName} cannot be defined as static.");
306
    }
307
308
    /**
309
     * Data provider.
310
     *
311
     * @see testWrongStaticMethod()
312
     *
313
     * @return array
314
     */
315
    public function dataWrongStaticMethod()
316
    {
317
        return array(
318
            /*
319
             * nonstatic_magic_methods.1.php
320
             */
321
            // Class.
322
            array('__get', 44),
323
            array('__set', 45),
324
            array('__isset', 46),
325
            array('__unset', 47),
326
            array('__call', 48),
327
328
            // Alternative property order & stacked.
329
            array('__get', 55),
330
            array('__set', 56),
331
            array('__isset', 57),
332
            array('__get', 65),
333
334
            // Interface.
335
            array('__get', 110),
336
            array('__set', 111),
337
            array('__isset', 112),
338
            array('__unset', 113),
339
            array('__call', 114),
340
341
            /*
342
             * nonstatic_magic_methods.2.php
343
             */
344
            // Trait.
345
            array('__get', 44, true),
346
            array('__set', 45, true),
347
            array('__isset', 46, true),
348
            array('__unset', 47, true),
349
            array('__call', 48, true),
350
351
        );
352
    }
353
354
355
    /**
356
     * testWrongNonStaticMethod
357
     *
358
     * @group MagicMethods
359
     *
360
     * @dataProvider dataWrongNonStaticMethod
361
     *
362
     * @param string $methodName Method name.
363
     * @param int    $line       The line number.
364
     * @param bool   $isTrait    Whether the test relates to a method in a trait.
365
     *
366
     * @return void
367
     */
368 View Code Duplication
    public function testWrongNonStaticMethod($methodName, $line, $isTrait = false)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
369
    {
370
        if ($isTrait === true && self::$recognizesTraits === false) {
371
            $this->markTestSkipped();
372
            return;
373
        }
374
375
        $file = $this->getTestFile($isTrait);
376
        $this->assertError($file, $line, "Magic method {$methodName} must be defined as static.");
377
    }
378
379
    /**
380
     * Data provider.
381
     *
382
     * @see testWrongNonStaticMethod()
383
     *
384
     * @return array
385
     */
386
    public function dataWrongNonStaticMethod()
387
    {
388
        return array(
389
            /*
390
             * nonstatic_magic_methods.1.php
391
             */
392
            // Class.
393
            array('__callStatic', 49),
394
            array('__set_state', 50),
395
396
            // Interface.
397
            array('__callStatic', 115),
398
            array('__set_state', 116),
399
400
            /*
401
             * nonstatic_magic_methods.2.php
402
             */
403
            // Trait.
404
            array('__callStatic', 49, true),
405
            array('__set_state', 50, true),
406
407
        );
408
    }
409
410
}
411