diff --git a/.github/FUNDING b/.github/FUNDING index 0dcac02..7635e8b 100644 --- a/.github/FUNDING +++ b/.github/FUNDING @@ -1 +1 @@ -ko_fi: sammyjo20 +github: sammyjo20 diff --git a/.github/workflows/phpstan.yml b/.github/workflows/phpstan.yml index 8ad0db3..c1e626f 100644 --- a/.github/workflows/phpstan.yml +++ b/.github/workflows/phpstan.yml @@ -18,7 +18,7 @@ jobs: - name: Setup PHP uses: shivammathur/setup-php@v2 with: - php-version: '8.1' + php-version: '8.4' coverage: none - name: Install composer dependencies diff --git a/.github/workflows/redis-tests.yml b/.github/workflows/redis-tests.yml index d42b7e6..9e54899 100644 --- a/.github/workflows/redis-tests.yml +++ b/.github/workflows/redis-tests.yml @@ -18,7 +18,7 @@ jobs: fail-fast: true matrix: os: [ ubuntu-latest ] - php: [ 8.1, 8.2 ] + php: [ 8.1, 8.2, 8.3, 8.4 ] stability: [ prefer-lowest, prefer-stable ] services: # Label used to access the service container @@ -39,7 +39,7 @@ jobs: steps: - name: Checkout code - uses: actions/checkout@v3 + uses: actions/checkout@v4 - name: Setup PHP uses: shivammathur/setup-php@v2 diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 821e41d..c908e0e 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -18,14 +18,14 @@ jobs: fail-fast: true matrix: os: [ ubuntu-latest, windows-latest ] - php: [ 8.1, 8.2 ] + php: [ 8.1, 8.2, 8.3, 8.4 ] stability: [ prefer-lowest, prefer-stable ] name: P${{ matrix.php }} - ${{ matrix.stability }} - ${{ matrix.os }} steps: - name: Checkout code - uses: actions/checkout@v3 + uses: actions/checkout@v4 - name: Setup PHP uses: shivammathur/setup-php@v2 diff --git a/.gitignore b/.gitignore index 7d655ba..a11142a 100644 --- a/.gitignore +++ b/.gitignore @@ -14,6 +14,7 @@ # Caches, externally stored stuff, etc .php-cs-fixer.cache .phpunit.result.cache +.phpunit.cache tests/Fixtures/Saloon # environments/configs diff --git a/composer.json b/composer.json index 05181d6..b47efdb 100644 --- a/composer.json +++ b/composer.json @@ -26,6 +26,7 @@ "orchestra/testbench": "^8.5" }, "minimum-stability": "stable", + "prefer-stable": true, "autoload": { "psr-4": { "Saloon\\RateLimitPlugin\\": "src/", diff --git a/src/Limit.php b/src/Limit.php index dc5fbe0..41ccc54 100644 --- a/src/Limit.php +++ b/src/Limit.php @@ -4,7 +4,6 @@ namespace Saloon\RateLimitPlugin; -use Closure; use DateInterval; use DateTimeImmutable; use Saloon\Http\Response; @@ -40,7 +39,7 @@ class Limit /** * The threshold that should be used when determining if a limit has been reached * - * Must be between 0 and 1. For example if you want the limiter to kick in at 85% + * Must be between 0 and 1. For example, if you want the limiter to kick in at 85% * you must set the threshold to 0.85 */ protected float $threshold = 1; @@ -58,8 +57,10 @@ class Limit /** * Custom response handler + * + * @var null|callable(): mixed */ - protected ?Closure $responseHandler = null; + protected $responseHandler = null; /** * Determines if we should sleep or not @@ -69,15 +70,15 @@ class Limit /** * @param (callable(): mixed)|null $responseHandler */ - final public function __construct(int $allow, float $threshold = 1, callable $responseHandler = null) + final public function __construct(int $allow, float $threshold = 1, ?callable $responseHandler = null) { $this->allow = $allow; $this->threshold = $threshold; - $this->responseHandler = isset($responseHandler) ? $responseHandler(...) : null; + $this->responseHandler = $responseHandler; } /** - * Construct a limiter's allow and threshold + * Construct a limiter's allowing and threshold */ public static function allow(int $requests, float $threshold = 1): static { @@ -89,7 +90,7 @@ public static function allow(int $requests, float $threshold = 1): static */ public static function custom(callable $responseHandler): static { - return (new static(1, 1, $responseHandler(...)))->everySeconds(60, 'custom'); + return (new static(1, 1, $responseHandler))->everySeconds(60, 'custom'); } /** @@ -123,7 +124,7 @@ public function hit(int $amount = 1): static /** * Set the limit as exceeded */ - public function exceeded(int $releaseInSeconds = null): void + public function exceeded(?int $releaseInSeconds = null): void { $this->exceeded = true; @@ -281,7 +282,7 @@ public function handleResponse(Response $response): void * * @return $this * @throws \JsonException - * @throws \Saloon\RateLimitPlugin\Exceptions\LimitException + * @throws LimitException */ public function update(RateLimitStore $store): static { @@ -316,14 +317,14 @@ public function update(RateLimitStore $store): static return $this; } - // If our expiry hasn't passed, yet then we'll set the expiry timestamp - // and, we'll also update the hits so the current instance has the + // If our expiry hasn't passed yet, then we'll set the expiry timestamp, + // and we'll also update the hits so the current instance has the // number of previous hits. $this->setExpiryTimestamp($expiry); $this->hit($hits); - // If this is a fromResponse limiter then we should apply the "allow" which will + // If this is a fromResponse limiter, then we should apply the "allow" which will // be useful to check if we have reached our rate limit if ($this->usesResponse()) { @@ -338,7 +339,7 @@ public function update(RateLimitStore $store): static * * @return $this * @throws \JsonException - * @throws \Saloon\RateLimitPlugin\Exceptions\LimitException + * @throws LimitException */ public function save(RateLimitStore $store, int $resetHits = 1): static {