Timed::elapsedTime()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 1
dl 0
loc 9
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace ReliqArts\Scavenger\Concern;
6
7
/**
8
 * Timed trait.
9
 */
10
trait Timed
11
{
12
    /**
13
     * @var float
14
     */
15
    protected float $startTime = 0;
16
17
    /**
18
     * Get seconds since a "micro-time" start-time.
19
     *
20
     * @return string seconds since, to 2 decimal places
21
     */
22
    protected function elapsedTime(?float $startTime = null): string
23
    {
24
        $startTime = $startTime ?? $this->startTime;
25
        $duration = microtime(true) - $startTime;
26
        $hours = (int)($duration / 60 / 60);
27
        $minutes = (int)($duration / 60) - $hours * 60;
28
        $seconds = $duration - $hours * 60 * 60 - $minutes * 60;
29
30
        return number_format((float)$seconds, 2, '.', '') . 's';
31
    }
32
33
    protected function startTimer(): void
34
    {
35
        $this->startTime = microtime(true);
36
    }
37
}
38