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); |
|
|
|
|
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); |
|
|
|
|
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
|
|
|
|
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 theid
property of an instance of theAccount
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.