Completed
Push — develop ( f10e50...4f888d )
by Alec
03:20
created

Timer::elapsed()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 4
nc 4
nop 1
dl 0
loc 7
ccs 4
cts 4
cp 1
crap 3
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * User: alec
4
 * Date: 14.10.18
5
 * Time: 2:19
6
 */
7
8
namespace AlecRabbit\Tools;
9
10
use function AlecRabbit\format_time;
11
use AlecRabbit\Tools\Contracts\TimerInterface;
12
use AlecRabbit\Tools\Reports\Contracts\ReportableInterface;
13
use AlecRabbit\Tools\Reports\Traits\Reportable;
14
use AlecRabbit\Tools\Traits\TimerFields;
15
16
class Timer implements TimerInterface, ReportableInterface
17
{
18
    use TimerFields, Reportable;
19
20
    /**
21
     * Timer constructor.
22
     * @param null|string $name
23
     */
24 16
    public function __construct(?string $name = null)
25
    {
26 16
        $this->name = $this->defaultName($name);
27 16
        $this->creation = $this->current();
28 16
    }
29
30
    /**
31
     * @return float
32
     */
33 16
    private function current(): float
34
    {
35
        return
36 16
            microtime(true);
37
    }
38
39 7
    public function prepareForReport(): void
40
    {
41 7
        if (null === $this->start) {
42 6
            $this->start();
43 6
            $this->mark();
44
        }
45 7
        $this->stop();
46 7
    }
47
48
    /**
49
     * Starts the timer.
50
     *
51
     * @return void
52
     */
53 10
    public function start(): void
54
    {
55 10
        $this->previous = $this->start = $this->current();
56 10
    }
57
58
    /**
59
     * @param int|null $iterationNumber
60
     */
61 9
    private function mark(?int $iterationNumber = null): void
62
    {
63 9
        $current = $this->current();
64 9
        $this->currentValue = $current - $this->previous;
65 9
        $this->previous = $current;
66
67 9
        if (0 !== $this->count) {
68 3
            if ($this->currentValue < $this->minValue) {
69 1
                $this->minValue = $this->currentValue;
70 1
                if ($iterationNumber) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $iterationNumber of type integer|null is loosely compared to true; this is ambiguous if the integer can be 0. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
71 1
                    $this->minValueIteration = $iterationNumber;
72
                }
73
            }
74 3
            if ($this->currentValue > $this->maxValue) {
75 1
                $this->maxValue = $this->currentValue;
76 1
                if ($iterationNumber) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $iterationNumber of type integer|null is loosely compared to true; this is ambiguous if the integer can be 0. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
77 1
                    $this->maxValueIteration = $iterationNumber;
78
                }
79
            }
80 3
            $this->avgValue = (($this->avgValue * $this->count) + $this->currentValue) / ++$this->count;
81
        } else {
82 9
            $this->count = 1;
83 9
            $this->maxValue = $this->currentValue;
84 9
            $this->minValue = $this->currentValue;
85 9
            $this->avgValue = $this->currentValue;
86
        }
87 9
    }
88
89 10
    public function stop(): void
90
    {
91 10
        $this->elapsed = $this->current() - $this->creation;
92 10
        $this->stopped = true;
93 10
    }
94
95
    /**
96
     * Marks the elapsed time.
97
     * If timer was not started starts the timer.
98
     * @param int|null $iterationNumber
99
     * @return Timer
100
     */
101 5
    public function check(?int $iterationNumber = null): Timer
102
    {
103 5
        if (null === $this->start) {
104 1
            $this->start();
105
        } else {
106 5
            $this->mark($iterationNumber);
107
        }
108 5
        return $this;
109
    }
110
111
    /**
112
     * @param bool $formatted
113
     * @return mixed
114
     */
115 4
    public function elapsed(bool $formatted = true)
116
    {
117 4
        if ($this->isNotStopped()) {
118 4
            $this->stop();
119
        }
120
        return
121 4
            $formatted ? format_time($this->getElapsed()) : $this->elapsed;
122
    }
123
}
124