当switch默认为抛出异常时,PhpStorm显示Missing return语句

时间:2017-09-22 11:38:32

标签: php phpstorm

PhpStorm使用“Missing return statement”警告标记此方法。

我想知道如何摆脱这种情况,因为在创建提交时会发出警告等等。但是不要关闭检查。

Missing return statement warning

我知道我可以在方法结束时抛出异常,并且PhpStorm会满意。

public static function getSomething(string $var)
{
    switch ($var)
    {
        case 1: return something();
        case 2: return somethingElse();
        default: throw new NowSomethingCompletelyDifferentException();
    }
}

我遗漏的代码有什么问题吗?

或者我应该向JetBrains解决这个问题?

2 个答案:

答案 0 :(得分:0)

查看PHPStorm文档Disabling and Enabling InspectionsSuppressing Inspections

禁用和启用检查

在编辑器中禁用对突出显示的问题的检查

以这种方式禁用检查时,将禁用当前检查配置文件。要重新启用以这种方式禁用的检查,请使用上述主要步骤。

将插入符号设置为突出显示的问题。

  1. 单击灯泡图标或按Alt + Enter显示检查 警报和建议清单。
  2. 选择要禁用的检查,然后单击向右箭头按钮 或者只需按右箭头键。
  3. 在子菜单上,单击“禁用”。

答案 1 :(得分:0)

如果它真的困扰你,你可以重构

public static function getSomething(string $var)
{
  $foo = [1 => 'something', 2 => 'somethingElse'];

  $method = $foo[$var];
  if $foo == NULL {
    throw new NowSomethingCompletelyDifferentException();
  }

  call_user_func($method);
}

但我不建议更改代码以满足您的IDE。只有当你认为这段代码更好时(可能是)。

或者您可以在jetbrains中提交错误并等待修复。