44
55namespace Saloon \RateLimitPlugin ;
66
7- use Closure ;
87use DateInterval ;
98use DateTimeImmutable ;
109use Saloon \Http \Response ;
@@ -40,7 +39,7 @@ class Limit
4039 /**
4140 * The threshold that should be used when determining if a limit has been reached
4241 *
43- * Must be between 0 and 1. For example if you want the limiter to kick in at 85%
42+ * Must be between 0 and 1. For example, if you want the limiter to kick in at 85%
4443 * you must set the threshold to 0.85
4544 */
4645 protected float $ threshold = 1 ;
@@ -58,8 +57,10 @@ class Limit
5857
5958 /**
6059 * Custom response handler
60+ *
61+ * @var null|callable(): mixed
6162 */
62- protected ? Closure $ responseHandler = null ;
63+ protected $ responseHandler = null ;
6364
6465 /**
6566 * Determines if we should sleep or not
@@ -69,15 +70,15 @@ class Limit
6970 /**
7071 * @param (callable(): mixed)|null $responseHandler
7172 */
72- final public function __construct (int $ allow , float $ threshold = 1 , callable $ responseHandler = null )
73+ final public function __construct (int $ allow , float $ threshold = 1 , ? callable $ responseHandler = null )
7374 {
7475 $ this ->allow = $ allow ;
7576 $ this ->threshold = $ threshold ;
76- $ this ->responseHandler = isset ( $ responseHandler) ? $ responseHandler (...) : null ;
77+ $ this ->responseHandler = $ responseHandler ;
7778 }
7879
7980 /**
80- * Construct a limiter's allow and threshold
81+ * Construct a limiter's allowing and threshold
8182 */
8283 public static function allow (int $ requests , float $ threshold = 1 ): static
8384 {
@@ -89,7 +90,7 @@ public static function allow(int $requests, float $threshold = 1): static
8990 */
9091 public static function custom (callable $ responseHandler ): static
9192 {
92- return (new static (1 , 1 , $ responseHandler(...) ))->everySeconds (60 , 'custom ' );
93+ return (new static (1 , 1 , $ responseHandler ))->everySeconds (60 , 'custom ' );
9394 }
9495
9596 /**
@@ -123,7 +124,7 @@ public function hit(int $amount = 1): static
123124 /**
124125 * Set the limit as exceeded
125126 */
126- public function exceeded (int $ releaseInSeconds = null ): void
127+ public function exceeded (? int $ releaseInSeconds = null ): void
127128 {
128129 $ this ->exceeded = true ;
129130
@@ -281,7 +282,7 @@ public function handleResponse(Response $response): void
281282 *
282283 * @return $this
283284 * @throws \JsonException
284- * @throws \Saloon\RateLimitPlugin\Exceptions\ LimitException
285+ * @throws LimitException
285286 */
286287 public function update (RateLimitStore $ store ): static
287288 {
@@ -316,14 +317,14 @@ public function update(RateLimitStore $store): static
316317 return $ this ;
317318 }
318319
319- // If our expiry hasn't passed, yet then we'll set the expiry timestamp
320- // and, we'll also update the hits so the current instance has the
320+ // If our expiry hasn't passed yet, then we'll set the expiry timestamp,
321+ // and we'll also update the hits so the current instance has the
321322 // number of previous hits.
322323
323324 $ this ->setExpiryTimestamp ($ expiry );
324325 $ this ->hit ($ hits );
325326
326- // If this is a fromResponse limiter then we should apply the "allow" which will
327+ // If this is a fromResponse limiter, then we should apply the "allow" which will
327328 // be useful to check if we have reached our rate limit
328329
329330 if ($ this ->usesResponse ()) {
@@ -338,7 +339,7 @@ public function update(RateLimitStore $store): static
338339 *
339340 * @return $this
340341 * @throws \JsonException
341- * @throws \Saloon\RateLimitPlugin\Exceptions\ LimitException
342+ * @throws LimitException
342343 */
343344 public function save (RateLimitStore $ store , int $ resetHits = 1 ): static
344345 {
0 commit comments