【问题标题】:php docBlock - Put type into @param, if it leads to exception?php docBlock - 将类型放入@param,如果它导致异常?
【发布时间】:2019-10-02 10:08:00
【问题描述】:

假设你有以下方法:

/**
* @throws \Exception
**/
function foo($param): void
{
    if (!(is_string($param) | is_array($param))) {
        throw new \Exception('Param is neither string nor array!');
    }

    postProcess($param);
}

该方法本身接受所有参数类型,但如果参数既不是字符串也不是数组,则抛出异常。 对于此方法,您更喜欢以下哪个@param-Tag?

  1. @param mixed
  2. @param string|array

【问题讨论】:

  • 通常混合会使用。

标签: php coding-style


【解决方案1】:

我认为它是基于意见的,但我会设置@param string|array 以便给出更清晰的解释,支持哪些类型。您也可以添加评论(@throws@params 或两者),例如:

/**
 * @throws \Exception If the provided argument is not array or string
 *
 * @param string|array $param Bla-bla-bla. If not array or string - \Exception will be thrown.
 */

【讨论】:

    猜你喜欢
    • 2012-07-29
    • 1970-01-01
    • 1970-01-01
    • 2023-03-13
    • 2018-09-20
    • 1970-01-01
    • 2018-09-22
    • 1970-01-01
    相关资源
    最近更新 更多