laravel-idea / plugin

Laravel Idea plugin for PhpStorm
https://laravel-idea.com/
177 stars 7 forks source link

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

Open liamduckett opened 1 month ago

liamduckett commented 1 month ago

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