diff --git a/src/Symfony/Component/HttpKernel/Debug/Stopwatch.php b/src/Symfony/Component/HttpKernel/Debug/Stopwatch.php index cf2d10ee43..06aa36a8d8 100644 --- a/src/Symfony/Component/HttpKernel/Debug/Stopwatch.php +++ b/src/Symfony/Component/HttpKernel/Debug/Stopwatch.php @@ -42,13 +42,17 @@ class Stopwatch /** * Stops the last started section. * + * The id parameter is used to retrieve the events from this section. + * + * @see getSectionEvents + * * @param string $id The identifier of the section */ public function stopSection($id) { $this->stop('section'); - if ($id) { + if (null !== $id) { $this->sections[$id] = $this->events; } @@ -74,9 +78,8 @@ class Stopwatch if (!isset($this->events[$name])) { $this->events[$name] = new StopwatchEvent($this->origin, $category); } - $this->events[$name]->start(); - return $this->events[$name]; + return $this->events[$name]->start(); } /** @@ -92,9 +95,7 @@ class Stopwatch throw new \LogicException(sprintf('Event "%s" is not started.', $name)); } - $this->events[$name]->stop(); - - return $this->events[$name]; + return $this->events[$name]->stop(); } /** @@ -106,9 +107,7 @@ class Stopwatch */ public function lap($name) { - $this->stop($name); - - return $this->start($name); + return $this->stop($name)->start(); } /** diff --git a/src/Symfony/Component/HttpKernel/Debug/StopwatchEvent.php b/src/Symfony/Component/HttpKernel/Debug/StopwatchEvent.php index 4965418596..9796175ae5 100644 --- a/src/Symfony/Component/HttpKernel/Debug/StopwatchEvent.php +++ b/src/Symfony/Component/HttpKernel/Debug/StopwatchEvent.php @@ -32,7 +32,7 @@ class StopwatchEvent public function __construct($origin, $category = null) { $this->origin = $origin; - $this->category = $category ?: 'default'; + $this->category = is_string($category) ? $category : 'default'; $this->started = array(); $this->periods = array(); } @@ -59,14 +59,20 @@ class StopwatchEvent /** * Starts a new event period. + * + * @return StopwatchEvent The event. */ public function start() { $this->started[] = $this->getNow(); + + return $this; } /** * Stops the last started event period. + * + * @return StopwatchEvent The event. */ public function stop() { @@ -75,15 +81,18 @@ class StopwatchEvent } $this->periods[] = array(array_pop($this->started), $this->getNow()); + + return $this; } /** * Stops the current period and then starts a new one. + * + * @return StopwatchEvent The event. */ public function lap() { - $this->stop(); - $this->start(); + return $this->stop()->start(); } /** diff --git a/tests/Symfony/Tests/Component/HttpKernel/Debug/StopwatchTest.php b/tests/Symfony/Tests/Component/HttpKernel/Debug/StopwatchTest.php index ea7ee2d19e..486af75714 100644 --- a/tests/Symfony/Tests/Component/HttpKernel/Debug/StopwatchTest.php +++ b/tests/Symfony/Tests/Component/HttpKernel/Debug/StopwatchTest.php @@ -73,15 +73,22 @@ class StopwatchTest extends \PHPUnit_Framework_TestCase $stopwatch->stop('foo'); $stopwatch->start('bar', 'cat'); $stopwatch->stop('bar'); - $stopwatch->stopSection(1); + $stopwatch->stopSection('1'); $stopwatch->startSection(); $stopwatch->start('foobar', 'cat'); $stopwatch->stop('foobar'); - $stopwatch->stopSection(2); + $stopwatch->stopSection('2'); + + $stopwatch->startSection(); + $stopwatch->start('foobar', 'cat'); + $stopwatch->stop('foobar'); + $stopwatch->stopSection('0'); + // the section is an event by itself - $this->assertEquals(3, count($stopwatch->getSectionEvents(1))); - $this->assertEquals(2, count($stopwatch->getSectionEvents(2))); + $this->assertEquals(3, count($stopwatch->getSectionEvents('1'))); + $this->assertEquals(2, count($stopwatch->getSectionEvents('2'))); + $this->assertEquals(2, count($stopwatch->getSectionEvents('0'))); } }