diff --git a/.State b/.State deleted file mode 100644 index a604de4..0000000 --- a/.State +++ /dev/null @@ -1 +0,0 @@ -finalized diff --git a/Bench.php b/Source/Bench.php similarity index 69% rename from Bench.php rename to Source/Bench.php index 791a574..f27ec5b 100644 --- a/Bench.php +++ b/Source/Bench.php @@ -1,5 +1,7 @@ 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]); @@ -135,8 +124,6 @@ public function __unset($id) /** * Destroy all mark. - * - * @return void */ public function unsetAll() { @@ -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 = []; @@ -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) { @@ -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); @@ -279,10 +197,8 @@ public function filter($callable) /** * Return all filters. - * - * @return array */ - public function getFilters() + public function getFilters(): array { return $this->_filters; } @@ -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 []; @@ -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; @@ -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 ''; @@ -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(); } @@ -423,4 +325,4 @@ public function __toString() /** * Flex entity. */ -Consistency::flexEntity('Hoa\Bench\Bench'); +Consistency::flexEntity(Bench::class); diff --git a/Exception.php b/Source/Exception.php similarity index 95% rename from Exception.php rename to Source/Exception.php index f3707d8..72158ea 100644 --- a/Exception.php +++ b/Source/Exception.php @@ -1,5 +1,7 @@ setId($id); @@ -112,11 +109,8 @@ public function __construct($id) /** * Set the mark ID. - * - * @param string $id The mark ID. - * @return string */ - protected function setId($id) + protected function setId(?string $id): ?string { $old = $this->_id; $this->_id = $id; @@ -126,10 +120,8 @@ protected function setId($id) /** * Get the mark ID. - * - * @return string */ - public function getId() + public function getId(): ?string { return $this->_id; } @@ -138,11 +130,8 @@ public function getId() * Start the mark. * A mark can be started if it is in pause, stopped, or if it is the first start. * Else, an exception will be thrown. - * - * @return \Hoa\Bench\Mark - * @throws \Hoa\Bench\Exception */ - public function start() + public function start(): self { if (true === $this->isStarted()) { if (false === $this->isPause()) { @@ -171,13 +160,8 @@ public function start() * Stop the mark. * A mark can be stopped if it is in pause, or started. Else, an exception * will be thrown (or not, according to the $silent argument). - * - * @param bool $silent If set to true and if the mark is not started, - * no exception will be thrown. - * @return \Hoa\Bench\Mark - * @throws \Hoa\Bench\Exception */ - public function stop($silent = false) + public function stop(bool $silent = false): self { if (false === $this->isStarted()) { if (false === $silent) { @@ -200,10 +184,8 @@ public function stop($silent = false) /** * Reset the mark. - * - * @return \Hoa\Bench\Mark */ - public function reset() + public function reset(): self { $this->start = 0.0; $this->stop = 0.0; @@ -218,14 +200,8 @@ public function reset() * Pause the mark. * A mark can be in pause if it is started. Else, an exception will be * thrown (or not, according to the $silent argument). - * - * @param bool $silent If set to true and the mark is not started, - * no exception will be throw. Idem if the mark - * is in pause. - * @return \Hoa\Bench\Mark - * @throws \Hoa\Bench\Exception */ - public function pause($silent = false) + public function pause(bool $silent = false): self { if (false === $this->isStarted()) { if (false === $silent) { @@ -261,10 +237,8 @@ public function pause($silent = false) * Get the difference between $stop and $start. * If the mark is still running (it contains the pause case), the current * microtime will be used in stay of $stop. - * - * @return float */ - public function diff() + public function diff(): float { if (false === $this->isStarted() || true === $this->isPause()) { return $this->stop - $this->start - $this->pause; @@ -278,11 +252,8 @@ public function diff() * $a op $b : return -1 if $a < $b, 0 if $a == $b, and 1 if $a > $b. We * compare the difference between $start and $stop, i.e. we call the diff() * method. - * - * @param \Hoa\Bench\Mark $mark The mark to compare to. - * @return int */ - public function compareTo(Mark $mark) + public function compareTo(self $mark): int { $a = $this->diff(); $b = $mark->diff(); @@ -300,39 +271,32 @@ public function compareTo(Mark $mark) * Check if the mark is running. * * @deprecated use `isStarted` instead - * @return bool */ - public function isRunning() + public function isRunning(): bool { return $this->isStarted(); } /** * Check if the mark is started. - * - * @return bool */ - public function isStarted() + public function isStarted(): bool { return $this->_started; } /** * Check if the mark is in pause. - * - * @return bool */ - public function isPause() + public function isPause(): bool { return $this->_pause; } /** * Alias of the diff() method, but return a string, not a float. - * - * @return string */ - public function __toString() + public function __toString(): string { return (string) $this->diff(); } diff --git a/Test/Unit/Mark.php b/Test/Unit/Mark.php index eedecab..65fd7da 100644 --- a/Test/Unit/Mark.php +++ b/Test/Unit/Mark.php @@ -1,5 +1,7 @@ object( - $mark = new CUT($id = uniqid()) - )->string($mark->getId())->isEqualTo($id) - ->boolean($mark->isRunning())->isFalse() - ->boolean($mark->isPause())->isFalse() + ->given($id = 'foo') + ->when($mark = new CUT($id)) + ->then + ->string($mark->getId()) + ->isEqualTo($id) + ->boolean($mark->isRunning()) + ->isFalse() + ->boolean($mark->isPause()) + ->isFalse() + ->assert('Start mark') + ->when($SUT = $mark->start()) - ->object($SUT)->isInstanceOf('Hoa\Bench\Mark') - ->boolean($mark->isRunning())->isTrue() - ->boolean($mark->isPause())->isFalse() + ->object($SUT) + ->isInstanceOf(CUT::class) + ->boolean($mark->isRunning()) + ->isTrue() + ->boolean($mark->isPause()) + ->isFalse() + ->assert('Pause mark') + ->when($SUT = $mark->pause()) - ->object($SUT)->isInstanceOf('Hoa\Bench\Mark') - ->boolean($mark->isRunning())->isTrue() - ->boolean($mark->isPause())->isTrue() + ->object($SUT) + ->isInstanceOf(CUT::class) + ->boolean($mark->isRunning()) + ->isTrue() + ->boolean($mark->isPause()) + ->isTrue() + ->assert('Restart mark after pause') + ->when($SUT = $mark->start()) - ->object($SUT)->isInstanceOf('Hoa\Bench\Mark') - ->boolean($mark->isRunning())->isTrue() - ->boolean($mark->isPause())->isFalse() + ->object($SUT) + ->isInstanceOf(CUT::class) + ->boolean($mark->isRunning()) + ->isTrue() + ->boolean($mark->isPause()) + ->isFalse() + ->assert('Stop mark') + ->when($SUT = $mark->stop()) - ->object($SUT)->isInstanceOf('Hoa\Bench\Mark') - ->boolean($mark->isRunning())->isFalse() - ->boolean($mark->isPause())->isFalse() - ; + ->object($SUT) + ->isInstanceOf(CUT::class) + ->boolean($mark->isRunning()) + ->isFalse() + ->boolean($mark->isPause()) + ->isFalse(); } } diff --git a/composer.json b/composer.json index d48bd79..43af3fa 100644 --- a/composer.json +++ b/composer.json @@ -24,16 +24,18 @@ "source": "https://central.hoa-project.net/Resource/Library/Bench" }, "require": { - "hoa/consistency": "~1.0", - "hoa/exception" : "~1.0", - "hoa/iterator" : "~2.0" + "php" : ">=7.1", + "hoa/consistency": "dev-master", + "hoa/exception" : "dev-master", + "hoa/iterator" : "dev-master" }, "require-dev": { - "hoa/test" : "~2.0" + "hoa/test" : "dev-master" }, "autoload": { "psr-4": { - "Hoa\\Bench\\": "." + "Hoa\\Bench\\" : "Source", + "Hoa\\Bench\\Test\\": "Test" } }, "extra": {