Skip to content

Commit

Permalink
Merge pull request #9067 from paulbalandan/phpstan-1.11.8
Browse files Browse the repository at this point in the history
refactor: fix `Throttler::check()` $tokens
  • Loading branch information
paulbalandan committed Jul 25, 2024
2 parents 9597237 + eeb71f1 commit da778b5
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions system/Throttle/Throttler.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,11 @@ public function check(string $key, int $capacity, int $seconds, int $cost = 1):
// Number of seconds to get one token
$refresh = 1 / $rate;

/** @var float|int|null $tokens */
$tokens = $this->cache->get($tokenName);

// Check to see if the bucket has even been created yet.
if (($tokens = $this->cache->get($tokenName)) === null) {
if ($tokens === null) {
// If it hasn't been created, then we'll set it to the maximum
// capacity - 1, and save it to the cache.
$tokens = $capacity - $cost;
Expand All @@ -124,7 +127,7 @@ public function check(string $key, int $capacity, int $seconds, int $cost = 1):
// should be refilled, then checked against capacity
// to be sure the bucket didn't overflow.
$tokens += $rate * $elapsed;
$tokens = $tokens > $capacity ? $capacity : $tokens;
$tokens = min($tokens, $capacity);

// If $tokens >= 1, then we are safe to perform the action, but
// we need to decrement the number of available tokens.
Expand Down

0 comments on commit da778b5

Please sign in to comment.