8000 feature #23285 [Stopwatch] Add a reset method (jmgq) · symfony/symfony@b23ddfe · GitHub
[go: up one dir, main page]

Skip to content
8000

Commit b23ddfe

Browse files
committed
feature #23285 [Stopwatch] Add a reset method (jmgq)
This PR was merged into the 3.4 branch. Discussion ---------- [Stopwatch] Add a reset method | Q | A | ------------- | --- | Branch? | 3.4 | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #23284 | License | MIT | Doc PR | symfony/symfony-docs#8082 Let the Stopwatch to be reset to its original state, deleting all the data measured so far. This allows the stopwatch to be reusable, and emulates an actual stopwatch's reset button. Commits ------- 7cda099 [Stopwatch] Add a reset method
2 parents 9bcb852 + 7cda099 commit b23ddfe

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

src/Symfony/Component/Stopwatch/Stopwatch.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class Stopwatch
3030

3131
public function __construct()
3232
{
33-
$this->sections = $this->activeSections = array('__root__' => new Section('__root__'));
33+
$this->reset();
3434
}
3535

3636
/**
@@ -156,4 +156,12 @@ public function getSectionEvents($id)
156156
{
157157
return isset($this->sections[$id]) ? $this->sections[$id]->getEvents() : array();
158158
}
159+
160+
/**
161+
* Resets the stopwatch to its original state.
162+
*/
163+
public function reset()
164+
{
165+
$this->sections = $this->activeSections = array('__root__' => new Section('__root__'));
166+
}
159167
}

src/Symfony/Component/Stopwatch/Tests/StopwatchTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,4 +153,16 @@ public function testReopenANewSectionShouldThrowAnException()
153153
$stopwatch = new Stopwatch();
154154
$stopwatch->openSection('section');
155155
}
156+
157+
public function testReset()
158+
{
159+
$stopwatch = new Stopwatch();
160+
161+
$stopwatch->openSection();
162+
$stopwatch->start('foo', 'cat');
163+
164+
$stopwatch->reset();
165+
166+
$this->assertEquals(new Stopwatch(), $stopwatch);
167+
}
156168
}

0 commit comments

Comments
 (0)
0