考虑以下代码:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Car extends Model
{
public static function getTheFirstCar(string $color): ?self
{
/** @var ?self */ // <-- Doesn't apply! Is there any alternative?
return (new self())->newQuery()->firstWhere('color', '=', $color);
}
}
代码工作正常;尽管如此,PhpStorm 抱怨道:
返回值预计为'
Car|null
',返回
' '\Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model
将表达式的结果分配给带注释的变量可以解决警告,但会引入“冗余”变量!
/** @var ?self $redundant */
$redundant = (new self())->newQuery()->firstWhere('color', '=', $color);
return $redundant;
那么,PhpStorm 中是否有一种方法可以为return
语句表达式的值显式地强制执行内联类型注释Car|null
,而不引入冗余变量或指定所有预期的返回类型?