Passed
Branch master (f36b3c)
by Matthew
08:01
created

Timer::stop()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 13
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 5
c 1
b 0
f 0
dl 0
loc 13
ccs 6
cts 6
cp 1
rs 10
cc 4
nc 6
nop 1
crap 4
1
<?php
2
namespace Fyuze\Debug;
3
4
class Timer
5
{
6
    /**
7
     * @var
8
     */
9
    protected $timers;
10
11
    protected $current;
12
13
    /**
14
     * @param string $name
15
     * @return float
16
     */
17 2
    public function start($name = 'default')
18
    {
19 2
        $this->current = $name;
20
21 2
        $start = microtime(true);
22
23 2
        $this->timers[$name]['start'] = $start;
24
25 2
        return $start;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $start also could return the type string which is incompatible with the documented return type double.
Loading history...
26
    }
27
28
    /**
29
     * @param null $name
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $name is correct as it would always require null to be passed?
Loading history...
30
     * @return array
31
     */
32 2
    public function stop($name = null)
33
    {
34 2
        $key = ($name !== null || ($name && array_key_exists($name, $this->timers))) ? $name : 'default';
0 ignored issues
show
introduced by
$name is of type null, thus it always evaluated to false.
Loading history...
Bug introduced by
$name of type void is incompatible with the type integer|string expected by parameter $key of array_key_exists(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

34
        $key = ($name !== null || ($name && array_key_exists(/** @scrutinizer ignore-type */ $name, $this->timers))) ? $name : 'default';
Loading history...
35
36 2
        $this->timers[$key]['stop'] = microtime(true);
37
38
        /** @var $start float */
39
        /** @var $stop float */
40 2
        extract($this->timers[$key]);
41
42 2
        $this->timers[$key]['duration'] = $stop - $start;
43
44 2
        return $this->timers[$key];
45
    }
46
47
    /**
48
     *
49
     */
50 1
    public function getTimers()
51
    {
52 1
        return $this->timers;
53
    }
54
55
    /**
56
     * @param \Closure $closure
57
     * @return array
58
     */
59 1
    public function transaction(\Closure $closure)
60
    {
61 1
        $this->start();
62
63 1
        $closure();
64
65 1
        return $this->stop();
66
    }
67
}
68