Skip to content

Commit 957586f

Browse files
committed
Add explicit types and throw exception if number of required args > 1
1 parent d04f197 commit 957586f

2 files changed

Lines changed: 19 additions & 6 deletions

File tree

system/Cache/CacheInterface.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,9 @@ public function save(string $key, mixed $value, int $ttl = 60): bool;
4444
* Attempts to get an item from the cache, or executes the callback
4545
* and stores the result on cache miss.
4646
*
47-
* @param string $key Cache item name
48-
* @param callable(): int|callable(mixed $value): int|int $ttl Time To Live, in seconds
49-
* @param Closure(): mixed $callback Callback executed on cache miss
47+
* @param string $key Cache item name
48+
* @param callable(): int|callable(mixed): int|int $ttl Time To Live, in seconds
49+
* @param Closure(): mixed $callback Callback executed on cache miss
5050
*/
5151
public function remember(string $key, callable|int $ttl, Closure $callback): mixed;
5252

system/Cache/Handlers/BaseHandler.php

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,22 @@ public function remember(string $key, callable|int $ttl, Closure $callback): mix
7676
$value = $callback();
7777

7878
if (is_callable($ttl)) {
79-
$ttl = (new ReflectionFunction(Closure::fromCallable($ttl)))->getNumberOfParameters() > 0
80-
? $ttl($value)
81-
: $ttl();
79+
$ttlClosure = Closure::fromCallable($ttl);
80+
$rf = new ReflectionFunction($ttlClosure);
81+
$params = $rf->getNumberOfRequiredParameters();
82+
83+
if ($params === 0) {
84+
/** @var Closure(): int $ttlClosure */
85+
$ttl = $ttlClosure();
86+
} elseif ($params === 1) {
87+
/** @var Closure(mixed): int $ttlClosure */
88+
$ttl = $ttlClosure($value);
89+
} else {
90+
throw new InvalidArgumentException(sprintf(
91+
'Argument #2 ($ttl) must accept 0 or 1 parameter, %d given.',
92+
$params,
93+
));
94+
}
8295
}
8396

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

0 commit comments

Comments
 (0)