diff --git a/composer.lock b/composer.lock index 1769d6c2fb..0ec24c8c21 100644 --- a/composer.lock +++ b/composer.lock @@ -3040,16 +3040,16 @@ }, { "name": "phpstan/phpstan", - "version": "1.12.12", + "version": "1.12.13", "source": { "type": "git", "url": "https://github.com/phpstan/phpstan.git", - "reference": "b5ae1b88f471d3fd4ba1aa0046234b5ca3776dd0" + "reference": "9b469068840cfa031e1deaf2fa1886d00e20680f" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/phpstan/phpstan/zipball/b5ae1b88f471d3fd4ba1aa0046234b5ca3776dd0", - "reference": "b5ae1b88f471d3fd4ba1aa0046234b5ca3776dd0", + "url": "https://api.github.com/repos/phpstan/phpstan/zipball/9b469068840cfa031e1deaf2fa1886d00e20680f", + "reference": "9b469068840cfa031e1deaf2fa1886d00e20680f", "shasum": "" }, "require": { @@ -3094,7 +3094,7 @@ "type": "github" } ], - "time": "2024-11-28T22:13:23+00:00" + "time": "2024-12-17T17:00:20+00:00" }, { "name": "phpunit/php-code-coverage", diff --git a/src/Abuse/Adapters/TimeLimit/Redis.php b/src/Abuse/Adapters/TimeLimit/Redis.php index 783f5884ed..3657a570d5 100644 --- a/src/Abuse/Adapters/TimeLimit/Redis.php +++ b/src/Abuse/Adapters/TimeLimit/Redis.php @@ -2,7 +2,6 @@ namespace Utopia\Abuse\Adapters\TimeLimit; -use Redis as Client; use Utopia\Abuse\Adapters\TimeLimit; class Redis extends TimeLimit @@ -10,11 +9,11 @@ class Redis extends TimeLimit public const NAMESPACE = 'abuse'; /** - * @var Client + * @var \Redis */ - protected Client $redis; + protected \Redis $redis; - public function __construct(string $key, int $limit, int $seconds, Client $redis) + public function __construct(string $key, int $limit, int $seconds, \Redis $redis) { $this->redis = $redis; $this->key = $key; diff --git a/src/Abuse/Adapters/TimeLimit/RedisCluster.php b/src/Abuse/Adapters/TimeLimit/RedisCluster.php index 426dc22fd5..24145d2b4b 100644 --- a/src/Abuse/Adapters/TimeLimit/RedisCluster.php +++ b/src/Abuse/Adapters/TimeLimit/RedisCluster.php @@ -2,7 +2,6 @@ namespace Utopia\Abuse\Adapters\TimeLimit; -use RedisCluster as Client; use Utopia\Abuse\Adapters\TimeLimit; class RedisCluster extends TimeLimit @@ -10,11 +9,11 @@ class RedisCluster extends TimeLimit public const NAMESPACE = 'abuse'; /** - * @var Client + * @var \RedisCluster */ - protected Client $redis; + protected \RedisCluster $redis; - public function __construct(string $key, int $limit, int $seconds, Client $redis) + public function __construct(string $key, int $limit, int $seconds, \RedisCluster $redis) { $this->redis = $redis; $this->key = $key; @@ -23,8 +22,8 @@ class RedisCluster extends TimeLimit $this->limit = $limit; } - /** - * Undocumented function + /** + * Get count for a key at specific datetime * * @param string $key * @param string $datetime @@ -40,9 +39,9 @@ class RedisCluster extends TimeLimit return $this->count; } - /** @var string $count */ + /** @var string|false $count */ $count = $this->redis->get(self::NAMESPACE . '__'. $key .'__'. $datetime); - if (!$count) { + if ($count === false) { $this->count = 0; } else { $this->count = intval($count); @@ -52,10 +51,11 @@ class RedisCluster extends TimeLimit } /** - * @param string $key - * @param string $datetime - * @return void + * Record a hit for a key at specific datetime * + * @param string $key + * @param string $datetime + * @return void */ protected function hit(string $key, string $datetime): void { @@ -63,9 +63,9 @@ class RedisCluster extends TimeLimit return; } - /** @var string $count */ + /** @var string|false $count */ $count = $this->redis->get(self::NAMESPACE . '__'. $key .'__'. $datetime); - if (!$count) { + if ($count === false) { $this->count = 0; } else { $this->count = intval($count); @@ -80,16 +80,15 @@ class RedisCluster extends TimeLimit * * Return logs with an offset and limit * - * @param int $offset - * @param int|null $limit + * @param int|null $offset + * @param int|null $limit * @return array */ - public function getLogs(int $offset = 0, ?int $limit = 25): array + public function getLogs(?int $offset = 0, ?int $limit = 25): array { // TODO limit potential is SCAN but needs cursor no offset - $cursor = null; - $keys = $this->scan($cursor, self::NAMESPACE . '__*', $limit); - if (!$keys) { + $keys = $this->scan(self::NAMESPACE . '__*', $offset, $limit); + if ($keys === false) { return []; } @@ -103,14 +102,14 @@ class RedisCluster extends TimeLimit /** * Delete all logs older than $datetime * - * @param string $datetime + * @param string $datetime * @return bool */ public function cleanup(string $datetime): bool { - $iterator = null; - $keys = $this->scan($iterator, self::NAMESPACE . '__*__*', 1000); + $keys = $this->scan(self::NAMESPACE . '__*__*'); $keys = $this->filterKeys($keys ? $keys : [], (int) $datetime); + /** @phpstan-ignore-next-line */ $this->redis->del($keys); return true; } @@ -135,12 +134,21 @@ class RedisCluster extends TimeLimit return $filteredKeys; } - protected function scan(&$testing, string $pattern, int $count): array|bool + /** + * Scan keys across all masters in the Redis cluster + * + * @param string $pattern Pattern to match keys + * @param int|null $cursor Reference to the cursor for scanning + * @param int|null $count Number of keys to return per iteration + * @return array|false + */ + protected function scan(string $pattern, ?int &$cursor = null, ?int $count = 1000): array|false { $matches = []; foreach ($this->redis->_masters() as $master) { $cursor = null; do { + /** @phpstan-ignore-next-line */ $keys = $this->redis->scan($cursor, $master, $pattern, $count); if ($keys !== false) { $matches = array_merge($matches, $keys); diff --git a/tests/Abuse/Bench/Database/TimeLimitBench.php b/tests/Abuse/Bench/Database/TimeLimitBench.php index e2a110c6b7..0525c44d81 100644 --- a/tests/Abuse/Bench/Database/TimeLimitBench.php +++ b/tests/Abuse/Bench/Database/TimeLimitBench.php @@ -4,7 +4,7 @@ namespace Abuse\Bench\Database; use PDO; use Utopia\Abuse\Abuse; -use Utopia\Abuse\Adapters\Database\TimeLimit; +use Utopia\Abuse\Adapters\TimeLimit\Database as TimeLimit; use Utopia\Cache\Adapter\None as NoCache; use Utopia\Cache\Cache; use Utopia\Database\Adapter\MariaDB; diff --git a/tests/Abuse/Bench/Redis/TimeLimitBench.php b/tests/Abuse/Bench/Redis/TimeLimitBench.php index 0d8cdb771d..3fdccfb811 100644 --- a/tests/Abuse/Bench/Redis/TimeLimitBench.php +++ b/tests/Abuse/Bench/Redis/TimeLimitBench.php @@ -4,7 +4,7 @@ namespace Abuse\Bench\Redis; use Redis as Client; use Utopia\Abuse\Abuse; -use Utopia\Abuse\Adapters\Redis\TimeLimit; +use Utopia\Abuse\Adapters\TimeLimit\Redis as TimeLimit; use Utopia\Tests\Bench\Base; final class TimeLimitBench extends Base