| Conditions | 15 |
| Paths | 842 |
| Total Lines | 52 |
| Code Lines | 38 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 38 |
| CRAP Score | 15 |
| Changes | 2 | ||
| 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 |
||
| 20 | 146 | public static function toNum(string $val): float|int |
|
| 21 | { |
||
| 22 | 146 | $f = (float) $val; |
|
| 23 | 146 | $i = (int) $val; |
|
| 24 | 146 | if ($i == $f) { |
|
| 25 | 144 | $res = $i; |
|
| 26 | } else { |
||
| 27 | 5 | $res = $f; |
|
| 28 | } |
||
| 29 | |||
| 30 | 146 | if (preg_match('/[^0-9]*$/', strtolower($val), $matches)) { |
|
| 31 | 146 | switch ($matches[0]) { |
|
| 32 | 146 | case 'g': |
|
| 33 | 1 | $res *= 1000; |
|
| 34 | /* no break */ |
||
| 35 | 146 | case 'm': |
|
| 36 | 1 | $res *= 1000; |
|
| 37 | /* no break */ |
||
| 38 | 146 | case 'k': |
|
| 39 | 4 | $res *= 1000; |
|
| 40 | 4 | break; |
|
| 41 | 146 | case 'gb': |
|
| 42 | 1 | $res *= 1024; |
|
| 43 | /* no break */ |
||
| 44 | 146 | case 'mb': |
|
| 45 | 3 | $res *= 1024; |
|
| 46 | /* no break */ |
||
| 47 | 146 | case 'kb': |
|
| 48 | 3 | $res *= 1024; |
|
| 49 | 3 | break; |
|
| 50 | 146 | case 'y': |
|
| 51 | 1 | $res *= 60 * 60 * 24 * 365; |
|
| 52 | 1 | break; |
|
| 53 | 146 | case 'w': |
|
| 54 | 1 | $res *= 7; |
|
| 55 | /* no break */ |
||
| 56 | 146 | case 'd': |
|
| 57 | 1 | $res *= 24; |
|
| 58 | /* no break */ |
||
| 59 | 146 | case 'h': |
|
| 60 | 1 | $res *= 60; |
|
| 61 | /* no break */ |
||
| 62 | 146 | case 'min': |
|
| 63 | 1 | $res *= 60; |
|
| 64 | 1 | break; |
|
| 65 | 146 | case 'ms': |
|
| 66 | 1 | $res /= 1000; |
|
| 67 | 1 | break; |
|
| 68 | } |
||
| 69 | } |
||
| 70 | |||
| 71 | 146 | return $res; |
|
| 72 | } |
||
| 81 |