Conditions | 17 |
Paths | 81 |
Total Lines | 72 |
Code Lines | 43 |
Lines | 0 |
Ratio | 0 % |
Tests | 25 |
CRAP Score | 38.198 |
Changes | 1 | ||
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 |
||
18 | 15 | protected function convertTo(float $time, string $unit): float |
|
19 | { |
||
20 | // Convert to seconds. |
||
21 | 15 | switch ($this->unit) { |
|
22 | 15 | case TimeUnit::NANOSECOND: |
|
23 | $time = $time / 10 ** 9; |
||
24 | |||
25 | break; |
||
26 | |||
27 | 15 | case TimeUnit::MICROSECOND: |
|
28 | $time = $time / 10 ** 6; |
||
29 | |||
30 | break; |
||
31 | |||
32 | 15 | case TimeUnit::MILLISECOND: |
|
33 | $time = $time / 10 ** 3; |
||
34 | |||
35 | break; |
||
36 | |||
37 | 15 | case TimeUnit::SECOND: |
|
38 | 15 | $time = $time; |
|
39 | |||
40 | 15 | break; |
|
41 | |||
42 | case TimeUnit::MINUTE: |
||
43 | $time = $time * 60; |
||
44 | |||
45 | break; |
||
46 | |||
47 | case TimeUnit::HOUR: |
||
48 | $time = $time * 60 * 60; |
||
49 | |||
50 | break; |
||
51 | |||
52 | case TimeUnit::DAY: |
||
53 | $time = $time * 60 * 60 * 24; |
||
54 | |||
55 | break; |
||
56 | |||
57 | case TimeUnit::WEEK: |
||
58 | $time = $time * 60 * 60 * 24 * 7; |
||
59 | |||
60 | break; |
||
61 | } |
||
62 | |||
63 | switch ($unit) { |
||
64 | 15 | case TimeUnit::NANOSECOND: |
|
65 | 2 | return $time * 10 ** 9; |
|
66 | |||
67 | 15 | case TimeUnit::MICROSECOND: |
|
68 | 2 | return $time * 10 ** 6; |
|
69 | |||
70 | 15 | case TimeUnit::MILLISECOND: |
|
71 | 13 | return $time * 10 ** 3; |
|
72 | |||
73 | 14 | case TimeUnit::SECOND: |
|
74 | 14 | return $time; |
|
75 | |||
76 | 3 | case TimeUnit::MINUTE: |
|
77 | 2 | return $time / 60; |
|
78 | |||
79 | 3 | case TimeUnit::HOUR: |
|
80 | 2 | return $time / (60 * 60); |
|
81 | |||
82 | 3 | case TimeUnit::DAY: |
|
83 | 2 | return $time / (60 * 60 * 24); |
|
84 | |||
85 | 3 | case TimeUnit::WEEK: |
|
86 | 2 | return $time / (60 * 60 * 24 * 7); |
|
87 | } |
||
88 | |||
89 | 1 | throw new Exception('Unable to convert.'); |
|
90 | } |
||
92 |