Passed
Push — master ( cbf6d3...d62b89 )
by Michiel
08:56
created

Timer::isRunning()   A

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 1 Features 0
Metric Value
eloc 1
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 1
b 1
f 0
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
 * This class can be used to obtain the execution time of all of the scripts
23
 * that are executed in the process of building a page.
24
 *
25
 * Example:
26
 * To be done before any scripts execute:
27
 *
28
 * $Timer = new Timer;
29
 * $Timer->Start_Timer();
30
 *
31
 * To be done after all scripts have executed:
32
 *
33
 * $timer->Stop_Timer();
34
 * $timer->Get_Elapsed_Time(int number_of_places);
35
 *
36
 * @author Charles Killian
37
 * @author Hans Lellelid <[email protected]>
38
 *
39
 */
40
class Timer
41
{
42
    /**
43
     * start time
44
     *
45
     * @var float
46
     */
47
    protected $stime;
48
49
    /**
50
     * end time
51
     *
52
     * @var float
53
     */
54
    protected $etime;
55
56
    /**
57
     * is the timer running
58
     *
59
     * @var bool
60
     */
61
    protected $running;
62
63
    /**
64
     * Starts the timer and sets the class variable $stime to the current time in microseconds.
65
     *
66
     * @return void
67
     */
68 2
    public function start()
69
    {
70 2
        $this->stime = microtime(true);
0 ignored issues
show
Documentation Bug introduced by
It seems like microtime(true) can also be of type string. However, the property $stime is declared as type double. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
71 2
        $this->running = true;
72 2
    }
73
74
    /**
75
     * Stops the timer and sets the class variable $etime to the current time in microseconds.
76
     *
77
     * @return void
78
     */
79 7
    public function stop()
80
    {
81 7
        $this->etime = microtime(true);
0 ignored issues
show
Documentation Bug introduced by
It seems like microtime(true) can also be of type string. However, the property $etime is declared as type double. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
82 7
        $this->running = false;
83 7
    }
84
85
    /**
86
     * This function returns the elapsed time in seconds.
87
     *
88
     * Call start_time() at the beginning of script execution and end_time() at
89
     * the end of script execution.  Then, call elapsed_time() to obtain the
90
     * difference between start_time() and end_time().
91
     *
92
     * @param int $places decimal place precision of elapsed time (default is 5)
93
     *
94
     * @return string Properly formatted time.
95
     */
96 1
    public function getElapsedTime($places = 5)
97
    {
98 1
        $etime = $this->etime - $this->stime;
99 1
        $format = "%0." . $places . "f";
100
101 1
        return (sprintf($format, $etime));
102
    }
103
104
    /**
105
     * @return bool
106
     */
107 2
    public function isRunning()
108
    {
109 2
        return $this->running;
110
    }
111
}
112