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 |
||
14 | View Code Duplication | class CurrencyConverterHelper extends Helper |
|
|
|||
15 | { |
||
16 | /** |
||
17 | * CurrencyratesTable Object |
||
18 | * @var \Cake\ORM\Table |
||
19 | */ |
||
20 | private $_currencyratesTable; |
||
21 | |||
22 | /** |
||
23 | * Default settings |
||
24 | * |
||
25 | * @var array |
||
26 | */ |
||
27 | protected $_defaultConfig = [ |
||
28 | 'database' => 2, // Use database to store rate |
||
29 | 'refresh' => 24, // Time interval for refreshing rate in database |
||
30 | 'decimal' => 2, // Number of decimal to use for convert number |
||
31 | ]; |
||
32 | |||
33 | /** |
||
34 | * @param array $config |
||
35 | * @return void |
||
36 | */ |
||
37 | public function initialize(array $config = []) { |
||
47 | |||
48 | /** |
||
49 | * Convert method take an amount as first parameter and convert it using $from currency and $to currency. |
||
50 | * |
||
51 | * @param float|string $amount the amount to convert. |
||
52 | * @param string $from currency to convert from |
||
53 | * @param string $to currency to convert to |
||
54 | * @return float $amount converted |
||
55 | */ |
||
56 | public function convert($amount, $from, $to) |
||
63 | |||
64 | /** |
||
65 | * Rate method return the rate of two currencies |
||
66 | * |
||
67 | * @param string $from currency to get the rate from |
||
68 | * @param string $to currency to get the rate to |
||
69 | * @return float|null $rate |
||
70 | */ |
||
71 | public function rate($from, $to) |
||
75 | |||
76 | /** |
||
77 | * getRateToUse return rate to use |
||
78 | * Using $from and $to parameters representing currency to deal with and the configuration settings |
||
79 | * This method save or update currencyrates Table if necesseray too. |
||
80 | * |
||
81 | * @param string $from currency to get the rate from |
||
82 | * @param string $to currency to get the rate to |
||
83 | * @return float|null $rate |
||
84 | */ |
||
85 | public function getRateToUse($from, $to) |
||
117 | |||
118 | /** |
||
119 | * Format float number using configuration |
||
120 | * |
||
121 | * @return floatval |
||
122 | */ |
||
123 | private function _formatConvert($number) |
||
127 | |||
128 | /** |
||
129 | * Call free.currencyconverterapi.com API to get a rate for one currency to an other one currency |
||
130 | * |
||
131 | * @param string $from the currency |
||
132 | * @param string $to the currency |
||
133 | * @return int|null $rate |
||
134 | */ |
||
135 | private function _getRateFromAPI($from, $to) |
||
154 | } |
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.