GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

Timer::time()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 3
Bugs 0 Features 2
Metric Value
c 3
b 0
f 2
dl 0
loc 10
ccs 5
cts 5
cp 1
rs 9.4285
cc 1
eloc 5
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Isswp101\Timer;
4
5
class Timer
6
{
7
    /**
8
     * @var double
9
     */
10
    private $start;
11
12
    /**
13
     * @var double
14
     */
15
    private $time;
16
17
    /**
18
     * @var string
19
     */
20
    private $format;
21
22
    /**
23
     * Create a new Timer instance and start it.
24
     *
25
     * @param string $format
26
     */
27 30
    public function __construct($format = 'H:i:s.ms')
28
    {
29 30
        $this->format($format)->start();
30 30
    }
31
32
    /**
33
     * Set the date format.
34
     *
35
     * @param string $format
36
     * @return $this
37
     */
38 30
    protected function format($format)
39
    {
40 30
        $this->format = $format;
41 30
        return $this;
42
    }
43
44
    /**
45
     * Start the timer.
46
     *
47
     * @return $this
48
     */
49 30
    public function start()
50
    {
51 30
        $this->start = microtime(true);
52 30
        return $this;
53
    }
54
55
    /**
56
     * Stop the timer.
57
     *
58
     * @return $this
59
     */
60 30
    public function stop()
61
    {
62 30
        $time = microtime(true);
63 30
        $this->time += $time - $this->start;
64 30
        $this->start = $time;
65 30
        return $this;
66
    }
67
68
    /**
69
     * Reset the timer.
70
     *
71
     * @return $this
72
     */
73 3
    public function reset()
74
    {
75 3
        $this->time = 0.0;
76 3
        $this->start();
77 3
        return $this;
78
    }
79
80
    /**
81
     * Return measured time.
82
     *
83
     * @return string
84
     */
85 30
    public function time()
86
    {
87 30
        $format = $this->format;
88
89 30
        $format = $this->replace('u', 6, $format);
90
91 30
        $format = $this->replace('ms', 3, $format);
92
93 30
        return gmdate($format, $this->time);
94
    }
95
96
    /**
97
     * Replace the date format with calculated values.
98
     *
99
     * @param string $pattern
100
     * @param int $precision
101
     * @param string $format
102
     * @return string
103
     */
104 30
    private function replace($pattern, $precision, $format)
105
    {
106 30
        $time = round($this->time, $precision);
107 30
        $value = substr(number_format($time - floor($time), $precision), 2);
108 30
        return str_replace($pattern, $value, $format);
109
    }
110
111
    /**
112
     * Stop the timer and return measured time.
113
     *
114
     * @return string
115
     */
116 21
    public function end()
117
    {
118 21
        return $this->stop()->time();
119
    }
120
}
121