有没有什么聪明的方法来记录一个Closure抛出的异常?我正在使用PhpStorm,我想摆脱恼人的警告,即在给定的块中永远不会抛出异常。
这个方法 createFromState()
正在投掷和 AbstractEntityRepositoryException
异常,我想让IDE知道这个异常。
/**
* Closure used to create an object from repository.
* The default implementation is using the method {@link EntityRepositoryInterface::createFromState()}
*
* @var Closure|null
* @throws AbstractEntityRepositoryException
*/
private ?Closure $create = null;
public function __construct(ControllerInteface $controller, string $id, ?Closure $create = null)
{
parent::__construct($controller, $id);
if ($create === null) {
$this->create = static function (EntityRepositoryInterface $repository, FormModelInterface $model): Entity {
return $repository->createFromState($model->toState());
};
}
else {
$this->create = $create;
}
}
/**
* @param FormModelInterface $model
* @return Entity
* @throws CreateActionException
*/
public function saveModel(FormModelInterface $model): Entity
{
try {
$create = $this->create;
return $create($this->repository, $model);
} catch (AbstractEntityRepositoryException $e) {
throw new CreateActionException($e->getMessage(), $e->getErrors());
}
}
似乎PHP Storm还没有正确地支持这个注释。https:/youtrack.jetbrains.comissueWI-52095。 正是我的情况,谢谢帮助!我已经把我的情况添加到上述的票据中。