TimerFields::getCreation()   A
last analyzed

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 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php declare(strict_types=1);
2
3
namespace AlecRabbit\Timers\Core\Traits;
4
5
use AlecRabbit\Timers\HRTimer;
6
use AlecRabbit\Traits\GettableName;
7
use AlecRabbit\Traits\StartableAndStoppable;
8
9
trait TimerFields
10
{
11
    use GettableName, StartableAndStoppable;
12
13
    /** @var int|float */
14
    protected $previous = 0.0;
15
16
    /** @var \DateTimeImmutable */
17
    protected $creationTime;
18
19
    /** @var \DateInterval */
20
    protected $elapsed;
21
22
    /** @var int|float */
23
    protected $currentValue = 0.0;
24
25
    /** @var float */
26
    protected $avgValue = 0.0;
27
28
    /** @var float|int|null */
29
    protected $minValue;
30
31
    /** @var float|int|null */
32
    protected $maxValue;
33
34
    /** @var int */
35
    protected $minValueIteration = 0;
36
37
    /** @var int */
38
    protected $maxValueIteration = 0;
39
40
    /** @var int */
41
    protected $count = 0;
42
43
    /**
44
     * @return float
45
     */
46 20
    public function getLastValue(): float
47
    {
48
        return
49 20
            $this->normalize($this->currentValue);
50
    }
51
52
    /**
53
     * @param int|float $value
54
     * @return float
55
     */
56 20
    protected function normalize($value): float
57
    {
58 20
        if ($this instanceof HRTimer) {
59
            return
60 3
                $value / HRTimer::VALUE_COEFFICIENT;
61
        }
62 17
        return $value;
63
    }
64
65
    /**
66
     * @return float
67
     */
68 20
    public function getAverageValue(): float
69
    {
70
        return
71 20
            $this->normalize($this->avgValue);
72
    }
73
74
    /**
75
     * @return null|float
76
     */
77 20
    public function getMinValue(): ?float
78
    {
79
80
        return
81 20
            $this->minValue ? $this->normalize($this->minValue) : null;
82
    }
83
84
    /**
85
     * @return null|float
86
     */
87 20
    public function getMaxValue(): ?float
88
    {
89
        return
90 20
            $this->maxValue ? $this->normalize($this->maxValue) : null;
91
    }
92
93
    /**
94
     * @return int
95
     */
96 20
    public function getCount(): int
97
    {
98 20
        return $this->count;
99
    }
100
101
    /**
102
     * @return int
103
     */
104 12
    public function getMinValueIteration(): int
105
    {
106 12
        return $this->minValueIteration;
107
    }
108
109
    /**
110
     * @return int
111
     */
112 12
    public function getMaxValueIteration(): int
113
    {
114 12
        return $this->maxValueIteration;
115
    }
116
117
    /**
118
     * @return \DateInterval
119
     */
120 8
    public function getElapsed(): \DateInterval
121
    {
122 8
        return $this->elapsed;
123
    }
124
125
    /**
126
     * @return \DateTimeImmutable
127
     */
128 11
    public function getCreation(): \DateTimeImmutable
129
    {
130 11
        return $this->creationTime;
131
    }
132
}
133