Skip to content

Commit bed1714

Browse files
committed
Add some style fixes to timer methods
1 parent 1567309 commit bed1714

File tree

4 files changed

+34
-4
lines changed

4 files changed

+34
-4
lines changed

cli/server.php

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
require __DIR__ . '/../src/Connection.php';
44
require __DIR__ . '/../src/Socket.php';
55
require __DIR__ . '/../src/Server.php';
6+
require __DIR__ . '/../src/Timer.php';
7+
require __DIR__ . '/../src/TimerCollection.php';
68

79
require __DIR__ . '/../src/Application/ApplicationInterface.php';
810
require __DIR__ . '/../src/Application/Application.php';

src/Server.php

+6
Original file line numberDiff line numberDiff line change
@@ -499,6 +499,12 @@ public function getMaxClients(): int
499499
return $this->maxClients;
500500
}
501501

502+
/**
503+
* Adds a periodic timer.
504+
*
505+
* @param int $interval Interval in microseconds.
506+
* @param callable $task
507+
*/
502508
public function addTimer(int $interval, callable $task): void
503509
{
504510
$this->timers->addTimer(new Timer($interval, $task));

src/Timer.php

+17-3
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,19 @@
66

77
final class Timer
88
{
9+
/**
10+
* @var int $interval
11+
*/
912
private $interval;
13+
14+
/**
15+
* @var callable $task
16+
*/
1017
private $task;
18+
19+
/**
20+
* @var int $lastRun
21+
*/
1122
private $lastRun;
1223

1324
public function __construct(int $interval, callable $task)
@@ -17,6 +28,11 @@ public function __construct(int $interval, callable $task)
1728
$this->lastRun = 0;
1829
}
1930

31+
/**
32+
* Executes the timer if intervall has passed.
33+
*
34+
* @return void
35+
*/
2036
public function run(): void
2137
{
2238
$now = round(microtime(true) * 1000);
@@ -25,8 +41,6 @@ public function run(): void
2541
}
2642

2743
$this->lastRun = $now;
28-
29-
$task = $this->task;
30-
$task();
44+
call_user_func($this->task);
3145
}
3246
}

src/TimerCollection.php

+9-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
final class TimerCollection
88
{
99
/**
10-
* @var Timer[]
10+
* @var array $timers
1111
*/
1212
private $timers;
1313

@@ -16,11 +16,19 @@ public function __construct(array $timers = [])
1616
$this->timers = $timers;
1717
}
1818

19+
/**
20+
* Adds a timer.
21+
*
22+
* @param Timer $timer
23+
*/
1924
public function addTimer(Timer $timer)
2025
{
2126
$this->timers[] = $timer;
2227
}
2328

29+
/**
30+
* Executes/runs all timers.
31+
*/
2432
public function runAll(): void
2533
{
2634
foreach ($this->timers as $timer) {

0 commit comments

Comments
 (0)