Skip to content

[Stopwatch] StopwatchEvent->getStartTime() is incorrect #34088

Closed
@sirian

Description

@sirian

Symfony version(s) affected: ^4.3

How to reproduce

$event = new \Symfony\Component\Stopwatch\StopwatchEvent(0, null, true);

$event->start();
usleep(100000);
var_dump($event->getStartTime());
$event->stop();
var_dump($event->getStartTime());

Output:

int(0); // But should not be 0
float(1571838784644.8)

Possible solutions

Bug lives here https://github.com/symfony/stopwatch/blob/9e2c41817c3e3d506e5db1d052cbfbbc6f685199/StopwatchEvent.php#L157

     public function getStartTime()
    {
        return isset($this->periods[0]) ? $this->periods[0]->getStartTime() : 0;
    }

Should be

    public function getStartTime()
    {
        if (isset($this->periods[0])) {
            return $this->periods[0]->getStartTime();
        }
        if (\count($this->started)) {
            return $this->started[0];
        }
        return 0;
    }

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions