This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include
, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | namespace Yep\Stopwatch; |
||
3 | |||
4 | class Event { |
||
5 | protected $name; |
||
6 | protected $group; |
||
7 | |||
8 | /** @var int|float */ |
||
9 | protected $start_time = 0; |
||
10 | |||
11 | /** @var int|float */ |
||
12 | protected $stop_time = 0; |
||
13 | |||
14 | /** @var int */ |
||
15 | protected $memory_usage = 0; |
||
16 | protected $started_laps = []; |
||
17 | |||
18 | /** @var Lap[] */ |
||
19 | protected $stopped_laps = []; |
||
20 | |||
21 | /** |
||
22 | * Event constructor |
||
23 | * |
||
24 | * @param string $name |
||
25 | * @param null|string $group |
||
26 | */ |
||
27 | 7 | public function __construct($name, $group = null) { |
|
28 | 7 | $this->name = $name; |
|
29 | 7 | $this->group = $group; |
|
30 | 7 | } |
|
31 | |||
32 | /** |
||
33 | * Returns the event name |
||
34 | * |
||
35 | * @return string |
||
36 | */ |
||
37 | 2 | public function getName() { |
|
38 | 2 | return $this->name; |
|
39 | } |
||
40 | |||
41 | /** |
||
42 | * Returns the event group |
||
43 | * |
||
44 | * @return null|string |
||
45 | */ |
||
46 | 2 | public function getGroup() { |
|
47 | 2 | return $this->group; |
|
48 | } |
||
49 | |||
50 | /** |
||
51 | * Starts new lap in the event |
||
52 | * |
||
53 | * @return Event |
||
54 | */ |
||
55 | 6 | public function startLap() { |
|
56 | 6 | $start_time = microtime(true); |
|
57 | |||
58 | 6 | if ($this->start_time === 0) { |
|
59 | 6 | $this->start_time = $start_time; |
|
60 | 6 | } |
|
61 | |||
62 | 6 | $this->started_laps[] = $start_time; |
|
63 | |||
64 | 6 | return $this; |
|
65 | } |
||
66 | |||
67 | /** |
||
68 | * Stops latest lap in the event |
||
69 | * |
||
70 | * @return Event |
||
71 | */ |
||
72 | 7 | public function stopLap() { |
|
73 | 7 | if (empty($this->started_laps)) { |
|
74 | 3 | throw new StopwatchLapNotStartedException('You must call startLap() before stopLap()'); |
|
75 | } |
||
76 | |||
77 | 6 | $this->memory_usage = memory_get_usage(true); |
|
78 | 6 | $this->stop_time = microtime(true); |
|
79 | |||
80 | 6 | $this->stopped_laps[] = new Lap(array_pop($this->started_laps), $this->stop_time, $this->memory_usage); |
|
81 | |||
82 | 6 | return $this; |
|
83 | } |
||
84 | |||
85 | /** |
||
86 | * Stops started laps |
||
87 | */ |
||
88 | 1 | public function stopStartedLaps() { |
|
89 | 1 | while (!empty($this->started_laps)) { |
|
90 | 1 | $this->stopLap(); |
|
91 | 1 | } |
|
92 | 1 | } |
|
93 | |||
94 | /** |
||
95 | * Returns true if some started lap exists, or false if not |
||
96 | * |
||
97 | * @return bool |
||
98 | */ |
||
99 | 3 | public function hasStartedLaps() { |
|
100 | 3 | return !empty($this->started_laps); |
|
101 | } |
||
102 | |||
103 | /** |
||
104 | * Returns the start time of the first lap |
||
105 | * |
||
106 | * @return int|float Time in seconds |
||
107 | */ |
||
108 | 1 | public function getStartTime() { |
|
109 | 1 | return $this->start_time; |
|
110 | } |
||
111 | |||
112 | /** |
||
113 | * Returns the stop time of the last stopped lap |
||
114 | * |
||
115 | * @return int|float Time in seconds |
||
116 | */ |
||
117 | 1 | public function getStopTime() { |
|
118 | 1 | return $this->stop_time; |
|
119 | } |
||
120 | |||
121 | /** |
||
122 | * Returns the duration of all laps durations |
||
123 | * |
||
124 | * @return float|int Time in seconds |
||
125 | */ |
||
126 | 1 | public function getDuration() { |
|
127 | 1 | return $this->stop_time - $this->start_time; |
|
128 | } |
||
129 | |||
130 | /** |
||
131 | * Returns the memory usage |
||
132 | * |
||
133 | * @return int Memory in bytes |
||
134 | */ |
||
135 | 2 | public function getMemoryUsage() { |
|
136 | 2 | return $this->memory_usage; |
|
137 | } |
||
138 | |||
139 | /** |
||
140 | * Returns the stopped laps |
||
141 | * |
||
142 | * @return Lap[] |
||
143 | */ |
||
144 | 2 | public function getStoppedLaps() { |
|
145 | 2 | return $this->stopped_laps; |
|
146 | } |
||
147 | |||
148 | /** |
||
149 | * Returns the lowest duration of all laps |
||
150 | * |
||
151 | * @return float|int Time in seconds |
||
152 | */ |
||
153 | 2 | public function getMinDuration() { |
|
154 | 2 | if (empty($this->stopped_laps)) { |
|
155 | 1 | return 0; |
|
156 | } |
||
157 | |||
158 | 1 | $duration = 0; |
|
159 | |||
160 | 1 | foreach ($this->stopped_laps as $lap) { |
|
161 | 1 | $lap_duration = $lap->getDuration(); |
|
162 | |||
163 | 1 | if ($duration === 0 || $lap_duration < $duration) { |
|
164 | 1 | $duration = $lap_duration; |
|
165 | 1 | } |
|
166 | 1 | } |
|
167 | |||
168 | 1 | return $duration; |
|
169 | } |
||
170 | |||
171 | /** |
||
172 | * Returns the highest duration of all laps |
||
173 | * |
||
174 | * @return float|int Time in seconds |
||
175 | */ |
||
176 | 2 | View Code Duplication | public function getMaxDuration() { |
0 ignored issues
–
show
|
|||
177 | 2 | if (empty($this->stopped_laps)) { |
|
178 | 1 | return 0; |
|
179 | } |
||
180 | |||
181 | 1 | $duration = 0; |
|
182 | |||
183 | 1 | foreach ($this->stopped_laps as $lap) { |
|
184 | 1 | if (($lap_duration = $lap->getDuration()) > $duration) { |
|
185 | 1 | $duration = $lap_duration; |
|
186 | 1 | } |
|
187 | 1 | } |
|
188 | |||
189 | 1 | return $duration; |
|
190 | } |
||
191 | |||
192 | /** |
||
193 | * Returns the average duration of all laps |
||
194 | * |
||
195 | * @return float|int Time in seconds |
||
196 | */ |
||
197 | 2 | View Code Duplication | public function getAverageDuration() { |
0 ignored issues
–
show
This method seems to be duplicated in your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository. ![]() |
|||
198 | 2 | if (empty($this->stopped_laps)) { |
|
199 | 1 | return 0; |
|
200 | } |
||
201 | |||
202 | 1 | $duration = 0; |
|
203 | 1 | $count = 0; |
|
204 | |||
205 | 1 | foreach ($this->stopped_laps as $lap) { |
|
206 | 1 | $duration += $lap->getDuration(); |
|
207 | 1 | $count++; |
|
208 | 1 | } |
|
209 | |||
210 | 1 | return $duration / $count; |
|
211 | } |
||
212 | } |
||
213 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.