我在构造中有一个自定义异常,但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();
}
}
答案 0 :(得分:0)
我可以想到三种选择:
注释缺少的异常:
/**
* @param null|string $id
* @throws InvalidUUIDException if `$id` is not a valid UUID
* @throws \Exception if new UUID generation failed
*/
捕获丢失的异常:
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();
}
只需忽略检查即可。
我认为这几乎涵盖了所有;-)