【发布时间】:2020-09-26 19:33:02
【问题描述】:
是否有任何聪明的方法来记录闭包引发异常?我正在使用 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());
}
}
【问题讨论】:
-
以下注释的代码与您的总体相似(总体思路:闭包变量是一个类字段):youtrack.jetbrains.com/issue/…。我可能只建议检查该和相关的票证部分,并在必要时提交您自己的文件(这样您就可以直接回复您的案件)。也许是youtrack.jetbrains.com/issue/WI-52095?
标签: php exception phpstorm phpdoc