AbstractTime   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Test Coverage

Coverage 58.14%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 45
c 1
b 0
f 0
dl 0
loc 76
ccs 25
cts 43
cp 0.5814
rs 10
wmc 17

1 Method

Rating   Name   Duplication   Size   Complexity  
C convertTo() 0 72 17
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