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 |
||
23 | abstract class ComparatorInterfaceTestCase extends VisiteeInterfaceTestCase |
||
24 | { |
||
25 | /** |
||
26 | * Test class. |
||
27 | */ |
||
28 | public function testClass() |
||
35 | |||
36 | /** |
||
37 | * Test compare. |
||
38 | * |
||
39 | * @param mixed $a |
||
40 | * @param mixed $b |
||
41 | * @param int $expected |
||
42 | * |
||
43 | * @dataProvider compareDataProvider |
||
44 | */ |
||
45 | View Code Duplication | public function testCompare($a, $b, $expected) |
|
61 | |||
62 | /** |
||
63 | * Test reverse method. |
||
64 | * |
||
65 | * @param mixed $a |
||
66 | * @param mixed $b |
||
67 | * @param int $expected |
||
68 | * |
||
69 | * @dataProvider compareDataProvider |
||
70 | */ |
||
71 | public function testReverse($a, $b, $expected) |
||
88 | |||
89 | /** |
||
90 | * Test otherwise method. |
||
91 | * |
||
92 | * @param callable $otherwise |
||
93 | * @param mixed $a |
||
94 | * @param mixed $b |
||
95 | * @param int $expected |
||
96 | * |
||
97 | * @dataProvider otherwiseDataProvider |
||
98 | */ |
||
99 | View Code Duplication | public function testOtherwise(callable $otherwise, $a, $b, $expected) |
|
118 | |||
119 | /** |
||
120 | * @return array |
||
121 | */ |
||
122 | abstract protected function compareDataProvider(); |
||
123 | |||
124 | /** |
||
125 | * @return array |
||
126 | */ |
||
127 | protected function otherwiseDataProvider() |
||
133 | |||
134 | /** |
||
135 | * @return callable |
||
136 | */ |
||
137 | abstract protected function newDefaultOtherwiseComparator(); |
||
138 | |||
139 | /** |
||
140 | * @param ComparatorInterface $comparator |
||
141 | * @param mixed $a |
||
142 | * @param mixed $b |
||
143 | * @param int $expected |
||
144 | */ |
||
145 | protected function comparision(ComparatorInterface $comparator, $a, $b, $expected) |
||
155 | |||
156 | /** |
||
157 | * @param float|int $number |
||
158 | * |
||
159 | * @return int |
||
160 | */ |
||
161 | protected function sign($number) |
||
165 | } |
||
166 |
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.