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 namespace Propaganistas\LaravelIntl; |
||
8 | class Currency extends Intl |
||
9 | { |
||
10 | /** |
||
11 | * @var \CommerceGuys\Intl\Currency\CurrencyRepository |
||
12 | */ |
||
13 | protected $data; |
||
14 | |||
15 | /** |
||
16 | * @var \CommerceGuys\Intl\NumberFormat\NumberFormatRepository |
||
17 | */ |
||
18 | protected $formatData; |
||
19 | |||
20 | /** |
||
21 | * Currency constructor. |
||
22 | * |
||
23 | * @param \CommerceGuys\Intl\Currency\CurrencyRepository $data |
||
24 | * @param \CommerceGuys\Intl\NumberFormat\NumberFormatRepository $formatData |
||
25 | */ |
||
26 | 36 | public function __construct(CurrencyRepository $data, NumberFormatRepository $formatData) |
|
31 | |||
32 | /** |
||
33 | * Get the localized name for the specified currency. |
||
34 | * |
||
35 | * @param string $currencyCode |
||
36 | * @return string |
||
37 | */ |
||
38 | 9 | public function name($currencyCode) |
|
42 | |||
43 | /** |
||
44 | * Get the symbol for the specified currency. |
||
45 | * |
||
46 | * @param string $currencyCode |
||
47 | * @return string |
||
48 | */ |
||
49 | 3 | public function symbol($currencyCode) |
|
53 | |||
54 | /** |
||
55 | * Format an value for the given currency. |
||
56 | * |
||
57 | * @param int|float $value |
||
58 | * @param string $currencyCode |
||
59 | * @return string |
||
60 | */ |
||
61 | 12 | View Code Duplication | public function format($value, $currencyCode) |
69 | |||
70 | /** |
||
71 | * Format an value for the given currency in accounting |
||
72 | * format. |
||
73 | * |
||
74 | * @param int|float $value |
||
75 | * @param string $currencyCode |
||
76 | * @return string |
||
77 | */ |
||
78 | 3 | View Code Duplication | public function formatAccounting($value, $currencyCode) |
86 | |||
87 | /** |
||
88 | * Parse a localized value into native PHP format. |
||
89 | * |
||
90 | * @param string|int|float $value |
||
91 | * @param string $currencyCode |
||
92 | * @return int|float |
||
93 | */ |
||
94 | 3 | View Code Duplication | public function parse($value, $currencyCode) |
102 | |||
103 | /** |
||
104 | * Set the default locale. |
||
105 | * |
||
106 | * @param $locale |
||
107 | * @return $this |
||
108 | */ |
||
109 | 12 | public function setLocale($locale) |
|
117 | |||
118 | /** |
||
119 | * Set the desired locale. |
||
120 | * |
||
121 | * @param $locale |
||
122 | * @return $this |
||
123 | */ |
||
124 | 3 | public function setFallbackLocale($locale) |
|
132 | } |
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.