首页 > 解决方案 > php docBlock - 将类型放入@param,如果它导致异常?

问题描述

鉴于您有以下方法:

/**
* @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

标签: phpcoding-style

解决方案


我认为它是基于意见的,但我会设置@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.
 */

推荐阅读