AbstractTime::convertTo()   C
last analyzed

Complexity

Conditions 17
Paths 81

Size

Total Lines 72
Code Lines 43

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 25
CRAP Score 38.198

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 17
eloc 43
c 1
b 0
f 0
nc 81
nop 2
dl 0
loc 72
ccs 25
cts 43
cp 0.5814
crap 38.198
rs 5.2166

How to fix   Long Method    Complexity   

Long Method

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:

1
<?php
2
3
/**
4
 * For the full copyright and license information, please view
5
 * the LICENSE file that was distributed with this source code.
6
 */
7
8
declare(strict_types=1);
9
10
namespace loophp\nanobench\Time;
11
12
use Exception;
13
14
abstract class AbstractTime implements TimeInterface
15
{
16
    protected string $unit;
17
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
    }
91
}
92