diff --git a/CompoundLimiter.php b/CompoundLimiter.php index 1c4f560..1dc0854 100644 --- a/CompoundLimiter.php +++ b/CompoundLimiter.php @@ -31,7 +31,7 @@ public function __construct(array $limiters) $this->limiters = $limiters; } - public function reserve(int $tokens = 1, float $maxTime = null): Reservation + public function reserve(int $tokens = 1, ?float $maxTime = null): Reservation { throw new ReserveNotSupportedException(__CLASS__); } diff --git a/Exception/MaxWaitDurationExceededException.php b/Exception/MaxWaitDurationExceededException.php index 53029ac..25a868d 100644 --- a/Exception/MaxWaitDurationExceededException.php +++ b/Exception/MaxWaitDurationExceededException.php @@ -20,7 +20,7 @@ class MaxWaitDurationExceededException extends \RuntimeException { private RateLimit $rateLimit; - public function __construct(string $message, RateLimit $rateLimit, int $code = 0, \Throwable $previous = null) + public function __construct(string $message, RateLimit $rateLimit, int $code = 0, ?\Throwable $previous = null) { parent::__construct($message, $code, $previous); diff --git a/Exception/RateLimitExceededException.php b/Exception/RateLimitExceededException.php index adbe576..15b52b9 100644 --- a/Exception/RateLimitExceededException.php +++ b/Exception/RateLimitExceededException.php @@ -20,7 +20,7 @@ class RateLimitExceededException extends \RuntimeException { private RateLimit $rateLimit; - public function __construct(RateLimit $rateLimit, int $code = 0, \Throwable $previous = null) + public function __construct(RateLimit $rateLimit, int $code = 0, ?\Throwable $previous = null) { parent::__construct('Rate Limit Exceeded', $code, $previous); diff --git a/Exception/ReserveNotSupportedException.php b/Exception/ReserveNotSupportedException.php index cb7a306..07a0fac 100644 --- a/Exception/ReserveNotSupportedException.php +++ b/Exception/ReserveNotSupportedException.php @@ -16,7 +16,7 @@ */ class ReserveNotSupportedException extends \BadMethodCallException { - public function __construct(string $limiterClass, int $code = 0, \Throwable $previous = null) + public function __construct(string $limiterClass, int $code = 0, ?\Throwable $previous = null) { parent::__construct(sprintf('Reserving tokens is not supported by "%s".', $limiterClass), $code, $previous); } diff --git a/LimiterInterface.php b/LimiterInterface.php index 6f0ae7d..f95cf8c 100644 --- a/LimiterInterface.php +++ b/LimiterInterface.php @@ -33,7 +33,7 @@ interface LimiterInterface * @throws ReserveNotSupportedException if this limiter implementation doesn't support reserving tokens * @throws \InvalidArgumentException if $tokens is larger than the maximum burst size */ - public function reserve(int $tokens = 1, float $maxTime = null): Reservation; + public function reserve(int $tokens = 1, ?float $maxTime = null): Reservation; /** * Use this method if you intend to drop if the required number diff --git a/Policy/FixedWindowLimiter.php b/Policy/FixedWindowLimiter.php index 6e71fe2..1d116e0 100644 --- a/Policy/FixedWindowLimiter.php +++ b/Policy/FixedWindowLimiter.php @@ -29,7 +29,7 @@ final class FixedWindowLimiter implements LimiterInterface private int $limit; private int $interval; - public function __construct(string $id, int $limit, \DateInterval $interval, StorageInterface $storage, LockInterface $lock = null) + public function __construct(string $id, int $limit, \DateInterval $interval, StorageInterface $storage, ?LockInterface $lock = null) { if ($limit < 1) { throw new \InvalidArgumentException(sprintf('Cannot set the limit of "%s" to 0, as that would never accept any hit.', __CLASS__)); @@ -42,7 +42,7 @@ public function __construct(string $id, int $limit, \DateInterval $interval, Sto $this->interval = TimeUtil::dateIntervalToSeconds($interval); } - public function reserve(int $tokens = 1, float $maxTime = null): Reservation + public function reserve(int $tokens = 1, ?float $maxTime = null): Reservation { if ($tokens > $this->limit) { throw new \InvalidArgumentException(sprintf('Cannot reserve more tokens (%d) than the size of the rate limiter (%d).', $tokens, $this->limit)); diff --git a/Policy/NoLimiter.php b/Policy/NoLimiter.php index 4878e4a..56339d3 100644 --- a/Policy/NoLimiter.php +++ b/Policy/NoLimiter.php @@ -25,7 +25,7 @@ */ final class NoLimiter implements LimiterInterface { - public function reserve(int $tokens = 1, float $maxTime = null): Reservation + public function reserve(int $tokens = 1, ?float $maxTime = null): Reservation { return new Reservation(microtime(true), new RateLimit(\PHP_INT_MAX, new \DateTimeImmutable(), true, \PHP_INT_MAX)); } diff --git a/Policy/SlidingWindowLimiter.php b/Policy/SlidingWindowLimiter.php index 468b0d0..fc9173d 100644 --- a/Policy/SlidingWindowLimiter.php +++ b/Policy/SlidingWindowLimiter.php @@ -37,7 +37,7 @@ final class SlidingWindowLimiter implements LimiterInterface private int $limit; private int $interval; - public function __construct(string $id, int $limit, \DateInterval $interval, StorageInterface $storage, LockInterface $lock = null) + public function __construct(string $id, int $limit, \DateInterval $interval, StorageInterface $storage, ?LockInterface $lock = null) { $this->storage = $storage; $this->lock = $lock; @@ -46,7 +46,7 @@ public function __construct(string $id, int $limit, \DateInterval $interval, Sto $this->interval = TimeUtil::dateIntervalToSeconds($interval); } - public function reserve(int $tokens = 1, float $maxTime = null): Reservation + public function reserve(int $tokens = 1, ?float $maxTime = null): Reservation { if ($tokens > $this->limit) { throw new \InvalidArgumentException(sprintf('Cannot reserve more tokens (%d) than the size of the rate limiter (%d).', $tokens, $this->limit)); diff --git a/Policy/TokenBucket.php b/Policy/TokenBucket.php index 517c669..f390599 100644 --- a/Policy/TokenBucket.php +++ b/Policy/TokenBucket.php @@ -32,7 +32,7 @@ final class TokenBucket implements LimiterStateInterface * @param Rate $rate the fill rate and time of this bucket * @param float|null $timer the current timer of the bucket, defaulting to microtime(true) */ - public function __construct(string $id, int $initialTokens, Rate $rate, float $timer = null) + public function __construct(string $id, int $initialTokens, Rate $rate, ?float $timer = null) { if ($initialTokens < 1) { throw new \InvalidArgumentException(sprintf('Cannot set the limit of "%s" to 0, as that would never accept any hit.', TokenBucketLimiter::class)); diff --git a/Policy/TokenBucketLimiter.php b/Policy/TokenBucketLimiter.php index d1ebeb2..dec4720 100644 --- a/Policy/TokenBucketLimiter.php +++ b/Policy/TokenBucketLimiter.php @@ -28,7 +28,7 @@ final class TokenBucketLimiter implements LimiterInterface private int $maxBurst; private Rate $rate; - public function __construct(string $id, int $maxBurst, Rate $rate, StorageInterface $storage, LockInterface $lock = null) + public function __construct(string $id, int $maxBurst, Rate $rate, StorageInterface $storage, ?LockInterface $lock = null) { $this->id = $id; $this->maxBurst = $maxBurst; @@ -50,7 +50,7 @@ public function __construct(string $id, int $maxBurst, Rate $rate, StorageInterf * @throws MaxWaitDurationExceededException if $maxTime is set and the process needs to wait longer than its value (in seconds) * @throws \InvalidArgumentException if $tokens is larger than the maximum burst size */ - public function reserve(int $tokens = 1, float $maxTime = null): Reservation + public function reserve(int $tokens = 1, ?float $maxTime = null): Reservation { if ($tokens > $this->maxBurst) { throw new \InvalidArgumentException(sprintf('Cannot reserve more tokens (%d) than the burst size of the rate limiter (%d).', $tokens, $this->maxBurst)); diff --git a/Policy/Window.php b/Policy/Window.php index 42d00de..6a103fc 100644 --- a/Policy/Window.php +++ b/Policy/Window.php @@ -26,7 +26,7 @@ final class Window implements LimiterStateInterface private int $maxSize; private float $timer; - public function __construct(string $id, int $intervalInSeconds, int $windowSize, float $timer = null) + public function __construct(string $id, int $intervalInSeconds, int $windowSize, ?float $timer = null) { $this->id = $id; $this->intervalInSeconds = $intervalInSeconds; @@ -44,7 +44,7 @@ public function getExpirationTime(): ?int return $this->intervalInSeconds; } - public function add(int $hits = 1, float $now = null): void + public function add(int $hits = 1, ?float $now = null): void { $now ??= microtime(true); if (($now - $this->timer) > $this->intervalInSeconds) { diff --git a/RateLimiterFactory.php b/RateLimiterFactory.php index 1b397ac..e9afcb0 100644 --- a/RateLimiterFactory.php +++ b/RateLimiterFactory.php @@ -30,7 +30,7 @@ final class RateLimiterFactory private StorageInterface $storage; private ?LockFactory $lockFactory; - public function __construct(array $config, StorageInterface $storage, LockFactory $lockFactory = null) + public function __construct(array $config, StorageInterface $storage, ?LockFactory $lockFactory = null) { $this->storage = $storage; $this->lockFactory = $lockFactory; @@ -41,7 +41,7 @@ public function __construct(array $config, StorageInterface $storage, LockFactor $this->config = $options->resolve($config); } - public function create(string $key = null): LimiterInterface + public function create(?string $key = null): LimiterInterface { $id = $this->config['id'].'-'.$key; $lock = $this->lockFactory?->createLock($id); diff --git a/Tests/Policy/TokenBucketLimiterTest.php b/Tests/Policy/TokenBucketLimiterTest.php index 94a8ca1..8ed21c2 100644 --- a/Tests/Policy/TokenBucketLimiterTest.php +++ b/Tests/Policy/TokenBucketLimiterTest.php @@ -182,7 +182,7 @@ public function testBucketRefilledWithStrictFrequency() } } - private function createLimiter($initialTokens = 10, Rate $rate = null) + private function createLimiter($initialTokens = 10, ?Rate $rate = null) { return new TokenBucketLimiter('test', $initialTokens, $rate ?? Rate::perSecond(10), $this->storage); }