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 Number extends Intl |
||
12 | { |
||
13 | use WithLocales; |
||
14 | |||
15 | /** |
||
16 | * Array of localized number formatters. |
||
17 | * |
||
18 | * @var array |
||
19 | */ |
||
20 | protected $formatters; |
||
21 | |||
22 | /** |
||
23 | * Format a number. |
||
24 | * |
||
25 | * @param string|int|float $number |
||
26 | * @param array $options |
||
27 | * @return string |
||
28 | */ |
||
29 | 15 | public function format($number, $options = []) |
|
35 | |||
36 | /** |
||
37 | * Format as percentage. |
||
38 | * |
||
39 | * @param string|int|float $number |
||
40 | * @param array $options |
||
41 | * @return string |
||
42 | */ |
||
43 | 3 | public function percent($number, $options = []) |
|
49 | |||
50 | /** |
||
51 | * Parse a localized number into native PHP format. |
||
52 | * |
||
53 | * @param string|int|float $number |
||
54 | * @param array $options |
||
55 | * @return string|false |
||
56 | */ |
||
57 | 3 | public function parse($number, $options = []) |
|
63 | |||
64 | /** |
||
65 | * Get the formatter's key. |
||
66 | * |
||
67 | * @param string $locale |
||
68 | * @param string $fallbackLocale |
||
69 | * @return string |
||
70 | */ |
||
71 | 21 | protected function getLocalesKey($locale, $fallbackLocale) |
|
78 | |||
79 | /** |
||
80 | * The current number formatter. |
||
81 | * |
||
82 | * @return \CommerceGuys\Intl\Formatter\NumberFormatter |
||
83 | */ |
||
84 | 21 | View Code Duplication | protected function formatter() |
97 | |||
98 | /** |
||
99 | * Merges the options array. |
||
100 | * |
||
101 | * @param array $options |
||
102 | * @param array $defaults |
||
103 | * @return array |
||
104 | */ |
||
105 | 21 | protected function mergeOptions(array $options, array $defaults = []) |
|
111 | } |
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.