Skip to content

Commit 928ab32

Browse files
author
Ahmed Raafat
committed
Add name property to the stopwatchEvent
1 parent 0ab5eed commit 928ab32

File tree

3 files changed

+32
-3
lines changed

3 files changed

+32
-3
lines changed

src/Symfony/Component/Stopwatch/Section.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ public function setId(string $id)
113113
public function startEvent(string $name, ?string $category)
114114
{
115115
if (!isset($this->events[$name])) {
116-
$this->events[$name] = new StopwatchEvent($this->origin ?: microtime(true) * 1000, $category, $this->morePrecision);
116+
$this->events[$name] = new StopwatchEvent($this->origin ?: microtime(true) * 1000, $category, $this->morePrecision, $name);
117117
}
118118

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

src/Symfony/Component/Stopwatch/StopwatchEvent.php

+19-2
Original file line numberDiff line numberDiff line change
@@ -43,18 +43,25 @@ class StopwatchEvent
4343
*/
4444
private $started = [];
4545

46+
/**
47+
* @var string
48+
*/
49+
private $name;
50+
4651
/**
4752
* @param float $origin The origin time in milliseconds
4853
* @param string|null $category The event category or null to use the default
4954
* @param bool $morePrecision If true, time is stored as float to keep the original microsecond precision
55+
* @param string $name The event name or empty string as default value
5056
*
5157
* @throws \InvalidArgumentException When the raw time is not valid
5258
*/
53-
public function __construct(float $origin, string $category = null, bool $morePrecision = false)
59+
public function __construct(float $origin, string $category = null, bool $morePrecision = false, string $name = '')
5460
{
5561
$this->origin = $this->formatTime($origin);
5662
$this->category = \is_string($category) ? $category : 'default';
5763
$this->morePrecision = $morePrecision;
64+
$this->name = $name;
5865
}
5966

6067
/**
@@ -236,11 +243,21 @@ private function formatTime(float $time): float
236243
return round($time, 1);
237244
}
238245

246+
/**
247+
* Gets the event name.
248+
*
249+
* @return string The event name
250+
*/
251+
public function getName()
252+
{
253+
return $this->name;
254+
}
255+
239256
/**
240257
* @return string
241258
*/
242259
public function __toString()
243260
{
244-
return sprintf('%s: %.2F MiB - %d ms', $this->getCategory(), $this->getMemory() / 1024 / 1024, $this->getDuration());
261+
return sprintf('%s: %.2F MiB - %d ms', ($this->getName() ?? $this->getCategory()), $this->getMemory() / 1024 / 1024, $this->getDuration());
245262
}
246263
}

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

+12
Original file line numberDiff line numberDiff line change
@@ -200,5 +200,17 @@ public function testHumanRepresentation()
200200

201201
$event = new StopwatchEvent(microtime(true) * 1000, 'foo');
202202
$this->assertEquals('foo: 0.00 MiB - 0 ms', (string) $event);
203+
204+
$event = new StopwatchEvent(microtime(true) * 1000, 'foo', false, 'name');
205+
$this->assertEquals('name: 0.00 MiB - 0 ms', (string) $event);
206+
}
207+
208+
public function testGetName()
209+
{
210+
$event = new StopwatchEvent(microtime(true) * 1000);
211+
$this->assertEquals('', $event->getName());
212+
213+
$event = new StopwatchEvent(microtime(true) * 1000, 'cat', false, 'test');
214+
$this->assertEquals('test', $event->getCategory());
203215
}
204216
}

0 commit comments

Comments
 (0)