Skip to content

Commit

Permalink
minor #32242 [Stopwatch] Add type-hints for Stopwatch and Section cla…
Browse files Browse the repository at this point in the history
…sses (jschaedl, Tobion)

This PR was merged into the 5.0-dev branch.

Discussion
----------

[Stopwatch] Add type-hints for Stopwatch and Section classes

| Q             | A
| ------------- | ---
| Branch?       | master <!-- see below -->
| Bug fix?      | no
| New feature?  | no <!-- please update src/**/CHANGELOG.md files -->
| BC breaks?    | no     <!-- see https://symfony.com/bc -->
| Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files -->
| Tests pass?   | yes    <!-- please add some, will be required by reviewers -->
| Fixed tickets | #32179  <!-- #-prefixed issue number(s), if any -->
| License       | MIT
| Doc PR        | N/A <!-- required for new features -->

This PR adds type hints to the `Stopwatch` and `Section` classes.

Commits
-------

538cc34 [Stopwatch] fix wrong nullable type
ff4528e [Stopwatch] Add type-hints for Stopwatch and Section classes
  • Loading branch information
Tobion committed Jul 29, 2019
2 parents ab7228f + 538cc34 commit 7b354df
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 46 deletions.
5 changes: 5 additions & 0 deletions UPGRADE-5.0.md
Expand Up @@ -477,6 +477,11 @@ Serializer
* The `AbstractNormalizer::handleCircularReference()` method has two new `$format` and `$context` arguments.
* Removed support for instantiating a `DataUriNormalizer` with a default MIME type guesser when the `symfony/mime` component isn't installed.

Stopwatch
---------

* Removed support for passing `null` as 1st (`$id`) argument of `Section::get()` method, pass a valid child section identifier instead.

Translation
-----------

Expand Down
5 changes: 5 additions & 0 deletions src/Symfony/Component/Stopwatch/CHANGELOG.md
@@ -1,6 +1,11 @@
CHANGELOG
=========

5.0.0
-----

* Removed support for passing `null` as 1st (`$id`) argument of `Section::get()` method, pass a valid child section identifier instead.

4.4.0
-----

Expand Down
31 changes: 8 additions & 23 deletions src/Symfony/Component/Stopwatch/Section.php
Expand Up @@ -56,11 +56,9 @@ public function __construct(float $origin = null, bool $morePrecision = false)
/**
* Returns the child section.
*
* @param string $id The child section identifier
*
* @return self|null The child section or null when none found
*/
public function get($id)
public function get(string $id)
{
foreach ($this->children as $child) {
if ($id === $child->getId()) {
Expand All @@ -78,7 +76,7 @@ public function get($id)
*
* @return self
*/
public function open($id)
public function open(?string $id)
{
if (null === $id || null === $session = $this->get($id)) {
$session = $this->children[] = new self(microtime(true) * 1000, $this->morePrecision);
Expand All @@ -98,11 +96,9 @@ public function getId()
/**
* Sets the session identifier.
*
* @param string $id The session identifier
*
* @return $this
*/
public function setId($id)
public function setId(string $id)
{
$this->id = $id;

Expand All @@ -112,12 +108,9 @@ public function setId($id)
/**
* Starts an event.
*
* @param string $name The event name
* @param string|null $category The event category
*
* @return StopwatchEvent The event
*/
public function startEvent($name, $category)
public function startEvent(string $name, ?string $category)
{
if (!isset($this->events[$name])) {
$this->events[$name] = new StopwatchEvent($this->origin ?: microtime(true) * 1000, $category, $this->morePrecision);
Expand All @@ -129,25 +122,21 @@ public function startEvent($name, $category)
/**
* Checks if the event was started.
*
* @param string $name The event name
*
* @return bool
*/
public function isEventStarted($name)
public function isEventStarted(string $name)
{
return isset($this->events[$name]) && $this->events[$name]->isStarted();
}

/**
* Stops an event.
*
* @param string $name The event name
*
* @return StopwatchEvent The event
*
* @throws \LogicException When the event has not been started
*/
public function stopEvent($name)
public function stopEvent(string $name)
{
if (!isset($this->events[$name])) {
throw new \LogicException(sprintf('Event "%s" is not started.', $name));
Expand All @@ -159,27 +148,23 @@ public function stopEvent($name)
/**
* Stops then restarts an event.
*
* @param string $name The event name
*
* @return StopwatchEvent The event
*
* @throws \LogicException When the event has not been started
*/
public function lap($name)
public function lap(string $name)
{
return $this->stopEvent($name)->start();
}

/**
* Returns a specific event by name.
*
* @param string $name The event name
*
* @return StopwatchEvent The event
*
* @throws \LogicException When the event is not known
*/
public function getEvent($name)
public function getEvent(string $name)
{
if (!isset($this->events[$name])) {
throw new \LogicException(sprintf('Event "%s" is not known.', $name));
Expand Down
31 changes: 8 additions & 23 deletions src/Symfony/Component/Stopwatch/Stopwatch.php
Expand Up @@ -59,7 +59,7 @@ public function getSections()
*
* @throws \LogicException When the section to re-open is not reachable
*/
public function openSection($id = null)
public function openSection(string $id = null)
{
$current = end($this->activeSections);

Expand All @@ -79,11 +79,9 @@ public function openSection($id = null)
*
* @see getSectionEvents()
*
* @param string $id The identifier of the section
*
* @throws \LogicException When there's no started section to be stopped
*/
public function stopSection($id)
public function stopSection(string $id)
{
$this->stop('__section__');

Expand All @@ -98,72 +96,59 @@ public function stopSection($id)
/**
* Starts an event.
*
* @param string $name The event name
* @param string|null $category The event category
*
* @return StopwatchEvent
*/
public function start($name, $category = null)
public function start(string $name, string $category = null)
{
return end($this->activeSections)->startEvent($name, $category);
}

/**
* Checks if the event was started.
*
* @param string $name The event name
*
* @return bool
*/
public function isStarted($name)
public function isStarted(string $name)
{
return end($this->activeSections)->isEventStarted($name);
}

/**
* Stops an event.
*
* @param string $name The event name
*
* @return StopwatchEvent
*/
public function stop($name)
public function stop(string $name)
{
return end($this->activeSections)->stopEvent($name);
}

/**
* Stops then restarts an event.
*
* @param string $name The event name
*
* @return StopwatchEvent
*/
public function lap($name)
public function lap(string $name)
{
return end($this->activeSections)->stopEvent($name)->start();
}

/**
* Returns a specific event by name.
*
* @param string $name The event name
*
* @return StopwatchEvent
*/
public function getEvent($name)
public function getEvent(string $name)
{
return end($this->activeSections)->getEvent($name);
}

/**
* Gets all events for a given section.
*
* @param string $id A section identifier
*
* @return StopwatchEvent[]
*/
public function getSectionEvents($id)
public function getSectionEvents(string $id)
{
return isset($this->sections[$id]) ? $this->sections[$id]->getEvents() : [];
}
Expand Down

0 comments on commit 7b354df

Please sign in to comment.