Skip to content

Commit 416a2a4

Browse files
committed
[Stopwatch] Fix some logic
1 parent 8c3505e commit 416a2a4

File tree

3 files changed

+34
-6
lines changed

3 files changed

+34
-6
lines changed

src/Symfony/Component/HttpKernel/Debug/Stopwatch.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public function stopSection($id)
7676
public function start($name, $category = null)
7777
{
7878
if (!isset($this->events[$name])) {
79-
$this->events[$name] = new StopwatchEvent($this->origin, $category);
79+
$this->events[$name] = new StopwatchEvent($this->origin ?: microtime(true) * 1000, $category);
8080
}
8181

8282
return $this->events[$name]->start();

src/Symfony/Component/HttpKernel/Debug/StopwatchEvent.php

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,12 @@ class StopwatchEvent
2828
*
2929
* @param integer $origin The origin time in milliseconds
3030
* @param string $category The event category
31+
*
32+
* @throws \InvalidArgumentException When the raw time is not valid
3133
*/
3234
public function __construct($origin, $category = null)
3335
{
34-
$this->origin = $origin;
36+
$this->origin = $this->formatTime($origin);
3537
$this->category = is_string($category) ? $category : 'default';
3638
$this->started = array();
3739
$this->periods = array();
@@ -132,7 +134,7 @@ public function getStartTime()
132134
*/
133135
public function getEndTime()
134136
{
135-
return count($this->periods) ? $this->periods[count($this->periods) - 1][1] : 0;
137+
return ($count = count($this->periods)) ? $this->periods[$count - 1][1] : 0;
136138
}
137139

138140
/**
@@ -147,11 +149,29 @@ public function getTotalTime()
147149
$total += $period[1] - $period[0];
148150
}
149151

150-
return sprintf('%.1f', $total);
152+
return $this->formatTime($total);
151153
}
152154

153155
private function getNow()
154156
{
155-
return sprintf('%.1f', microtime(true) * 1000 - $this->origin);
157+
return $this->formatTime(microtime(true) * 1000 - $this->origin);
158+
}
159+
160+
/**
161+
* Formats a time.
162+
*
163+
* @param numerical $time A raw time
164+
*
165+
* @return float The formatted time
166+
*
167+
* @throws \InvalidArgumentException When the raw time is not valid
168+
*/
169+
private function formatTime($time)
170+
{
171+
if (!is_numeric($time)) {
172+
throw new \InvalidArgumentException('The time must be a numerical value');
173+
}
174+
175+
return round($time, 1);
156176
}
157177
}

tests/Symfony/Tests/Component/HttpKernel/Debug/StopwatchEventTest.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public function testGetCategory()
3131
$event = new StopwatchEvent(microtime(true) * 1000);
3232
$this->assertEquals('default', $event->getCategory());
3333

34-
$event = new StopwatchEvent(time(), 'cat');
34+
$event = new StopwatchEvent(microtime(true) * 1000, 'cat');
3535
$this->assertEquals('cat', $event->getCategory());
3636
}
3737

@@ -141,4 +141,12 @@ public function testEndTime()
141141
$end = $event->getEndTime();
142142
$this->assertTrue($end >= 18 && $end <= 30);
143143
}
144+
145+
/**
146+
* @expectedException \InvalidArgumentException
147+
*/
148+
public function testInvalidOriginThrowsAnException()
149+
{
150+
new StopwatchEvent("abc");
151+
}
144152
}

0 commit comments

Comments
 (0)