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 | View Code Duplication | class Country extends Intl |
|
|
|||
10 | { |
||
11 | use WithLocales; |
||
12 | |||
13 | /** |
||
14 | * Loaded localized country data. |
||
15 | * |
||
16 | * @var array |
||
17 | */ |
||
18 | protected $data; |
||
19 | |||
20 | /** |
||
21 | * Get a localized record by key. |
||
22 | * |
||
23 | * @param string $key |
||
24 | * @return string |
||
25 | */ |
||
26 | 15 | public function get($key) |
|
30 | |||
31 | /** |
||
32 | * Alias of get(). |
||
33 | * |
||
34 | * @param string $key |
||
35 | * @return string |
||
36 | */ |
||
37 | 15 | public function name($key) |
|
41 | |||
42 | /** |
||
43 | * Get all localized records. |
||
44 | * |
||
45 | * @return array |
||
46 | */ |
||
47 | 18 | public function all() |
|
54 | |||
55 | /** |
||
56 | * Get the data for the given locale. |
||
57 | * |
||
58 | * @param string $locale |
||
59 | * @return array |
||
60 | */ |
||
61 | 18 | protected function data($locale) |
|
71 | } |
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.