skip to Main Content

I have a function that checks if a value is null and after I call that function I use the value that is obviously not null.

but phpstan still says it could be null.

Do you guys have any solution for phpstan to know the outcome of this function.

protected ?string $filterCacheKey = null;
protected function usesFilterCaching(): bool
{
    return $this->filterCacheKey !== null;
}

Parameter #1 $key of method IlluminateContractsSessionSession::get() expects string, string|null given.

if ($this->usesFilterCaching() && $this->request()->method() === Request::METHOD_GET) {
    $sessionCacheFilters = $this->filterStore()->get($this->filterCacheKey);

    return Arr::get($sessionCacheFilters, $filterName, $defaultChecked);
}

3

Answers


  1. Chosen as BEST ANSWER

    I think this is the cleanest solution:

    I added the following docblock to the usesFilterCaching() function.

    /**
     * @phpstan-assert-if-true string $this->filterCacheKey
     */
    protected function usesFilterCaching(): bool
    {
        return $this->filterCacheKey !== null;
    }
    

    This sets $this->filterCacheKey to a string if the function returns true.


  2. I think your problem is right here:

    $sessionCacheFilters = $this->filterStore()->get($this->filterCacheKey); 
    

    $this->filterStore() does not have a value at key $this->filterCacheKey

    So I think your check should be:

    if ($this->usesFilterCaching() && $this->filterStore()->get($this->filterCacheKey) != null && $this->request()->method() === Request::METHOD_GET)

    Login or Signup to reply.
  3. Although your code is clearly checking the null value before it is used, phpstan does not check called methods. It is a static analysis tool that will only stick to parameter types and return types, not what checks occur inside a called function.

    As a quick workaround for this problem, I would suggest removing the usesFilterCaching function altogether as it can be replaced with a simple if check like so:

    if ($this->filterCacheKey && $this->request()->method() === Request::METHOD_GET) {
        $sessionCacheFilters = $this->filterStore()->get($this->filterCacheKey);
    
        return Arr::get($sessionCacheFilters, $filterName, $defaultChecked);
    }
    

    This will perform the same null check and the code will not be executed if the variable is null.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search