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 |
||
6 | class ArrayHelperTest extends TestCase |
||
7 | { |
||
8 | /** |
||
9 | * @dataProvider provideTrueScenarios |
||
10 | * @param $array |
||
11 | * @param $keys |
||
12 | */ |
||
13 | public function testHasKey($array, $keys) |
||
20 | |||
21 | /** |
||
22 | * @dataProvider provideFalseScenarios |
||
23 | * @param $array |
||
24 | * @param $keys |
||
25 | */ |
||
26 | public function testFailHasKey($array, $keys) |
||
33 | |||
34 | /** |
||
35 | * @expectedException \InvalidArgumentException |
||
36 | * @dataProvider provideExceptionScenarios |
||
37 | * @param $notArray |
||
38 | */ |
||
39 | public function testHasKeyException($notArray) |
||
44 | |||
45 | /** |
||
46 | * @expectedException \InvalidArgumentException |
||
47 | * @dataProvider provideExceptionScenarios |
||
48 | * @param $notArray |
||
49 | */ |
||
50 | public function testGetValueException($notArray) |
||
55 | |||
56 | /** |
||
57 | * @dataProvider provideValueScenarios |
||
58 | * @param array $array |
||
59 | * @param array $keys |
||
60 | * @param mixed $expectedValue |
||
61 | */ |
||
62 | public function testGetValue($array, $keys, $expectedValue) |
||
70 | |||
71 | /** |
||
72 | * @dataProvider provideValueScenarios |
||
73 | * @param array $array |
||
74 | * @param array $keys |
||
75 | */ |
||
76 | public function testFailGetValue($array, $keys) |
||
81 | |||
82 | /** |
||
83 | * @expectedException \NeedleProject\Common\Exception\NotFoundException |
||
84 | */ |
||
85 | public function testNotFoundGetValue() |
||
90 | |||
91 | /** |
||
92 | * Tied to ::testHasKey |
||
93 | * @return array |
||
94 | */ |
||
95 | public function provideTrueScenarios() |
||
116 | |||
117 | /** |
||
118 | * Tied to ::testFailHasKey |
||
119 | * @return array |
||
120 | */ |
||
121 | public function provideFalseScenarios() |
||
148 | |||
149 | /** |
||
150 | * Tied to ::testException |
||
151 | * @return array |
||
152 | */ |
||
153 | public function provideExceptionScenarios() |
||
165 | |||
166 | /** |
||
167 | * Tied to ::testGetValue |
||
168 | * @return array |
||
169 | */ |
||
170 | View Code Duplication | public function provideValueScenarios() |
|
191 | |||
192 | |||
193 | /** |
||
194 | * Tied to ::testFailGetValue |
||
195 | * @return array |
||
196 | */ |
||
197 | View Code Duplication | public function provideFailValueScenarios() |
|
217 | } |
||
218 |
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.