Skip to content
This repository was archived by the owner on Sep 20, 2021. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .State

This file was deleted.

142 changes: 22 additions & 120 deletions Bench.php → Source/Bench.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

/**
* Hoa
*
Expand Down Expand Up @@ -42,15 +44,12 @@
/**
* Class \Hoa\Bench.
*
* The \Hoa\Bench class allows to manage marks easily, and to make some
* The Hoa\Bench class allows to manage marks easily, and to make some
* statistics.
* The \Hoa\Bench class implements Iterator and Countable interfaces to iterate
* The Hoa\Bench class implements Iterator and Countable interfaces to iterate
* marks, or count the number of marks.
*
* @copyright Copyright © 2007-2017 Hoa community
* @license New BSD License
*/
class Bench implements Iterator, \Countable
class Bench implements Iterator\Aggregate, \Countable
{
/**
* Statistic : get the result.
Expand Down Expand Up @@ -85,12 +84,8 @@ class Bench implements Iterator, \Countable
/**
* Get a mark.
* If the mark does not exist, it will be automatically create.
*
* @param string $id The mark ID.
* @return \Hoa\Bench\Mark
* @throws \Hoa\Bench\Exception
*/
public function __get($id)
public function __get(string $id): Mark
{
if (true === empty(self::$_mark)) {
$global = new Mark(Mark::GLOBAL_NAME);
Expand All @@ -111,22 +106,16 @@ public function __get($id)
/**
* Check if a mark exists.
* Alias of the protected markExist method.
*
* @param string $id The mark ID.
* @return bool
*/
public function __isset($id)
public function __isset(string $id): bool
{
return $this->markExists($id);
}

/**
* Destroy a mark.
*
* @param string $id The mark ID.
* @return void
*/
public function __unset($id)
public function __unset(string $id)
{
unset(self::$_mark[$id]);

Expand All @@ -135,8 +124,6 @@ public function __unset($id)

/**
* Destroy all mark.
*
* @return void
*/
public function unsetAll()
{
Expand All @@ -147,89 +134,26 @@ public function unsetAll()

/**
* Check if a mark already exists.
*
* @param string $id The mark ID.
* @return bool
*/
protected function markExists($id)
protected function markExists(string $id): bool
{
return isset(self::$_mark[$id]);
}

/**
* Get the current mark for the iterator.
*
* @return \Hoa\Bench\Mark
*/
public function current()
{
return current(self::$_mark);
}

/**
* Get the current mark ID for the iterator.
*
* @return string
*/
public function key()
{
return key(self::$_mark);
}

/**
* Advance the internal mark collection pointer, and return the current
* mark.
*
* @return \Hoa\Bench\Mark
*/
public function next()
{
return next(self::$_mark);
}

/**
* Rewind the internal mark collection pointer, and return the first mark.
*
* @return \Hoa\Bench\Mark
*/
public function rewind()
{
return reset(self::$_mark);
}

/**
* Check if there is a current element after calls the rewind or the next
* methods.
*
* @return bool
* Iterate over marks.
*/
public function valid()
public function getIterator(): \Traversable
{
if (empty(self::$_mark)) {
return false;
}

$key = key(self::$_mark);
$return = (next(self::$_mark) ? true : false);
prev(self::$_mark);

if (false === $return) {
end(self::$_mark);

if ($key === key(self::$_mark)) {
$return = true;
}
foreach (self::$_mark as $mark) {
yield $mark;
}

return $return;
}

/**
* Pause all marks and return only previously started tasks.
*
* @return array
*/
public function pause()
public function pause(): array
{
$startedMarks = [];

Expand All @@ -248,9 +172,6 @@ public function pause()

/**
* Resume a specific set of marks.
*
* @param array $marks The marks to resume.
* @return void
*/
public static function resume(array $marks)
{
Expand All @@ -266,11 +187,8 @@ public static function resume(array $marks)
* Used in the self::getStatistic() method, no in iterator.
* A filter is a callable that will receive 3 values about a mark: ID, time
* result, and time pourcent. The callable must return a boolean.
*
* @param mixed $callable Callable.
* @return void
*/
public function filter($callable)
public function filter($callable): self
{
$this->_filters[] = xcallable($callable);

Expand All @@ -279,10 +197,8 @@ public function filter($callable)

/**
* Return all filters.
*
* @return array
*/
public function getFilters()
public function getFilters(): array
{
return $this->_filters;
}
Expand All @@ -292,12 +208,8 @@ public function getFilters()
* Return an associative array : id => sub-array. The sub-array contains the
* result time in second (given by the constant self::STAT_RESULT), and the
* result pourcent (given by the constant self::START_POURCENT).
*
* @param bool $considerFilters Whether we should consider filters or
* not.
* @return array
*/
public function getStatistic($considerFilters = true)
public function getStatistic(bool $considerFilters = true): array
{
if (empty(self::$_mark)) {
return [];
Expand Down Expand Up @@ -333,10 +245,8 @@ public function getStatistic($considerFilters = true)

/**
* Get the maximum, i.e. the longest mark in time.
*
* @return \Hoa\Bench\Mark
*/
public function getLongest()
public function getLongest(): Mark
{
$max = 0;
$outMark = null;
Expand All @@ -353,12 +263,8 @@ public function getLongest()

/**
* Draw statistic in text mode.
*
* @param int $width The graphic width.
* @return string
* @throws \Hoa\Bench\Exception
*/
public function drawStatistic($width = 80)
public function drawStatistic(int $width = 80): string
{
if (empty(self::$_mark)) {
return '';
Expand Down Expand Up @@ -401,20 +307,16 @@ public function drawStatistic($width = 80)

/**
* Count the number of mark.
*
* @return int
*/
public function count()
public function count(): int
{
return count(self::$_mark);
}

/**
* Alias of drawStatistic() method.
*
* @return string
*/
public function __toString()
public function __toString(): string
{
return $this->drawStatistic();
}
Expand All @@ -423,4 +325,4 @@ public function __toString()
/**
* Flex entity.
*/
Consistency::flexEntity('Hoa\Bench\Bench');
Consistency::flexEntity(Bench::class);
5 changes: 2 additions & 3 deletions Exception.php → Source/Exception.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

/**
* Hoa
*
Expand Down Expand Up @@ -42,9 +44,6 @@
* Class \Hoa\Bench\Exception.
*
* Extending the \Hoa\Exception\Exception class.
*
* @copyright Copyright © 2007-2017 Hoa community
* @license New BSD License
*/
class Exception extends HoaException
{
Expand Down
Loading