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 |
||
12 | class Currency extends Intl |
||
13 | { |
||
14 | use WithLocales; |
||
15 | |||
16 | /** |
||
17 | * Loaded localized currency data. |
||
18 | * |
||
19 | * @var array |
||
20 | */ |
||
21 | protected $data; |
||
22 | |||
23 | /** |
||
24 | * Array of localized currency formatters. |
||
25 | * |
||
26 | * @var array |
||
27 | */ |
||
28 | protected $formatters; |
||
29 | |||
30 | /** |
||
31 | * Get a localized record by key. |
||
32 | * |
||
33 | * @param string $currencyCode |
||
34 | * @return string |
||
35 | */ |
||
36 | 15 | public function get($currencyCode) |
|
40 | |||
41 | /** |
||
42 | * Alias of get(). |
||
43 | * |
||
44 | * @param string $currencyCode |
||
45 | * @return string |
||
46 | */ |
||
47 | 12 | public function name($currencyCode) |
|
51 | |||
52 | /** |
||
53 | * Get the symbol of the given currency. |
||
54 | * |
||
55 | * @param string $currencyCode |
||
56 | * @return string |
||
57 | */ |
||
58 | 3 | public function symbol($currencyCode) |
|
62 | |||
63 | /** |
||
64 | * Format a number. |
||
65 | * |
||
66 | * @param string|int|float $number |
||
67 | * @param string $currencyCode |
||
68 | * @param array $options |
||
69 | * @return mixed|string |
||
70 | */ |
||
71 | 12 | public function format($number, $currencyCode, $options = []) |
|
77 | |||
78 | /** |
||
79 | * Format a number. |
||
80 | * |
||
81 | * @param string|int|float $number |
||
82 | * @param string $currencyCode |
||
83 | * @param array $options |
||
84 | * @return mixed|string |
||
85 | */ |
||
86 | 3 | public function formatAccounting($number, $currencyCode, $options = []) |
|
92 | |||
93 | /** |
||
94 | * Parse a localized currency string into a number. |
||
95 | * |
||
96 | * @param string $number |
||
97 | * @param string $currencyCode |
||
98 | * @param array $options |
||
99 | * @return mixed|string |
||
100 | */ |
||
101 | 3 | public function parse($number, $currencyCode, $options = []) |
|
107 | |||
108 | /** |
||
109 | * Get all localized records. |
||
110 | * |
||
111 | * @return array |
||
112 | */ |
||
113 | 3 | public function all() |
|
117 | |||
118 | /** |
||
119 | * Get the formatter's key. |
||
120 | * |
||
121 | * @param string $locale |
||
122 | * @param string $fallbackLocale |
||
123 | * @return string |
||
124 | */ |
||
125 | 33 | protected function getLocalesKey($locale, $fallbackLocale) |
|
132 | |||
133 | /** |
||
134 | * The currency repository. |
||
135 | * |
||
136 | * @return \CommerceGuys\Intl\Currency\CurrencyRepository |
||
137 | */ |
||
138 | 33 | View Code Duplication | protected function data() |
151 | |||
152 | /** |
||
153 | * The current number formatter. |
||
154 | * |
||
155 | * @return \CommerceGuys\Intl\Formatter\CurrencyFormatter |
||
156 | */ |
||
157 | 18 | View Code Duplication | protected function formatter() |
174 | |||
175 | /** |
||
176 | * Merges the options array. |
||
177 | * |
||
178 | * @param array $options |
||
179 | * @param array $defaults |
||
180 | * @return array |
||
181 | */ |
||
182 | 18 | protected function mergeOptions(array $options, array $defaults = []) |
|
188 | } |
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.