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 |
||
9 | class GenderizeClientTest extends TestCase |
||
10 | { |
||
11 | /** |
||
12 | * @var \Pixelpeter\Genderize\GenderizeClient |
||
13 | */ |
||
14 | protected $client; |
||
15 | |||
16 | /** |
||
17 | * @var \Unirest\Request |
||
18 | */ |
||
19 | protected $request; |
||
20 | |||
21 | /** |
||
22 | * @var \Unirest\Response |
||
23 | */ |
||
24 | protected $response; |
||
25 | |||
26 | /** |
||
27 | * Set up |
||
28 | */ |
||
29 | public function setUp() |
||
47 | |||
48 | /** |
||
49 | * Name sets names correctly when given as a string |
||
50 | * |
||
51 | * @test |
||
52 | */ |
||
53 | public function name_works_correctly_with_string() |
||
61 | |||
62 | /** |
||
63 | * Name sets names correctly when given as an array |
||
64 | * |
||
65 | * @test |
||
66 | */ |
||
67 | View Code Duplication | public function name_works_correctly_with_arrays() |
|
76 | |||
77 | /** |
||
78 | * Names sets names correctly when given as an array |
||
79 | * |
||
80 | * @test |
||
81 | */ |
||
82 | View Code Duplication | public function names_works_correctly_with_arrays() |
|
91 | |||
92 | /** |
||
93 | * Country sets country correctly |
||
94 | * |
||
95 | * @test |
||
96 | */ |
||
97 | View Code Duplication | public function country_works_correctly() |
|
104 | |||
105 | /** |
||
106 | * Lang set lang correctly |
||
107 | * |
||
108 | * @test |
||
109 | */ |
||
110 | View Code Duplication | public function lang_works_correctly() |
|
117 | |||
118 | /** |
||
119 | * Getting a response works correctly |
||
120 | * |
||
121 | * @test |
||
122 | */ |
||
123 | public function get_works_correctly() |
||
130 | |||
131 | /** |
||
132 | * Name variable is reset after each call to get() |
||
133 | * |
||
134 | * @test |
||
135 | */ |
||
136 | public function name_is_reset_after_each_usage() |
||
151 | |||
152 | /** |
||
153 | * Tear down |
||
154 | */ |
||
155 | public function tearDown() |
||
159 | } |
||
160 |
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.