Skip to content

Commit 701a710

Browse files
committed
Copy the logic into MockCache and order the new addition in changelog
1 parent 957586f commit 701a710

2 files changed

Lines changed: 18 additions & 4 deletions

File tree

system/Test/Mock/MockCache.php

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use CodeIgniter\Cache\Handlers\BaseHandler;
1919
use CodeIgniter\Cache\LockStoreInterface;
2020
use CodeIgniter\Cache\LockStoreProviderInterface;
21+
use CodeIgniter\Exceptions\InvalidArgumentException;
2122
use CodeIgniter\I18n\Time;
2223
use PHPUnit\Framework\Assert;
2324
use ReflectionFunction;
@@ -84,9 +85,22 @@ public function remember(string $key, callable|int $ttl, Closure $callback): mix
8485
$value = $callback();
8586

8687
if (is_callable($ttl)) {
87-
$ttl = (new ReflectionFunction($ttl(...)))->getNumberOfParameters() > 0
88-
? $ttl($value)
89-
: $ttl();
88+
$ttlClosure = Closure::fromCallable($ttl);
89+
$rf = new ReflectionFunction($ttlClosure);
90+
$params = $rf->getNumberOfRequiredParameters();
91+
92+
if ($params === 0) {
93+
/** @var Closure(): int $ttlClosure */
94+
$ttl = $ttlClosure();
95+
} elseif ($params === 1) {
96+
/** @var Closure(mixed): int $ttlClosure */
97+
$ttl = $ttlClosure($value);
98+
} else {
99+
throw new InvalidArgumentException(sprintf(
100+
'Argument #2 ($ttl) must accept 0 or 1 parameter, %d given.',
101+
$params,
102+
));
103+
}
90104
}
91105

92106
$this->save($key, $value, $ttl);

user_guide_src/source/changelogs/v4.8.0.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,10 @@ Interface Changes
4343
**NOTE:** If you've implemented your own classes that implement these interfaces from scratch, you will need to
4444
update your implementations to include the new methods or method changes to ensure compatibility.
4545

46+
- **Cache:** ``CodeIgniter\Cache\CacheInterface::remember()`` now accepts a TTL callable. All built-in cache handlers inherit this method via ``BaseHandler``, so no changes are required for them.
4647
- **Database:** ``CodeIgniter\Database\ConnectionInterface`` now requires the ``afterCommit()``, ``afterRollback()``, and ``transaction()`` methods.
4748
- **Logging:** ``CodeIgniter\Log\Handlers\HandlerInterface::handle()`` now requires a third parameter ``array $context = []``. Any custom log handler that overrides ``handle()`` - whether implementing ``HandlerInterface`` directly or extending a built-in handler class - must add the parameter to its ``handle()`` method signature.
4849
- **Security:** The ``SecurityInterface``'s ``verify()`` method now has a native return type of ``static``.
49-
- **Cache:** ``CodeIgniter\Cache\CacheInterface::remember()`` now accepts a TTL callable. All built-in cache handlers inherit this method via ``BaseHandler``, so no changes are required for them.
5050

5151
Method Signature Changes
5252
========================

0 commit comments

Comments
 (0)