【问题标题】:PHPDoc for throwing exceptions in closures用于在闭包中抛出异常的 PHPDoc
【发布时间】: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());
        }
    }

【问题讨论】:

标签: php exception phpstorm phpdoc


【解决方案1】:

PHP Storm 似乎还没有正确支持该注释。 这个问题https://youtrack.jetbrains.com/issue/WI-52095 正是我的情况。 感谢您的帮助! 我已将我的案例添加到上述票证中。

【讨论】:

    猜你喜欢
    • 2023-04-02
    • 1970-01-01
    • 2015-11-06
    • 2012-01-24
    • 2021-05-26
    • 2011-02-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多