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 |
||
8 | View Code Duplication | class Country extends Intl |
|
|
|||
9 | { |
||
10 | /** |
||
11 | * Loaded localized country data. |
||
12 | * |
||
13 | * @var array |
||
14 | */ |
||
15 | protected $data; |
||
16 | |||
17 | /** |
||
18 | * The current locale. |
||
19 | * |
||
20 | * @var string $locale |
||
21 | */ |
||
22 | protected $locale; |
||
23 | |||
24 | /** |
||
25 | * The current locale. |
||
26 | * |
||
27 | * @var string $locale |
||
28 | */ |
||
29 | protected $fallbackLocale; |
||
30 | |||
31 | /** |
||
32 | * Get a localized record by key. |
||
33 | * |
||
34 | * @param string $key |
||
35 | * @return string |
||
36 | */ |
||
37 | 15 | public function get($key) |
|
41 | |||
42 | /** |
||
43 | * Alias of get(). |
||
44 | * |
||
45 | * @param string $key |
||
46 | * @return string |
||
47 | */ |
||
48 | 15 | public function name($key) |
|
52 | |||
53 | /** |
||
54 | * Get all localized records. |
||
55 | * |
||
56 | * @return array |
||
57 | */ |
||
58 | 18 | public function all() |
|
62 | |||
63 | /** |
||
64 | * Get the current locale. |
||
65 | * |
||
66 | * @return string |
||
67 | */ |
||
68 | 21 | public function getLocale() |
|
72 | |||
73 | /** |
||
74 | * Set the current locale. |
||
75 | * |
||
76 | * @param $locale |
||
77 | * @return $this |
||
78 | */ |
||
79 | 21 | public function setLocale($locale) |
|
87 | |||
88 | /** |
||
89 | * Get the fallback locale. |
||
90 | * |
||
91 | * @return string |
||
92 | */ |
||
93 | 18 | public function getFallbackLocale() |
|
97 | |||
98 | /** |
||
99 | * Set the fallback locale. |
||
100 | * |
||
101 | * @param $locale |
||
102 | * @return $this |
||
103 | */ |
||
104 | 21 | public function setFallbackLocale($locale) |
|
112 | |||
113 | /** |
||
114 | * Load the data for the given locale. |
||
115 | * |
||
116 | * @param string $locale |
||
117 | * @return void |
||
118 | */ |
||
119 | 21 | protected function load($locale) |
|
127 | } |
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.