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 Illuminate\Contracts\Session\Session::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);
}
CodePudding user response:
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)
CodePudding user response:
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.
CodePudding user response:
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.