Conditions | 12 |
Paths | 9 |
Total Lines | 23 |
Code Lines | 15 |
Lines | 0 |
Ratio | 0 % |
Changes | 5 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
54 | public static function convert(float $amount, $from, $to, $date = false, int $decimals = 4) : float |
||
55 | { |
||
56 | if ($from !== $to && $amount > 0) { |
||
57 | $date = new DateTime($date ?: date('Y-m-d')); |
||
58 | $rates = ExchangeRate::actualForDate($date)->orderByDesc('date')->first(); |
||
59 | $rates = data_get($rates, 'rates'); |
||
60 | |||
61 | if (!$rates) { |
||
62 | $rates = self::fetchRates($date); |
||
63 | } |
||
64 | |||
65 | if ($rates && array_key_exists($to, $rates) && array_key_exists($from, $rates) && $rates[$to]['buy'] > 0 && $rates[$from]['sell'] > 0) { |
||
66 | $base = 'TRY'; |
||
67 | if ($from === $base) { |
||
68 | $amount = $amount / (float)$rates[$to]['buy']; |
||
69 | } elseif ($to === $base) { |
||
70 | $amount = $amount * (float)$rates[$from]['sell']; |
||
71 | } else { |
||
72 | $amount = $amount * (float)$rates[$from]['sell'] / (float)$rates[$to]['buy']; |
||
73 | } |
||
74 | } |
||
75 | } |
||
76 | return (float)number_format($amount, $decimals, '.', ''); |
||
77 | } |
||
79 |