phpstan未使用函数的结果。

huangapple go评论84阅读模式
英文:

phpstan not using outcome of 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);
}
英文:

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

答案1

得分: 1

我认为问题出在这里:

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

$this->filterStore() 在键 $this->filterCacheKey 上没有值。

所以我认为你的检查应该是:

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

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)

答案2

得分: 0

尽管您的代码明显在使用之前检查了空值,但phpstan不会检查调用的方法。它是一个静态分析工具,仅关注参数类型和返回类型,而不关注在调用的函数内发生了哪些检查。

作为解决这个问题的快速临时方法,我建议完全删除usesFilterCaching函数,因为它可以用一个简单的if检查来替代,如下所示:

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

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

这将执行相同的空值检查,如果变量为空,代码将不会执行。

英文:

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.

答案3

得分: 0

我认为这是最清晰的解决方案:

我向usesFilterCaching()函数添加了以下文档块。

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

如果该函数返回true,它会将$this->filterCacheKey设置为字符串。

英文:

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.

huangapple
  • 本文由 发表于 2023年1月9日 18:11:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/75055745.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定