php - 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?
@param mixed
@param string|array
解决方案
我认为它是基于意见的,但我会设置@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.
*/
推荐阅读
- mysql - Laravel lockForUpdate 和发票号码生成
- node.js - 我们可以在 azure mongodb 客户端中连接到 fitbit 服务器吗?
- javascript - Google Sheets Api 不使用 .json 文件,而是将其嵌入 python 代码中
- multithreading - 为什么在涉及线程时需要“move”关键字;为什么我永远不想要这种行为?
- flutter - 从 StreamBuilder 获取有效值
和一个孩子 StreamBuilder - google-sheets - 谷歌应用脚本将多个工作表动态合并到一个主文件中
- ckeditor5 - 样式化 CKEditors 的单个实例的方式?
- amazon-web-services - 一旦它变得健康,有没有办法让 LB 将流量路由到主机?
- c# - 如何使用物理改变子弹射击速度并保持子弹射击距离?
- dryioc - 使用工厂类解析 DryIoc serviceKey 实现