Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
"name": "zwirek/react-timer-handler",
"type": "library",
"description": "Simple handler for ReactPHP timers",
"keywords": ["ReactPHP", "Timer", "react", "handler"],
"keywords": ["ReactPHP", "Timer", "react", "handler", "php", "async"],
"license": "MIT",
"authors": [
{
"name": "Zbigniew Żwirek",
"email": "git.srv@zwirek.net"
"email": "github.srv@zwirek.net"
}
],
"autoload": {
Expand All @@ -21,10 +21,10 @@
}
},
"require": {
"php": "^7.1",
"php": ">=8.1",
"react/event-loop": "^1.1"
},
"require-dev": {
"phpunit/phpunit": "^8.2 || ^7.0 || ^6.4 || ^5.7"
"phpunit/phpunit": "^8.2"
}
}
12 changes: 6 additions & 6 deletions src/TimerHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,17 @@ final class TimerHandler implements TimerHandlerInterface
/**
* @var LoopInterface
*/
private $loop;
private LoopInterface $loop;

/**
* @var TimerInterface[]
*/
private $timers = [];
private array $timers = [];

/**
* @var int[]
*/
private $limitedPeriodicTimerCounters = [];
private array $limitedPeriodicTimerCounters = [];

public function __construct(LoopInterface $loop)
{
Expand All @@ -43,7 +43,7 @@ public function addTimer(string $name, $interval, callable $callback): bool
$this->timers[$name] = null;
unset($this->timers[$name]);

call_user_func($callback, $timer);
$callback($timer);
});

return true;
Expand Down Expand Up @@ -106,9 +106,9 @@ public function addLimitedPeriodicTimer(string $name, $interval, callable $callb
$this->limitedPeriodicTimerCounters[$name] = 0;

$this->timers[$name] = $this->loop->addPeriodicTimer($interval, function (TimerInterface $timer) use ($callback, $name, $callLimit) {
call_user_func($callback, $timer);
$callback($timer);

if (++$this->limitedPeriodicTimerCounters >= $callLimit) {
if (++$this->limitedPeriodicTimerCounters[$name] >= $callLimit) {
$this->loop->cancelTimer($timer);
$this->timers[$name] = null;
unset($this->timers[$name]);
Expand Down
6 changes: 3 additions & 3 deletions tests/TimerHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,10 @@ public function shouldPassBindParameters()
{
$timerHandler = new TimerHandler($this->loop);

$parameter = 'binded parameter';
$parameter = 'bound parameter';

$timerHandler->addTimer('test', 0.005, function() use ($parameter) {
self::assertEquals('bind parameter', $parameter);
self::assertEquals('bound parameter', $parameter);

$this->assertLoop();
});
Expand Down Expand Up @@ -157,7 +157,7 @@ public function shouldCallTimerAFewTimes()
$timerHandler->addLimitedPeriodicTimer('limited', 0.005, function() use (&$counter) {
$counter++;

if ($counter = 5) {
if ($counter === 5) {
$this->assertLoop();
}
}, 5);
Expand Down