| Conditions | 10 |
| Paths | 24 |
| Total Lines | 32 |
| 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 |
||
| 67 | public static function round($value, $places = 0, $type = null) |
||
| 68 | { |
||
| 69 | if (!isset($type)) { |
||
| 70 | $type = Config::inst()->get(self::class, "default_round"); |
||
| 71 | } |
||
| 72 | |||
| 73 | $offset = 0; |
||
| 74 | |||
| 75 | // If we are rounding to decimals get a more granular number. |
||
| 76 | if ($places !== 0 && $type !== self::ROUND_DEFAULT) { |
||
| 77 | if ($type == self::ROUND_DOWN) { |
||
| 78 | $offset = -0.45; |
||
| 79 | } elseif ($type == self::ROUND_UP) { |
||
| 80 | $offset = 0.45; |
||
| 81 | } |
||
| 82 | $offset /= pow(10, $places) + 1; |
||
| 83 | } |
||
| 84 | |||
| 85 | // if we are rounding to whole numbers and forcing up |
||
| 86 | // down, use ceil/floor |
||
| 87 | if ($places == 0 && $type == self::ROUND_UP) { |
||
| 88 | $return = ceil($value); |
||
| 89 | } elseif ($places == 0 && $type == self::ROUND_DOWN) { |
||
| 90 | $return = floor($value); |
||
| 91 | } else { |
||
| 92 | $return = round( |
||
| 93 | $value + $offset, |
||
| 94 | $places |
||
| 95 | ); |
||
| 96 | } |
||
| 97 | |||
| 98 | return $return; |
||
| 99 | } |
||
| 101 |