diff --git a/.gitignore b/.gitignore index 3bf6283..d2f34cd 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,4 @@ vendor .php_cs.cache coverage .phpunit* +*.cache diff --git a/.php-cs-fixer.dist.php b/.php-cs-fixer.dist.php new file mode 100644 index 0000000..0413484 --- /dev/null +++ b/.php-cs-fixer.dist.php @@ -0,0 +1,21 @@ + true, + ]); + } +}; + +$config->getFinder() + ->in(__DIR__ . '/lib') + ->in(__DIR__ . '/test') + ->in(__DIR__ . '/examples'); + +$cacheDir = getenv('TRAVIS') ? getenv('HOME') . '/.php-cs-fixer' : __DIR__; + +$config->setCacheFile($cacheDir . '/.php_cs.cache'); + +return $config; diff --git a/.php_cs.dist b/.php_cs.dist deleted file mode 100644 index 8d02bce..0000000 --- a/.php_cs.dist +++ /dev/null @@ -1,13 +0,0 @@ -getFinder() - ->in(__DIR__ . '/examples') - ->in(__DIR__ . '/lib') - ->in(__DIR__ . '/test'); - -$cacheDir = getenv('TRAVIS') ? getenv('HOME') . '/.php-cs-fixer' : __DIR__; - -$config->setCacheFile($cacheDir . '/.php_cs.cache'); - -return $config; diff --git a/examples/1. Basic/GenericLoop.php b/examples/1. Basic/GenericLoop.php index a41ba0f..e79d04f 100644 --- a/examples/1. Basic/GenericLoop.php +++ b/examples/1. Basic/GenericLoop.php @@ -17,15 +17,15 @@ for ($x = 0; $x < 10; $x++) { }; $loop = new GenericLoop($callable, "Loop number $x"); $loop->start(); - delay(100); + delay(0.1); $loops []= $loop; } -delay(5000); +delay(5); echo "Resuming prematurely all loops!".PHP_EOL; foreach ($loops as $loop) { $loop->resume(); } echo "OK done, waiting 5 more seconds!".PHP_EOL; -delay(5000); +delay(5); echo "Closing all loops!".PHP_EOL; -delay(10); +delay(0.01); diff --git a/examples/1. Basic/PeriodicLoop.php b/examples/1. Basic/PeriodicLoop.php index 946ffb1..b1a8cea 100644 --- a/examples/1. Basic/PeriodicLoop.php +++ b/examples/1. Basic/PeriodicLoop.php @@ -17,15 +17,15 @@ for ($x = 0; $x < 10; $x++) { }; $loop = new PeriodicLoop($callable, "Loop number $x", 1000); $loop->start(); - delay(100); + delay(0.1); $loops []= $loop; } -delay(5000); +delay(5); echo "Resuming prematurely all loops!".PHP_EOL; foreach ($loops as $loop) { $loop->resume(); } echo "OK done, waiting 5 more seconds!".PHP_EOL; -delay(5000); +delay(5); echo "Closing all loops!".PHP_EOL; -delay(10); +delay(0.01); diff --git a/examples/2. Advanced/Loop.php b/examples/2. Advanced/Loop.php index d518ac5..80ed892 100644 --- a/examples/2. Advanced/Loop.php +++ b/examples/2. Advanced/Loop.php @@ -77,13 +77,13 @@ class MyLoop extends Loop } $function = function (int $number) { - delay(1000); + delay(1); return $number + 1; }; $loops = []; for ($x = 0; $x < 10; $x++) { $loop = new MyLoop($function, "Loop number $x"); $loop->start(); - delay(100); + delay(0.1); $loops []= $loop; } diff --git a/examples/2. Advanced/ResumableLoop.php b/examples/2. Advanced/ResumableLoop.php index dbaccc8..cd053d4 100644 --- a/examples/2. Advanced/ResumableLoop.php +++ b/examples/2. Advanced/ResumableLoop.php @@ -71,10 +71,10 @@ $loops = []; for ($x = 0; $x < 10; $x++) { $loop = new MyLoop("Loop number $x"); $loop->start(); - delay(100); + delay(0.1); $loops []= $loop; } -delay(5000); +delay(5); echo "Resuming prematurely all loops!".PHP_EOL; foreach ($loops as $loop) { $loop->resume(); diff --git a/examples/2. Advanced/ResumableSignalLoop.php b/examples/2. Advanced/ResumableSignalLoop.php index 5ba32b9..5b6d455 100644 --- a/examples/2. Advanced/ResumableSignalLoop.php +++ b/examples/2. Advanced/ResumableSignalLoop.php @@ -54,16 +54,16 @@ $loops = []; for ($x = 0; $x < 10; $x++) { $loop = new ResSigLoop("Loop number $x"); $loop->start(); - delay(100); + delay(0.1); $loops []= $loop; } -delay(5000); +delay(5); echo "Resuming prematurely all loops!".PHP_EOL; foreach ($loops as $loop) { $loop->resume(); } echo "OK done, waiting 5 more seconds!".PHP_EOL; -delay(5000); +delay(5); echo "Closing all loops!".PHP_EOL; foreach ($loops as $loop) { $loop->signal(true); diff --git a/examples/2. Advanced/SignalLoop.php b/examples/2. Advanced/SignalLoop.php index 32aae37..1d43281 100644 --- a/examples/2. Advanced/SignalLoop.php +++ b/examples/2. Advanced/SignalLoop.php @@ -5,6 +5,7 @@ require 'vendor/autoload.php'; use Amp\Loop; use danog\Loop\SignalLoop; +use function Amp\async; use function Amp\delay; class SigLoop extends SignalLoop @@ -31,7 +32,7 @@ class SigLoop extends SignalLoop { $number = 0; while (true) { - if ($this->waitSignal(delay(1000))) { + if ($this->waitSignal(async(delay(...), 1))) { echo "Got exit signal in $this!".PHP_EOL; return; } @@ -53,10 +54,10 @@ $loops = []; for ($x = 0; $x < 10; $x++) { $loop = new SigLoop("Loop number $x"); $loop->start(); - delay(100); + delay(0.1); $loops []= $loop; } -delay(5000); +delay(5); echo "Closing all loops!".PHP_EOL; foreach ($loops as $loop) { $loop->signal(true); diff --git a/lib/Generic/GenericLoop.php b/lib/Generic/GenericLoop.php index 8570347..4075d52 100644 --- a/lib/Generic/GenericLoop.php +++ b/lib/Generic/GenericLoop.php @@ -51,9 +51,7 @@ class GenericLoop extends ResumableSignalLoop /** * Callable. * - * @var callable - * - * @psalm-var callable():TCallableReturn + * @var callable():TCallableReturn */ protected $callable; /** @@ -91,7 +89,7 @@ class GenericLoop extends ResumableSignalLoop } elseif ($timeout > 0) { $this->reportPause($timeout); } - if ($timeout === self::STOP || $this->waitSignal(async(fn () => $this->pause($timeout)))) { + if ($timeout === self::STOP || $this->waitSignal(async($this->pause(...), $timeout))) { break; } } diff --git a/lib/Generic/PeriodicLoop.php b/lib/Generic/PeriodicLoop.php index 2697ad5..f170e47 100644 --- a/lib/Generic/PeriodicLoop.php +++ b/lib/Generic/PeriodicLoop.php @@ -34,30 +34,17 @@ class PeriodicLoop extends ResumableSignalLoop * @psalm-var callable():(bool|Future) */ private $callback; - /** - * Loop name. - * - * @var string - */ - private $name; - /** - * Loop interval. - * - * @var ?int - */ - private $interval; /** * Constructor. * * If possible, the callable will be bound to the current instance of the loop. * - * @param callable $callback Callback to call + * @param callable():(bool|Future) $callback Callable to run * @param string $name Loop name * @param ?int $interval Loop interval * - * @psalm-param callable():(bool|Future) $callback Callable to run */ - public function __construct(callable $callback, string $name, ?int $interval) + public function __construct(callable $callback, private string $name, private ?int $interval) { if ($callback instanceof \Closure) { try { @@ -72,7 +59,6 @@ class PeriodicLoop extends ResumableSignalLoop } /** * Loop implementation. - * */ public function loop(): void { @@ -86,7 +72,7 @@ class PeriodicLoop extends ResumableSignalLoop break; } /** @var ?bool */ - $result = $this->waitSignal(async(fn () => $this->pause($this->interval))); + $result = $this->waitSignal(async($this->pause(...), $this->interval)); if ($result === true) { break; } diff --git a/test/ResumableTest.php b/test/ResumableTest.php index aea7bf6..8c0e856 100644 --- a/test/ResumableTest.php +++ b/test/ResumableTest.php @@ -36,9 +36,9 @@ class ResumableTest extends Fixtures $this->assertTrue($loop->start()); $this->assertAfterStart($loop); - delay(10); + delay(0.010); $this->assertTrue(self::isResolved($paused)); - delay(100); + delay(0.1); $this->assertFinal($loop); } @@ -59,7 +59,7 @@ class ResumableTest extends Fixtures $this->assertPreStart($loop); $this->assertTrue($loop->start()); - delay(1); + delay(0.001); $this->assertFalse(self::isResolved($paused)); // Did not pause // Invert the order as the afterTest assertions will begin the test anew @@ -81,7 +81,7 @@ class ResumableTest extends Fixtures { $paused = $deferred ? $loop->resumeDefer() : $loop->resume(); // Will resolve on next pause if ($deferred) { - delay(1); // Avoid resuming after starting + delay(0.001); // Avoid resuming after starting } $loop->setInterval($interval); @@ -89,17 +89,17 @@ class ResumableTest extends Fixtures $this->assertTrue($loop->start()); $this->assertAfterStart($loop); - delay(1); + delay(0.001); $this->assertTrue(self::isResolved($paused)); // Did pause $paused = $deferred ? $loop->resumeDefer() : $loop->resume(); if ($deferred) { $this->assertAfterStart($loop); - delay(1); + delay(0.001); } $this->assertFinal($loop); - delay(1); + delay(0.001); $this->assertFalse(self::isResolved($paused)); // Did not pause again } @@ -116,24 +116,24 @@ class ResumableTest extends Fixtures { $paused1 = $loop->resumeDeferOnce(); // Will resolve on next pause $paused2 = $loop->resumeDeferOnce(); // Will resolve on next pause - delay(1); // Avoid resuming after starting + delay(0.001); // Avoid resuming after starting $loop->setInterval(10000); $this->assertPreStart($loop); $this->assertTrue($loop->start()); $this->assertAfterStart($loop); - delay(1); + delay(0.001); $this->assertTrue(self::isResolved($paused1)); // Did pause $this->assertTrue(self::isResolved($paused2)); // Did pause $paused1 = $loop->resumeDeferOnce(); $paused2 = $loop->resumeDeferOnce(); $this->assertAfterStart($loop); - delay(1); + delay(0.001); $this->assertFinal($loop); - delay(1); + delay(0.001); $this->assertFalse(self::isResolved($paused1)); // Did not pause again $this->assertFalse(self::isResolved($paused2)); // Did not pause again } diff --git a/test/Traits/Basic.php b/test/Traits/Basic.php index 2e248aa..82357c5 100644 --- a/test/Traits/Basic.php +++ b/test/Traits/Basic.php @@ -48,7 +48,7 @@ trait Basic public function loop(): void { $this->inited = true; - delay(100); + delay(0.1); $this->ran = true; } /** diff --git a/test/Traits/Signal.php b/test/Traits/Signal.php index 6913edb..e562c44 100644 --- a/test/Traits/Signal.php +++ b/test/Traits/Signal.php @@ -49,7 +49,7 @@ trait Signal */ private function testGenerator(int $interval): void { - delay($interval); + delay($interval/1000); } /** * Loop implementation.