Conditions | 10 |
Paths | 8 |
Total Lines | 31 |
Code Lines | 18 |
Lines | 0 |
Ratio | 0 % |
Changes | 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 |
||
85 | public function getRateToUse($from, $to) |
||
86 | { |
||
87 | if ($from == $to) return 1; |
||
88 | if ($this->database) { |
||
89 | // Get a currency rate from table |
||
90 | $result = $this->_currencyratesTable->find('all')->where(['from_currency' => $from, 'to_currency' => $to])->first(); |
||
91 | // If currency rate is in table and it doesn't have to be updated |
||
92 | if ($result && $result->modified->wasWithinLast($this->refresh . ' hours')) return $rate = $result->rate; |
||
93 | // If currency rate is in table and it have to be updated |
||
94 | if ($result && !$result->modified->wasWithinLast($this->refresh . ' hours')) { |
||
95 | if ($rate = $this->_getRateFromAPI($from, $to)) { |
||
96 | $result->rate = $rate; |
||
97 | $this->_currencyratesTable->save($result); |
||
98 | } |
||
99 | return $rate; |
||
100 | } |
||
101 | // If currency rate isn't in table |
||
102 | if (!$result) { |
||
103 | if ($rate = $this->_getRateFromAPI($from, $to)) { |
||
104 | $entity = $this->_currencyratesTable->newEntity([ |
||
105 | 'from_currency' => $from, |
||
106 | 'to_currency' => $to, |
||
107 | 'rate' => $rate |
||
108 | ]); |
||
109 | $this->_currencyratesTable->save($entity); |
||
110 | } |
||
111 | return $rate; |
||
112 | } |
||
113 | } |
||
114 | |||
115 | return $this->_getRateFromAPI($from, $to); |
||
116 | } |
||
154 | } |