Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
1 | <?php |
||
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 | public static function setUpBeforeClass() |
||
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) { |
||
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) |
||
86 | |||
87 | /** |
||
88 | * Data provider. |
||
89 | * |
||
90 | * @see testCorrectImplementation() |
||
91 | * |
||
92 | * @return array |
||
93 | */ |
||
94 | public function dataCorrectImplementation() |
||
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) |
|
215 | |||
216 | /** |
||
217 | * Data provider. |
||
218 | * |
||
219 | * @see testWrongMethodVisibility() |
||
220 | * |
||
221 | * @return array |
||
222 | */ |
||
223 | public function dataWrongMethodVisibility() |
||
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) |
|
307 | |||
308 | /** |
||
309 | * Data provider. |
||
310 | * |
||
311 | * @see testWrongStaticMethod() |
||
312 | * |
||
313 | * @return array |
||
314 | */ |
||
315 | public function dataWrongStaticMethod() |
||
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) |
|
378 | |||
379 | /** |
||
380 | * Data provider. |
||
381 | * |
||
382 | * @see testWrongNonStaticMethod() |
||
383 | * |
||
384 | * @return array |
||
385 | */ |
||
386 | public function dataWrongNonStaticMethod() |
||
409 | |||
410 | } |
||
411 |
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.