Passed
Push — main ( 0ca30d...79da7a )
by Siad
05:30
created

DefaultClock::stop()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 4
rs 10
c 0
b 0
f 0
ccs 3
cts 3
cp 1
cc 1
nc 1
nop 0
crap 1
1
<?php
2
/**
3
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
 *
15
 * This software consists of voluntary contributions made by many individuals
16
 * and is licensed under the LGPL. For more information please see
17
 * <http://phing.info>.
18
 */
19
20
namespace Phing\Util;
21
22
/**
23
 * @author  Siad Ardroumli <[email protected]>
24
 */
25
class DefaultClock implements Clock
26
{
27
    /**
28
     * start time
29
     *
30
     * @var float
31
     */
32
    protected $stime;
33
34
    /**
35
     * end time
36
     *
37
     * @var float
38
     */
39
    protected $etime;
40
41
    /**
42
     * is the timer running
43
     *
44
     * @var bool
45
     */
46
    protected $running;
47
48
    /**
49
     * Starts the timer and sets the class variable $stime to the current time in microseconds.
50
     *
51
     */
52 2
    public function start()
53
    {
54 2
        $this->stime = $this->getCurrentTime();
55 2
        $this->running = true;
56 2
    }
57
58
    /**
59
     * Stops the timer and sets the class variable $etime to the current time in microseconds.
60
     *
61
     */
62 7
    public function stop()
63
    {
64 7
        $this->etime = $this->getCurrentTime();
65 7
        $this->running = false;
66 7
    }
67
68
    /**
69
     * This function returns the elapsed time in seconds.
70
     *
71
     * Call start_time() at the beginning of script execution and end_time() at
72
     * the end of script execution.  Then, call elapsed_time() to obtain the
73
     * difference between start_time() and end_time().
74
     *
75
     * @param int $places decimal place precision of elapsed time (default is 5)
76
     *
77
     * @return string Properly formatted time.
78
     */
79 1
    public function getElapsedTime($places = 5)
80
    {
81 1
        $etime = $this->etime - $this->stime;
82 1
        $format = "%0." . $places . "f";
83
84 1
        return (sprintf($format, $etime));
85
    }
86
87
    /**
88
     * @return bool
89
     */
90 2
    public function isRunning()
91
    {
92 2
        return $this->running;
93
    }
94
95
    /**
96
     * @return int
97
     */
98 11
    public function getCurrentTime()
99
    {
100 11
        return microtime(true);
0 ignored issues
show
Bug Best Practice introduced by
The expression return microtime(true) also could return the type string which is incompatible with the documented return type integer.
Loading history...
101
    }
102
}
103