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 | View Code Duplication | class ForbiddenNegativeBitshiftSniffTest extends BaseSniffTest |
|
17 | { |
||
18 | const TEST_FILE = 'sniff-examples/forbidden_negative_bitshift.php'; |
||
19 | |||
20 | /** |
||
21 | * testForbiddenNegativeBitshift |
||
22 | * |
||
23 | * @group forbiddenNegativeBitshift |
||
24 | * |
||
25 | * @dataProvider dataForbiddenNegativeBitshift |
||
26 | * |
||
27 | * @param int $line Line number where the error should occur. |
||
28 | * |
||
29 | * @return void |
||
30 | */ |
||
31 | public function testForbiddenNegativeBitshift($line) |
||
39 | |||
40 | /** |
||
41 | * dataForbiddenNegativeBitshift |
||
42 | * |
||
43 | * @see testForbiddenNegativeBitshift() |
||
44 | * |
||
45 | * @return array |
||
46 | */ |
||
47 | public function dataForbiddenNegativeBitshift() { |
||
53 | |||
54 | |||
55 | /** |
||
56 | * testNoViolation |
||
57 | * |
||
58 | * @group forbiddenNegativeBitshift |
||
59 | * |
||
60 | * @dataProvider dataNoViolation |
||
61 | * |
||
62 | * @param int $line The line number. |
||
63 | * |
||
64 | * @return void |
||
65 | */ |
||
66 | public function testNoViolation($line) |
||
71 | |||
72 | /** |
||
73 | * Data provider. |
||
74 | * |
||
75 | * @see testNoViolation() |
||
76 | * |
||
77 | * @return array |
||
78 | */ |
||
79 | public function dataNoViolation() |
||
89 | } |
||
90 | |||
91 |
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.