Timed   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 26
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 26
rs 10
c 0
b 0
f 0
wmc 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A elapsedTime() 0 9 1
A startTimer() 0 3 1
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