Completed
Push — master ( cc971e...223785 )
by Miloš
06:18
created

Stopwatch::getElapsed()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
/**
4
 * This file is part of the Stopwatch package.
5
 *
6
 * Copyright (c) Miloš Đurić <[email protected]>
7
 *
8
 * For full copyright and license information, please refer to the LICENSE file,
9
 * located at the package root folder.
10
 */
11
12
namespace Laganica\Stopwatch;
13
14
/**
15
 * Class Stopwatch
16
 *
17
 * @package Laganica\Stopwatch
18
 */
19
final class Stopwatch
20
{
21
    /**
22
     * @var float
23
     */
24
    private $time;
25
26
    /**
27
     * @var float
28
     */
29
    private $elapsed;
30
31
    /**
32
     * @var bool
33
     */
34
    private $isRunning;
35
36 5
    private function __construct()
37
    {
38 5
        $this->init();
39 5
    }
40
41
    /**
42
     * @return Stopwatch
43
     */
44 5
    public static function createNew(): self
45
    {
46 5
        return new self();
47
    }
48
49
    /**
50
     * Starts or resumes measurement.
51
     *
52 7
     * @return void
53
     */
54 7
    public function start(): void
55 2
    {
56
        if ($this->time !== 0.0) {
57 6
            $this->time = microtime(true) - $this->elapsed + $this->time;
58
        } else {
59
            $this->time = microtime(true);
60 7
        }
61 7
62
        $this->isRunning = true;
63
    }
64
65
    /**
66 2
     * Stops measurement.
67
     *
68 2
     * @return void
69 2
     */
70 2
    public function stop(): void
71
    {
72
        $this->storeElapsed();
73
        $this->isRunning = false;
74
    }
75 1
76
    /**
77 1
     * Stops measurement and starts from the beginning.
78 1
     *
79 1
     * @return void
80
     */
81
    public function restart(): void
82
    {
83
        $this->init();
84 1
        $this->start();
85
    }
86 1
87 1
    /**
88
     * Stops measurement and resets elapsed time.
89
     *
90
     * @return void
91
     */
92 10
    public function reset(): void
93
    {
94 10
        $this->init();
95
    }
96 10
97
    /**
98
     * @return float
99
     */
100
    public function getElapsed(): float
101
    {
102 1
        $this->storeElapsed();
103
104 1
        return $this->elapsed - $this->time;
105
    }
106
107
    /**
108
     * @return int
109
     */
110 1
    public function getElapsedSeconds(): int
111
    {
112 1
        return (int) $this->getElapsed();
113
    }
114
115
    /**
116
     * @return int
117
     */
118 1
    public function getElapsedMilliseconds(): int
119
    {
120 1
        return (int) ($this->getElapsed() * 1000);
121
    }
122
123
    /**
124
     * @return int
125
     */
126
    public function getElapsedMicroseconds(): int
127 6
    {
128
        return (int) ($this->getElapsed() * 1000000);
129 6
    }
130
131
    /**
132
     * Returns true if stopwatch is running, otherwise false.
133
     *
134
     * @return bool
135 7
     */
136
    public function isRunning(): bool
137 7
    {
138 7
        return $this->isRunning;
139 7
    }
140 7
141
    /**
142
     * Initializes stopwatch internals.
143
     *
144
     * @return void
145 10
     */
146
    private function init(): void
147 10
    {
148 3
        $this->time = 0.0;
149
        $this->elapsed = 0.0;
150
        $this->isRunning = false;
151 8
    }
152 8
153
    /**
154
     * Stores elapsed time.
155
     *
156
     * @return void
157
     */
158
    private function storeElapsed(): void
159
    {
160
        if ($this->isRunning === false) {
161
            return;
162
        }
163
164
        $this->elapsed = microtime(true);
165
    }
166
}
167