PhpStorm无法在注释@throws标记中识别我的自定义异常

时间:2018-08-20 15:35:49

标签: phpstorm

我在构造中有一个自定义异常,但PhpStorm 2018.2无法识别类并说:

  

PHPDoc注释不包含所有必需的@throws标记

use App\Domain\Common\Exception\InvalidUUIDException;
....
/**
 * @param null|string $id
 * @throws InvalidUUIDException
 */
public function __construct(string $id = null)
{
    try {

        $this->uuid = Uuid::fromString($id ?: Uuid::uuid4())->toString();

    } catch (\InvalidArgumentException $e) {

        throw new InvalidUUIDException();
    }
}

1 个答案:

答案 0 :(得分:0)

我可以想到三种选择:

  1. 注释缺少的异常:

    /**
     * @param null|string $id
     * @throws InvalidUUIDException if `$id` is not a valid UUID
     * @throws \Exception if new UUID generation failed
     */
    
  2. 捕获丢失的异常:

    try {
        $this->uuid = Uuid::fromString($id ?: Uuid::uuid4())->toString();
    } catch (\InvalidArgumentException $e) {
        throw new InvalidUUIDException();
    } catch (\Exception $e) {
        // Do something interesting here
    }
    

    或(此处不适当,因为这两种异常都有不同的原因):

    try {
        $this->uuid = Uuid::fromString($id ?: Uuid::uuid4())->toString();
    } catch (\InvalidArgumentException | \Exception $e) {
        throw new InvalidUUIDException();
    }
    
  3. 只需忽略检查即可。

我认为这几乎涵盖了所有;-)