Stopwatch::start()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 4
cts 4
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
     * @return void
53
     */
54 7
    public function start(): void
55
    {
56 7
        $this->time = microtime(true) - $this->getElapsed();
57 7
        $this->isRunning = true;
58 7
    }
59
60
    /**
61
     * Stops measurement.
62
     *
63
     * @return void
64
     */
65 2
    public function stop(): void
66
    {
67 2
        $this->storeElapsed();
68 2
        $this->isRunning = false;
69 2
    }
70
71
    /**
72
     * Stops measurement and starts from the beginning.
73
     *
74
     * @return void
75
     */
76 1
    public function restart(): void
77
    {
78 1
        $this->init();
79 1
        $this->start();
80 1
    }
81
82
    /**
83
     * Stops measurement and resets elapsed time.
84
     *
85
     * @return void
86
     */
87 1
    public function reset(): void
88
    {
89 1
        $this->init();
90 1
    }
91
92
    /**
93
     * @return float
94
     */
95 10
    public function getElapsed(): float
96
    {
97 10
        $this->storeElapsed();
98
99 10
        return $this->elapsed - $this->time;
100
    }
101
102
    /**
103
     * @return int
104
     */
105 1
    public function getElapsedSeconds(): int
106
    {
107 1
        return (int) $this->getElapsed();
108
    }
109
110
    /**
111
     * @return int
112
     */
113 1
    public function getElapsedMilliseconds(): int
114
    {
115 1
        return (int) ($this->getElapsed() * 1000);
116
    }
117
118
    /**
119
     * @return int
120
     */
121 1
    public function getElapsedMicroseconds(): int
122
    {
123 1
        return (int) ($this->getElapsed() * 1000000);
124
    }
125
126
    /**
127
     * Returns true if stopwatch is running, otherwise false.
128
     *
129
     * @return bool
130
     */
131 6
    public function isRunning(): bool
132
    {
133 6
        return $this->isRunning;
134
    }
135
136
    /**
137
     * Initializes stopwatch internals.
138
     *
139
     * @return void
140
     */
141 7
    private function init(): void
142
    {
143 7
        $this->time = 0.0;
144 7
        $this->elapsed = 0.0;
145 7
        $this->isRunning = false;
146 7
    }
147
148
    /**
149
     * Stores elapsed time.
150
     *
151
     * @return void
152
     */
153 10
    private function storeElapsed(): void
154
    {
155 10
        if ($this->isRunning === false) {
156 10
            return;
157
        }
158
159 8
        $this->elapsed = microtime(true);
160 8
    }
161
}
162