Skip to content

Commit

Permalink
Add generic hints to Cache
Browse files Browse the repository at this point in the history
  • Loading branch information
AJenbo committed Dec 21, 2022
1 parent f2c51fd commit 067e02b
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 23 deletions.
30 changes: 20 additions & 10 deletions src/Illuminate/Cache/Repository.php
Expand Up @@ -84,9 +84,11 @@ public function missing($key)
/**
* Retrieve an item from the cache by key.
*
* @template TCacheValue
*
* @param array|string $key
* @param mixed $default
* @return mixed
* @param TCacheValue|(\Closure(): TCacheValue) $default
* @return TCacheValue
*/
public function get($key, $default = null): mixed
{
Expand Down Expand Up @@ -175,9 +177,11 @@ protected function handleManyResult($keys, $key, $value)
/**
* Retrieve an item from the cache and delete it.
*
* @template TCacheValue
*
* @param string $key
* @param mixed $default
* @return mixed
* @param TCacheValue|(\Closure(): TCacheValue) $default
* @return TCacheValue
*/
public function pull($key, $default = null)
{
Expand Down Expand Up @@ -372,10 +376,12 @@ public function forever($key, $value)
/**
* Get an item from the cache, or execute the given Closure and store the result.
*
* @template TCacheValue
*
* @param string $key
* @param \Closure|\DateTimeInterface|\DateInterval|int|null $ttl
* @param \Closure $callback
* @return mixed
* @param \Closure(): TCacheValue $callback
* @return TCacheValue
*/
public function remember($key, $ttl, Closure $callback)
{
Expand All @@ -396,9 +402,11 @@ public function remember($key, $ttl, Closure $callback)
/**
* Get an item from the cache, or execute the given Closure and store the result forever.
*
* @template TCacheValue
*
* @param string $key
* @param \Closure $callback
* @return mixed
* @param \Closure(): TCacheValue $callback
* @return TCacheValue
*/
public function sear($key, Closure $callback)
{
Expand All @@ -408,9 +416,11 @@ public function sear($key, Closure $callback)
/**
* Get an item from the cache, or execute the given Closure and store the result forever.
*
* @template TCacheValue
*
* @param string $key
* @param \Closure $callback
* @return mixed
* @param \Closure(): TCacheValue $callback
* @return TCacheValue
*/
public function rememberForever($key, Closure $callback)
{
Expand Down
24 changes: 16 additions & 8 deletions src/Illuminate/Contracts/Cache/Repository.php
Expand Up @@ -10,9 +10,11 @@ interface Repository extends CacheInterface
/**
* Retrieve an item from the cache and delete it.
*
* @template TCacheValue
*
* @param string $key
* @param mixed $default
* @return mixed
* @param TCacheValue|(\Closure(): TCacheValue) $default
* @return TCacheValue
*/
public function pull($key, $default = null);

Expand Down Expand Up @@ -66,28 +68,34 @@ public function forever($key, $value);
/**
* Get an item from the cache, or execute the given Closure and store the result.
*
* @template TCacheValue
*
* @param string $key
* @param \DateTimeInterface|\DateInterval|int|null $ttl
* @param \Closure $callback
* @return mixed
* @param \Closure(): TCacheValue $callback
* @return TCacheValue
*/
public function remember($key, $ttl, Closure $callback);

/**
* Get an item from the cache, or execute the given Closure and store the result forever.
*
* @template TCacheValue
*
* @param string $key
* @param \Closure $callback
* @return mixed
* @param \Closure(): TCacheValue $callback
* @return TCacheValue
*/
public function sear($key, Closure $callback);

/**
* Get an item from the cache, or execute the given Closure and store the result forever.
*
* @template TCacheValue
*
* @param string $key
* @param \Closure $callback
* @return mixed
* @param \Closure(): TCacheValue $callback
* @return TCacheValue
*/
public function rememberForever($key, Closure $callback);

Expand Down
10 changes: 5 additions & 5 deletions src/Illuminate/Support/Facades/Cache.php
Expand Up @@ -14,10 +14,10 @@
* @method static \Illuminate\Cache\CacheManager extend(string $driver, \Closure $callback)
* @method static bool has(string $key)
* @method static bool missing(string $key)
* @method static mixed get(array|string $key, mixed $default = null)
* @method static TCacheValue get(array|string $key, TCacheValue|(\Closure(): TCacheValue) $default = null)
* @method static array many(array $keys)
* @method static iterable getMultiple(iterable<string> $keys, mixed $default = null)
* @method static mixed pull(string $key, mixed $default = null)
* @method static TCacheValue pull(string $key, TCacheValue|(\Closure(): TCacheValue) $default = null)
* @method static bool put(array|string $key, mixed $value, \DateTimeInterface|\DateInterval|int|null $ttl = null)
* @method static bool set(string $key, mixed $value, null|int|\DateInterval $ttl = null)
* @method static bool putMany(array $values, \DateTimeInterface|\DateInterval|int|null $ttl = null)
Expand All @@ -26,9 +26,9 @@
* @method static int|bool increment(string $key, mixed $value = 1)
* @method static int|bool decrement(string $key, mixed $value = 1)
* @method static bool forever(string $key, mixed $value)
* @method static mixed remember(string $key, \Closure|\DateTimeInterface|\DateInterval|int|null $ttl, \Closure $callback)
* @method static mixed sear(string $key, \Closure $callback)
* @method static mixed rememberForever(string $key, \Closure $callback)
* @method static TCacheValue remember(string $key, \Closure|\DateTimeInterface|\DateInterval|int|null $ttl, \Closure(): TCacheValue $callback)
* @method static TCacheValue sear(string $key, \Closure(): TCacheValue $callback)
* @method static TCacheValue rememberForever(string $key, \Closure(): TCacheValue $callback)
* @method static bool forget(string $key)
* @method static bool delete(string $key)
* @method static bool deleteMultiple(iterable<string> $keys)
Expand Down
37 changes: 37 additions & 0 deletions types/Support/Cache.php
@@ -0,0 +1,37 @@
<?php

use Illuminate\Cache\Repository;
use Illuminate\Contracts\Cache\Repository as RepositoryInterface;
use function PHPStan\Testing\assertType;

/** @var RepositoryInterface $cache */
$cache = resolve(RepositoryInterface::class);

assertType('int', $cache->pull('cache', 13));
assertType('int', $cache->pull('cache', function (): int {
return 12;
}));
assertType('int', $cache->sear('cache', function (): int {
return 15;
}));
assertType('int', $cache->rememberForever('cache', function (): int {
return 18;
}));

/** @var Repository $cache */
$cache = resolve(Repository::class);

assertType('int', $cache->get('cache', 27));
assertType('int', $cache->get('cache', function (): int {
return 26;
}));
assertType('int', $cache->pull('cache', 28));
assertType('int', $cache->pull('cache', function (): int {
return 30;
}));
assertType('int', $cache->sear('cache', function (): int {
return 33;
}));
assertType('int', $cache->rememberForever('cache', function (): int {
return 36;
}));

0 comments on commit 067e02b

Please sign in to comment.