当错误推导返回类型时,如何防止PhpStorm中出现虚假警告? [英] How to prevent a false-warning in PhpStorm when the return-type is deduced incorrectly?
问题描述
考虑以下代码:
<?php命名空间App \ Models;使用Illuminate \ Database \ Eloquent \ Model;使用Illuminate \ Database \ Eloquent \ Builder;/***类MyModel* @程序包App \ Models* @mixin生成器*/类MyModel扩展Model{公共静态函数getGreens():生成器{return(new self())-> where('color','=','green');}}
在 return
语句中,PhpStorm(2020.3)抱怨:
返回值应为
'\ Illuminate \ Database \ Eloquent \ Builder'
,返回的'MyModel'
并建议:
将返回类型从
'\ Illuminate \ Database \ Eloquent \ Builder'
更改为'MyModel'
这是很奇怪的错误(
是由于未遵循最佳做法". MyModel
的类层次结构没有提供 where
的方法.换句话说,这种方法在类层次结构中不存在.但! Model
确实提供了魔术方法 __ call()
,该异常将在对象上下文中调用无法访问的方法(在您的情况下为 where
的方法)时触发.它实质上是转发调用" 到 \ Illuminate \ Database \ Eloquent \ Builder
的新实例,该实例具有所请求方法的实现(它是通过调用速度慢.
因此;删除 @mixin
标记,而不是使用魔术方法",而应使用本地访问":
<?php命名空间App \ Models;使用Illuminate \ Database \ Eloquent \ Model;使用Illuminate \ Database \ Eloquent \ Builder;类MyModel扩展Model{公共静态函数getGreens():生成器{return(new self())-> newQuery()-> where('color','=','green');//^^^^^^^^^^^^^}}
Consider the following code:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Builder;
/**
* Class MyModel
* @package App\Models
* @mixin Builder
*/
class MyModel extends Model
{
public static function getGreens(): Builder
{
return (new self())->where('color', '=', 'green');
}
}
On the return
statement, the PhpStorm (2020.3) complains that:
Return value is expected to be
'\Illuminate\Database\Eloquent\Builder'
,'MyModel'
returned
And suggest to:
Change return type from
'\Illuminate\Database\Eloquent\Builder'
to'MyModel'
which is weirdly incorrect (the where
method does return an instance of \Illuminate\Database\Eloquent\Builder
, while the IDE deduces the return type as being of MyModel
type). By removing the return type, the IDE issues another warning:
Missing function's return type declaration
The code works without any problems, but the IDE shouldn't report any false warnings! How should I avoid these warnings in PhpStorm?
It's a result of not following the "best practices". The class-hierarchy of MyModel
does not provide a method of where
; in other words, such a method does not exist in the class-hierarchy. But! The parent class of Model
does provide a magic method of __call()
which gets triggered when an inaccessible method in the object context is invoked (in your case, the method of where
). It essentially forwards the "call" to a new instance of \Illuminate\Database\Eloquent\Builder
, that has the implementation of the requested method (it's acquired from invoking the method of newQuery()
). This mechanism is not only IDE-unfriendly, but also slower.
Thus; drop the @mixin
tag, and instead of utilizing the "magic methods", use "native access":
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Builder;
class MyModel extends Model
{
public static function getGreens(): Builder
{
return (new self())->newQuery()->where('color', '=', 'green');
// ^^^^^^^^^^^^
}
}
这篇关于当错误推导返回类型时,如何防止PhpStorm中出现虚假警告?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!