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 |
||
11 | class Currency extends Intl |
||
12 | { |
||
13 | /** |
||
14 | * Loaded localized currency data. |
||
15 | * |
||
16 | * @var array |
||
17 | */ |
||
18 | protected $data; |
||
19 | |||
20 | /** |
||
21 | * Array of localized currency formatters. |
||
22 | * |
||
23 | * @var array |
||
24 | */ |
||
25 | protected $formatters; |
||
26 | |||
27 | /** |
||
28 | * The current locale. |
||
29 | * |
||
30 | * @var string $locale |
||
31 | */ |
||
32 | protected $locale; |
||
33 | |||
34 | /** |
||
35 | * The current locale. |
||
36 | * |
||
37 | * @var string $locale |
||
38 | */ |
||
39 | protected $fallbackLocale; |
||
40 | |||
41 | /** |
||
42 | * Get a localized record by key. |
||
43 | * |
||
44 | * @param string $currencyCode |
||
45 | * @return string |
||
46 | */ |
||
47 | 15 | public function get($currencyCode) |
|
51 | |||
52 | /** |
||
53 | * Alias of get(). |
||
54 | * |
||
55 | * @param string $currencyCode |
||
56 | * @return string |
||
57 | */ |
||
58 | 12 | public function name($currencyCode) |
|
62 | |||
63 | /** |
||
64 | * Get the symbol of the given currency. |
||
65 | * |
||
66 | * @param string $currencyCode |
||
67 | * @return string |
||
68 | */ |
||
69 | 3 | public function symbol($currencyCode) |
|
73 | |||
74 | /** |
||
75 | * Format a number. |
||
76 | * |
||
77 | * @param string|int|float $number |
||
78 | * @param string $currencyCode |
||
79 | * @param array $options |
||
80 | * @return mixed|string |
||
81 | */ |
||
82 | 12 | public function format($number, $currencyCode, $options = []) |
|
88 | |||
89 | /** |
||
90 | * Format a number. |
||
91 | * |
||
92 | * @param string|int|float $number |
||
93 | * @param string $currencyCode |
||
94 | * @param array $options |
||
95 | * @return mixed|string |
||
96 | */ |
||
97 | 3 | public function formatAccounting($number, $currencyCode, $options = []) |
|
103 | |||
104 | /** |
||
105 | * Parse a localized currency string into a number. |
||
106 | * |
||
107 | * @param string $number |
||
108 | * @param string $currencyCode |
||
109 | * @param array $options |
||
110 | * @return mixed|string |
||
111 | */ |
||
112 | 3 | public function parse($number, $currencyCode, $options = []) |
|
118 | |||
119 | /** |
||
120 | * Get all localized records. |
||
121 | * |
||
122 | * @return array |
||
123 | */ |
||
124 | 3 | public function all() |
|
128 | |||
129 | /** |
||
130 | * Get the current locale. |
||
131 | * |
||
132 | * @return string |
||
133 | */ |
||
134 | 36 | public function getLocale() |
|
138 | |||
139 | /** |
||
140 | * Set the current locale. |
||
141 | * |
||
142 | * @param $locale |
||
143 | * @return $this |
||
144 | */ |
||
145 | 36 | public function setLocale($locale) |
|
153 | |||
154 | /** |
||
155 | * Get the fallback locale. |
||
156 | * |
||
157 | * @return string |
||
158 | */ |
||
159 | 36 | public function getFallbackLocale() |
|
163 | |||
164 | /** |
||
165 | * Set the fallback locale. |
||
166 | * |
||
167 | * @param $locale |
||
168 | * @return $this |
||
169 | */ |
||
170 | 36 | public function setFallbackLocale($locale) |
|
178 | |||
179 | /** |
||
180 | * Load the format repository for the given locale. |
||
181 | * |
||
182 | * @param string $locale |
||
183 | * @return void |
||
184 | */ |
||
185 | 36 | protected function load($locale, $fallbackLocale) |
|
201 | |||
202 | /** |
||
203 | * Get the formatter's key. |
||
204 | * |
||
205 | * @param string|null $locale |
||
206 | * @param string|null $fallbackLocale |
||
207 | * @return string |
||
208 | */ |
||
209 | 36 | View Code Duplication | protected function getLocalesKey($locale = null, $fallbackLocale = null) |
216 | |||
217 | /** |
||
218 | * The current number formatter. |
||
219 | * |
||
220 | * @return \CommerceGuys\Intl\Currency\CurrencyRepository |
||
221 | */ |
||
222 | 21 | protected function data() |
|
226 | |||
227 | /** |
||
228 | * The current number formatter. |
||
229 | * |
||
230 | * @return \CommerceGuys\Intl\Formatter\CurrencyFormatter |
||
231 | */ |
||
232 | 18 | protected function formatter() |
|
236 | |||
237 | /** |
||
238 | * Merges the options array. |
||
239 | * |
||
240 | * @param array $options |
||
241 | * @param array $defaults |
||
242 | * @return array |
||
243 | */ |
||
244 | 18 | protected function mergeOptions(array $options, array $defaults = []) |
|
250 | } |
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.