Skip to content

Commit

Permalink
Ensure usleep() always receives an integer
Browse files Browse the repository at this point in the history
  • Loading branch information
FelipeAzambuja authored and duncan3dc committed Oct 30, 2024
1 parent 70a0e1b commit f1fb14c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/TerminalObject/Helper/Sleeper.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class Sleeper implements SleeperInterface
/**
* The default length of the sleep
*
* @var int|float $speed
* @var int $speed
*/
protected $speed = 50000;

Expand All @@ -16,12 +16,12 @@ class Sleeper implements SleeperInterface
*
* @param int|float $percentage
*
* @return float
* @return int
*/
public function speed($percentage)
{
if (is_numeric($percentage) && $percentage > 0) {
$this->speed *= (100 / $percentage);
$this->speed = (int) round($this->speed * (100 / $percentage));
}

return $this->speed;
Expand Down
12 changes: 12 additions & 0 deletions tests/SleeperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,16 @@ public function it_will_ignore_zero_percentages()

$sleeper->sleep();
}


/**
* @test
*/
public function it_uses_whole_integers_only()
{
$sleeper = new Sleeper();

$result = $sleeper->speed(33);
self::assertSame(151515, $result);
}
}

0 comments on commit f1fb14c

Please sign in to comment.