Completed
Push — master ( 6a7da5...1827cf )
by Alec
08:13 queued 02:32
created

Timer::computeAverage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace AlecRabbit\Tools;
4
5
use AlecRabbit\Pretty;
6
use AlecRabbit\Tools\Contracts\TimerInterface;
7
use AlecRabbit\Tools\Reports\Contracts\ReportableInterface;
8
use AlecRabbit\Tools\Reports\Traits\Reportable;
9
use AlecRabbit\Tools\Traits\TimerFields;
10
11
class Timer implements TimerInterface, ReportableInterface
12
{
13
    use TimerFields, Reportable;
14
15
    /**
16
     * Timer constructor.
17
     * @param null|string $name
18
     * @param bool $start
19
     */
20 21
    public function __construct(?string $name = null, bool $start = true)
21
    {
22 21
        $this->name = $this->defaultName($name);
23 21
        $this->creation = $this->current();
24 21
        if ($start) {
25 18
            $this->start($this->creation);
26
        }
27 21
    }
28
29
    /**
30
     * @return float
31
     */
32 21
    public function current(): float
33
    {
34
        return
35 21
            microtime(true);
36
    }
37
38
    /**
39
     * Starts the timer.
40
     *
41
     * @param null|float $point
42
     * @return void
43
     */
44 21
    public function start(?float $point = null): void
45
    {
46 21
        $this->previous = $point ?? $this->current();
47 21
        $this->started = true;
48 21
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53 10
    public function prepareForReport(): void
54
    {
55 10
        if ($this->isNotStarted()) {
56 1
            $this->start();
57 1
            $this->mark();
58
        }
59 10
        $this->stop();
60 10
    }
61
62
    /**
63
     * @param int|null $iterationNumber
64
     */
65 6
    private function mark(?int $iterationNumber = null): void
66
    {
67 6
        $current = $this->current();
68 6
        $this->currentValue = $current - $this->previous;
69 6
        $this->previous = $current;
70
71 6
        $this->compute($iterationNumber);
72 6
    }
73
74
    /**
75
     * @param null|int $iterationNumber
76
     */
77 10
    private function compute(?int $iterationNumber): void
78
    {
79 10
        if (0 !== $this->count) {
80 8
            ++$this->count;
81 8
            $this->checkMinValue($iterationNumber);
82 8
            $this->checkMaxValue($iterationNumber);
83 8
            $this->computeAverage();
84
        } else {
85 10
            $this->initValues();
86
        }
87 10
    }
88
89
    /**
90
     * @param null|int $iterationNumber
91
     */
92 8
    private function checkMinValue(?int $iterationNumber): void
93
    {
94 8
        if ($this->currentValue < $this->minValue) {
95 2
            $this->minValue = $this->currentValue;
96 2
            $this->minValueIteration = $iterationNumber ?? $this->count;
97
        }
98 8
    }
99
100
    /**
101
     * @param null|int $iterationNumber
102
     */
103 8
    private function checkMaxValue(?int $iterationNumber): void
104
    {
105 8
        if ($this->currentValue > $this->maxValue) {
106 3
            $this->maxValue = $this->currentValue;
107 3
            $this->maxValueIteration = $iterationNumber ?? $this->count;
108
        }
109 8
    }
110
111 8
    private function computeAverage(): void
112
    {
113 8
        $this->avgValue = (($this->avgValue * ($this->count - 1)) + $this->currentValue) / $this->count;
114 8
    }
115
116 10
    private function initValues(): void
117
    {
118 10
        $this->maxValueIteration = $this->minValueIteration = $this->count = 1;
119 10
        $this->maxValue = $this->currentValue;
120 10
        $this->minValue = $this->currentValue;
121 10
        $this->avgValue = $this->currentValue;
122 10
    }
123
124 12
    public function stop(): void
125
    {
126 12
        $this->computeElapsed();
127 12
        $this->stopped = true;
128 12
    }
129
130 15
    private function computeElapsed(): void
131
    {
132 15
        $this->elapsed = $this->current() - $this->creation;
133 15
    }
134
135
    /**
136
     * Marks the time.
137
     * If timer was not started starts the timer.
138
     * @param int|null $iterationNumber
139
     * @return Timer
140
     */
141 5
    public function check(?int $iterationNumber = null): Timer
142
    {
143 5
        if ($this->isNotStarted()) {
144 1
            $this->start();
145
        } else {
146 5
            $this->mark($iterationNumber);
147
        }
148 5
        return $this;
149
    }
150
151
    /**
152
     * @param float $start
153
     * @param float $stop
154
     * @param null|int $iterationNumber
155
     * @return Timer
156
     */
157 4
    public function bounds(float $start, float $stop, ?int $iterationNumber = null): Timer
158
    {
159 4
        if ($this->isNotStarted()) {
160 1
            $this->start();
161
        }
162 4
        $this->currentValue = $stop - $start;
163 4
        $this->previous = $stop;
164
165 4
        $this->compute($iterationNumber);
166 4
        return $this;
167
    }
168
169
    /**
170
     * @param bool $pretty
171
     * @return mixed
172
     */
173 6
    public function elapsed(bool $pretty = true)
174
    {
175 6
        if ($this->isNotStopped()) {
176 4
            $this->stop();
177
        }
178
        return
179 6
            $pretty ? Pretty::seconds($this->getElapsed()) : $this->elapsed;
180
    }
181
182
    /**
183
     * @return float
184
     */
185 15
    public function getElapsed(): float
186
    {
187 15
        if ($this->isNotStopped()) {
188 3
            $this->computeElapsed();
189
        }
190 15
        return $this->elapsed;
191
    }
192
}
193