Skip to content

Commit

Permalink
feature #54855 [Stopwatch] Add getLastPeriod method to `StopwatchEv…
Browse files Browse the repository at this point in the history
…ent` (hacfi)

This PR was merged into the 7.2 branch.

Discussion
----------

[Stopwatch] Add `getLastPeriod` method to `StopwatchEvent`

| Q             | A
| ------------- | ---
| Branch?       | 7.1
| Bug fix?      | no
| New feature?  | no
| Deprecations? | no
| Issues        | -
| License       | MIT

It's often useful to just get the last period (if you start and stop an event several times).

### Example
https://github.com/doctrine/migrations/blob/3.7.x/lib/Doctrine/Migrations/Version/DbalExecutor.php#L167-L168

This adds a simple `getLastPeriod` method on the `StopwatchEvent` class.

Commits
-------

5c25139 [Stopwatch] Add `getLastPeriod` method to `StopwatchEvent`
  • Loading branch information
fabpot committed May 21, 2024
2 parents b94aad4 + 5c25139 commit 2ea870f
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
5 changes: 5 additions & 0 deletions src/Symfony/Component/Stopwatch/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

7.2
---

* Add method `getLastPeriod()` to `StopwatchEvent`

5.2
---

Expand Down
12 changes: 12 additions & 0 deletions src/Symfony/Component/Stopwatch/StopwatchEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,18 @@ public function getPeriods(): array
return $this->periods;
}

/**
* Gets the last event period.
*/
public function getLastPeriod(): ?StopwatchPeriod
{
if ([] === $this->periods) {
return null;
}

return $this->periods[array_key_last($this->periods)];
}

/**
* Gets the relative time of the start of the first period in milliseconds.
*/
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/Stopwatch/StopwatchPeriod.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
namespace Symfony\Component\Stopwatch;

/**
* Represents an Period for an Event.
* Represents a Period for an Event.
*
* @author Fabien Potencier <fabien@symfony.com>
*/
Expand Down

0 comments on commit 2ea870f

Please sign in to comment.