Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add generic hints to Cache #45325

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
12 changes: 7 additions & 5 deletions src/Illuminate/Support/Facades/Cache.php
Expand Up @@ -3,6 +3,8 @@
namespace Illuminate\Support\Facades;

/**
* @template TCacheValue
*
* @method static \Illuminate\Contracts\Cache\Repository store(string|null $name = null)
* @method static \Illuminate\Contracts\Cache\Repository driver(string|null $driver = null)
* @method static \Illuminate\Cache\Repository repository(\Illuminate\Contracts\Cache\Store $store)
Expand All @@ -14,10 +16,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 +28,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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You will need to add tests for the cases when a closure is given for the $default arguments.


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;
}));