From 17f924d3630c3123626363c5121c18e4f7adbba4 Mon Sep 17 00:00:00 2001 From: SQKo <87897282+SQKo@users.noreply.github.com> Date: Sun, 3 Dec 2023 20:05:44 +0700 Subject: [PATCH 1/4] Add helper for void cache --- src/Discord/Helpers/VoidCache.php | 66 +++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 src/Discord/Helpers/VoidCache.php diff --git a/src/Discord/Helpers/VoidCache.php b/src/Discord/Helpers/VoidCache.php new file mode 100644 index 000000000..282bb5b59 --- /dev/null +++ b/src/Discord/Helpers/VoidCache.php @@ -0,0 +1,66 @@ + + * + * This file is subject to the MIT license that is bundled + * with this source code in the LICENSE.md file. + */ + +namespace Discord\Helpers; + +use Psr\SimpleCache\CacheInterface; + +/** + * The cache that always be null/void + */ +class VoidCache implements CacheInterface +{ + public function get(string $key, mixed $default = null): mixed + { + return $default; + } + + public function set(string $key, mixed $value, null|int|\DateInterval $ttl = null): bool + { + return true; + } + + public function delete(string $key): bool + { + return true; + } + + public function getMultiple(iterable $keys, mixed $default = null): iterable + { + $result = []; + + foreach ($keys as $key) { + $result[$key] = $default; + } + + return $result; + } + + public function setMultiple(iterable $values, null|int|\DateInterval $ttl = null): bool + { + return true; + } + + public function deleteMultiple(iterable $keys): bool + { + return true; + } + + public function clear(): bool + { + return true; + } + + public function has(string $key): bool + { + return false; + } +} From 89d595dc43480b5ee0ce750f2feb46072374fc39 Mon Sep 17 00:00:00 2001 From: Valithor Obsidion Date: Mon, 13 Apr 2026 15:57:48 -0400 Subject: [PATCH 2/4] Version compatible methods Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- src/Discord/Helpers/VoidCache.php | 124 +++++++++++++++++++++--------- 1 file changed, 89 insertions(+), 35 deletions(-) diff --git a/src/Discord/Helpers/VoidCache.php b/src/Discord/Helpers/VoidCache.php index 282bb5b59..c5c96ac3d 100644 --- a/src/Discord/Helpers/VoidCache.php +++ b/src/Discord/Helpers/VoidCache.php @@ -14,53 +14,107 @@ use Psr\SimpleCache\CacheInterface; /** - * The cache that always be null/void + * The cache that always be null/void. + * + * This class is conditionally defined to remain compatible with + * psr/simple-cache v1, v2 and v3 method signatures. */ -class VoidCache implements CacheInterface -{ - public function get(string $key, mixed $default = null): mixed +if ((new \ReflectionMethod(CacheInterface::class, 'get'))->hasReturnType()) { + class VoidCache implements CacheInterface { - return $default; - } + public function get(string $key, mixed $default = null): mixed + { + return $default; + } - public function set(string $key, mixed $value, null|int|\DateInterval $ttl = null): bool - { - return true; - } + public function set(string $key, mixed $value, null|int|\DateInterval $ttl = null): bool + { + return true; + } - public function delete(string $key): bool - { - return true; - } + public function delete(string $key): bool + { + return true; + } - public function getMultiple(iterable $keys, mixed $default = null): iterable - { - $result = []; + public function getMultiple(iterable $keys, mixed $default = null): iterable + { + $result = []; - foreach ($keys as $key) { - $result[$key] = $default; + foreach ($keys as $key) { + $result[$key] = $default; + } + + return $result; } - return $result; - } + public function setMultiple(iterable $values, null|int|\DateInterval $ttl = null): bool + { + return true; + } - public function setMultiple(iterable $values, null|int|\DateInterval $ttl = null): bool - { - return true; - } + public function deleteMultiple(iterable $keys): bool + { + return true; + } - public function deleteMultiple(iterable $keys): bool - { - return true; - } + public function clear(): bool + { + return true; + } - public function clear(): bool - { - return true; + public function has(string $key): bool + { + return false; + } } - - public function has(string $key): bool +} else { + class VoidCache implements CacheInterface { - return false; + public function get($key, $default = null) + { + return $default; + } + + public function set($key, $value, $ttl = null) + { + return true; + } + + public function delete($key) + { + return true; + } + + public function getMultiple($keys, $default = null) + { + $result = []; + + foreach ($keys as $key) { + $result[$key] = $default; + } + + return $result; + } + + public function setMultiple($values, $ttl = null) + { + return true; + } + + public function deleteMultiple($keys) + { + return true; + } + + public function clear() + { + return true; + } + + public function has($key) + { + return false; + } } } From dd7e9fa0562889046099fb47209573d4755c16b5 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Mon, 13 Apr 2026 19:58:38 +0000 Subject: [PATCH 3/4] Update VoidCache docblock: fix description and add @since v10.48.0 Agent-Logs-Url: https://github.com/discord-php/DiscordPHP/sessions/c4bacf6c-7fa9-4447-801c-b9b09a5b534f Co-authored-by: valzargaming <7202504+valzargaming@users.noreply.github.com> --- src/Discord/Helpers/VoidCache.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Discord/Helpers/VoidCache.php b/src/Discord/Helpers/VoidCache.php index c5c96ac3d..6b2b0864e 100644 --- a/src/Discord/Helpers/VoidCache.php +++ b/src/Discord/Helpers/VoidCache.php @@ -14,10 +14,12 @@ use Psr\SimpleCache\CacheInterface; /** - * The cache that always be null/void. + * A no-op cache that never stores values and always returns the provided default. * * This class is conditionally defined to remain compatible with * psr/simple-cache v1, v2 and v3 method signatures. + * + * @since v10.48.0 */ if ((new \ReflectionMethod(CacheInterface::class, 'get'))->hasReturnType()) { class VoidCache implements CacheInterface From 2f1e16ef1762f4e8b8529907985bb7d37f560ac3 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Mon, 13 Apr 2026 20:06:40 +0000 Subject: [PATCH 4/4] Add unit tests for VoidCache Agent-Logs-Url: https://github.com/discord-php/DiscordPHP/sessions/4948aa9c-c6c7-41e6-9547-49c9ce130606 Co-authored-by: valzargaming <7202504+valzargaming@users.noreply.github.com> --- tests/Helpers/VoidCacheTest.php | 93 +++++++++++++++++++++++++++++++++ 1 file changed, 93 insertions(+) create mode 100644 tests/Helpers/VoidCacheTest.php diff --git a/tests/Helpers/VoidCacheTest.php b/tests/Helpers/VoidCacheTest.php new file mode 100644 index 000000000..d63604baf --- /dev/null +++ b/tests/Helpers/VoidCacheTest.php @@ -0,0 +1,93 @@ + + * + * This file is subject to the MIT license that is bundled + * with this source code in the LICENSE.md file. + */ + +use Discord\Helpers\VoidCache; +use PHPUnit\Framework\TestCase; + +final class VoidCacheTest extends TestCase +{ + private VoidCache $cache; + + protected function setUp(): void + { + $this->cache = new VoidCache(); + } + + public function testGetReturnsNullByDefault(): void + { + $this->assertNull($this->cache->get('key')); + } + + public function testGetReturnsProvidedDefault(): void + { + $this->assertSame('default', $this->cache->get('key', 'default')); + $this->assertSame(42, $this->cache->get('key', 42)); + } + + public function testSetReturnsTrue(): void + { + $this->assertTrue($this->cache->set('key', 'value')); + } + + public function testDeleteReturnsTrue(): void + { + $this->assertTrue($this->cache->delete('key')); + } + + public function testClearReturnsTrue(): void + { + $this->assertTrue($this->cache->clear()); + } + + public function testHasReturnsFalse(): void + { + $this->assertFalse($this->cache->has('key')); + } + + public function testGetMultipleReturnsDefaultForAllKeys(): void + { + $keys = ['a', 'b', 'c']; + $result = $this->cache->getMultiple($keys, 'default'); + + $this->assertSame([ + 'a' => 'default', + 'b' => 'default', + 'c' => 'default', + ], $result); + } + + public function testGetMultipleReturnsNullDefaultWhenNotSpecified(): void + { + $result = $this->cache->getMultiple(['x', 'y']); + + $this->assertSame(['x' => null, 'y' => null], $result); + } + + public function testSetMultipleReturnsTrue(): void + { + $this->assertTrue($this->cache->setMultiple(['a' => 1, 'b' => 2])); + } + + public function testDeleteMultipleReturnsTrue(): void + { + $this->assertTrue($this->cache->deleteMultiple(['a', 'b'])); + } + + public function testSetDoesNotPersistValue(): void + { + $this->cache->set('key', 'value'); + + $this->assertNull($this->cache->get('key')); + $this->assertFalse($this->cache->has('key')); + } +}