Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 2 commits into from Jul 29, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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)
Tobion marked this conversation as resolved.
Show resolved Hide resolved
{
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)
Tobion marked this conversation as resolved.
Show resolved Hide resolved
{
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)
Tobion marked this conversation as resolved.
Show resolved Hide resolved
{
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