Conditions | 10 |
Paths | 8 |
Total Lines | 32 |
Lines | 32 |
Ratio | 100 % |
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 | } |
||
117 | |||
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.