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

[Bug]: Cache::get 2nd parameter $default is restricted to null|Closure #1078

Open
liamduckett opened this issue Oct 3, 2024 · 0 comments
Open

Comments

@liamduckett
Copy link

Bug description

Hey, appreciate the work on the plugin.

The following code:

Cache::get('foo', 1);

Results in the following warning: "Expected parameter of type '\Closure|null', 'int' provided"

An integer is valid here, as per the definition:

/**
 * Retrieve an item from the cache by key.
 *
 * @template TCacheValue
 *
 * @param  array|string  $key
 * @param  TCacheValue|(\Closure(): TCacheValue)  $default
 * @return (TCacheValue is null ? mixed : TCacheValue)
 */
public function get($key, $default = null): mixed
{
    if (is_array($key)) {
        return $this->many($key);
    }

    $this->event(new RetrievingKey($this->getName(), $key));

    $value = $this->store->get($this->itemKey($key));

    // If we could not find the cache value, we will fire the missed event and get
    // the default value for this cache value. This default could be a callback
    // so we will execute the value function which will resolve it if needed.
    if (is_null($value)) {
        $this->event(new CacheMissed($this->getName(), $key));

        $value = value($default);
    } else {
        $this->event(new CacheHit($this->getName(), $key, $value));
    }

    return $value;
}

Noting specifically:

$value = value($default);

Which when passed an int, will just return the int

/**
 * Return the default value of the given value.
 *
 * @template TValue
 * @template TArgs
 *
 * @param  TValue|\Closure(TArgs): TValue  $value
 * @param  TArgs  ...$args
 * @return TValue
 */
function value($value, ...$args)
{
    return $value instanceof Closure ? $value(...$args) : $value;
}

Plugin version

8.3.3.242

Operating system

MacOS

Steps to reproduce

Make use of the Cache facade's get method, and set the default to an integer

Cache::get('foo', 1);

Relevant log output

No response

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant