From eecbe49955dd9b43fd4e45c6e8f45175fa532394 Mon Sep 17 00:00:00 2001 From: crazywhalecc Date: Sat, 2 Apr 2022 23:37:22 +0800 Subject: [PATCH] fix code to phpstan-level-2 --- phpstan.neon | 2 +- src/Module/Example/Hello.php | 8 +- src/ZM/API/OneBotV11.php | 304 +++++++++++--------- src/ZM/API/TuringAPI.php | 16 +- src/ZM/Annotation/AnnotationParser.php | 14 +- src/ZM/Annotation/CQ/CQBefore.php | 2 +- src/ZM/Annotation/CQ/CQMetaEvent.php | 2 +- src/ZM/Annotation/Swoole/OnTask.php | 2 +- src/ZM/Context/Context.php | 56 ++-- src/ZM/DB/DB.php | 56 ++-- src/ZM/DB/InsertBody.php | 3 +- src/ZM/Event/SwooleEvent/OnManagerStart.php | 3 +- src/ZM/Framework.php | 6 +- src/ZM/Http/Response.php | 62 ++-- src/ZM/Module/ModulePacker.php | 4 +- src/ZM/Module/QQBot.php | 56 ++-- src/ZM/MySQL/MySQLStatement.php | 12 +- src/ZM/MySQL/MySQLWrapper.php | 33 ++- src/ZM/Store/LightCache.php | 8 +- src/ZM/Store/LightCacheInside.php | 10 +- src/ZM/Store/ZMAtomic.php | 5 +- src/ZM/Utils/DataProvider.php | 30 +- src/ZM/Utils/HttpUtil.php | 6 +- src/ZM/Utils/Manager/ModuleManager.php | 5 +- src/ZM/Utils/Manager/TaskManager.php | 9 +- src/ZM/Utils/Manager/WorkerManager.php | 14 +- src/ZM/Utils/MessageUtil.php | 35 ++- src/ZM/Utils/SignalListener.php | 2 +- src/ZM/Utils/Terminal.php | 36 ++- src/ZM/Utils/ZMUtil.php | 11 +- src/ZM/global_functions.php | 22 +- 31 files changed, 450 insertions(+), 384 deletions(-) diff --git a/phpstan.neon b/phpstan.neon index d049a7bc..65c6048c 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -1,6 +1,6 @@ parameters: reportUnmatchedIgnoredErrors: false - level: 1 + level: 2 paths: - ./src/ ignoreErrors: diff --git a/src/Module/Example/Hello.php b/src/Module/Example/Hello.php index 3a38f8ab..5db9d750 100644 --- a/src/Module/Example/Hello.php +++ b/src/Module/Example/Hello.php @@ -170,10 +170,10 @@ class Hello /** * 使用自定义参数的路由参数 * @RequestMapping("/whoami/{name}") - * @param $param - * @return string + * @param array $param 参数 + * @return string 返回的 HTML Body */ - public function paramGet($param) + public function paramGet(array $param = []): string { return 'Hello, ' . $param['name']; } @@ -181,7 +181,7 @@ class Hello /** * 在机器人连接后向终端输出信息 * @OnOpenEvent("qq") - * @param $conn + * @param ConnectionObject $conn WebSocket 连接对象 */ public function onConnect(ConnectionObject $conn) { diff --git a/src/ZM/API/OneBotV11.php b/src/ZM/API/OneBotV11.php index 8ab33f63..4d16385f 100644 --- a/src/ZM/API/OneBotV11.php +++ b/src/ZM/API/OneBotV11.php @@ -6,6 +6,7 @@ declare(strict_types=1); namespace ZM\API; +use Closure; use ZM\ConnectionManager\ConnectionObject; use ZM\ConnectionManager\ManagerGM; use ZM\Exception\RobotNotFoundException; @@ -39,11 +40,12 @@ class OneBotV11 } /** - * @param $robot_id + * 获取机器人Action/API实例 + * @param int|string $robot_id 机器人ID * @throws RobotNotFoundException - * @return ZMRobot + * @return ZMRobot 机器人实例 */ - public static function get($robot_id) + public static function get($robot_id): ZMRobot { $r = ManagerGM::getAllByName('qq'); foreach ($r as $v) { @@ -55,10 +57,11 @@ class OneBotV11 } /** + * 随机获取一个连接到框架的机器人实例 * @throws RobotNotFoundException - * @return ZMRobot + * @return ZMRobot 机器人实例 */ - public static function getRandom() + public static function getRandom(): ZMRobot { $r = ManagerGM::getAllByName('qq'); if ($r == []) { @@ -68,9 +71,10 @@ class OneBotV11 } /** - * @return ZMRobot[] + * 获取所有机器人实例 + * @return ZMRobot[] 机器人实例们 */ - public static function getAllRobot() + public static function getAllRobot(): array { $r = ManagerGM::getAllByName('qq'); $obj = []; @@ -80,13 +84,23 @@ class OneBotV11 return $obj; } - public function setCallback($callback = true) + /** + * 设置回调或启用协程等待API回包 + * @param bool|Closure $callback 是否开启协程或设置异步回调函数,如果为true,则协程等待结果,如果为false,则异步执行并不等待结果,如果为回调函数,则异步执行且调用回调 + * @return OneBotV11 返回本身 + */ + public function setCallback($callback = true): OneBotV11 { $this->callback = $callback; return $this; } - public function setPrefix($prefix = self::API_NORMAL) + /** + * 设置API调用类型后缀 + * @param int $prefix 设置后缀类型,API_NORMAL为不加后缀,API_ASYNC为异步调用,API_RATE_LIMITED为加后缀并且限制调用频率 + * @return OneBotV11 返回本身 + */ + public function setPrefix(int $prefix = self::API_NORMAL): OneBotV11 { $this->prefix = $prefix; return $this; @@ -101,12 +115,13 @@ class OneBotV11 /** * 发送私聊消息 - * @see https://github.com/howmanybots/onebot/blob/master/v11/specs/api/public.md#send_private_msg-%E5%8F%91%E9%80%81%E7%A7%81%E8%81%8A%E6%B6%88%E6%81%AF - * @param $user_id - * @param $message - * @return null|array|bool + * @see https://github.com/botuniverse/onebot-11/blob/master/api/public.md#send_private_msg-%E5%8F%91%E9%80%81%E7%A7%81%E8%81%8A%E6%B6%88%E6%81%AF + * @param int|string $user_id 用户ID + * @param string $message 消息内容 + * @param bool $auto_escape 是否自动转义(默认为false) + * @return array|bool 返回API调用结果(数组)或异步API调用状态(bool) */ - public function sendPrivateMsg($user_id, $message, bool $auto_escape = false) + public function sendPrivateMsg($user_id, string $message, bool $auto_escape = false) { return $this->processAPI($this->connection, [ 'action' => $this->getActionName($this->prefix, __FUNCTION__), @@ -120,12 +135,13 @@ class OneBotV11 /** * 发送群消息 - * @see https://github.com/howmanybots/onebot/blob/master/v11/specs/api/public.md#send_group_msg-%E5%8F%91%E9%80%81%E7%BE%A4%E6%B6%88%E6%81%AF - * @param $group_id - * @param $message - * @return null|array|bool + * @see https://github.com/botuniverse/onebot-11/blob/master/api/public.md#send_group_msg-%E5%8F%91%E9%80%81%E7%BE%A4%E6%B6%88%E6%81%AF + * @param int|string $group_id 群组ID + * @param string $message 消息内容 + * @param bool $auto_escape 是否自动转义(默认为false) + * @return array|bool 返回API调用结果(数组)或异步API调用状态(bool) */ - public function sendGroupMsg($group_id, $message, bool $auto_escape = false) + public function sendGroupMsg($group_id, string $message, bool $auto_escape = false) { return $this->processAPI($this->connection, [ 'action' => $this->getActionName($this->prefix, __FUNCTION__), @@ -139,13 +155,14 @@ class OneBotV11 /** * 发送消息 - * @see https://github.com/howmanybots/onebot/blob/master/v11/specs/api/public.md#send_msg-%E5%8F%91%E9%80%81%E6%B6%88%E6%81%AF - * @param $message_type - * @param $target_id - * @param $message - * @return null|array|bool + * @see https://github.com/botuniverse/onebot-11/blob/master/api/public.md#send_msg-%E5%8F%91%E9%80%81%E6%B6%88%E6%81%AF + * @param string $message_type 消息类型 + * @param int|string $target_id 目标ID + * @param string $message 消息内容 + * @param bool $auto_escape 是否自动转义(默认为false) + * @return array|bool 返回API调用结果(数组)或异步API调用状态(bool) */ - public function sendMsg($message_type, $target_id, $message, bool $auto_escape = false) + public function sendMsg(string $message_type, $target_id, string $message, bool $auto_escape = false) { return $this->processAPI($this->connection, [ 'action' => $this->getActionName($this->prefix, __FUNCTION__), @@ -160,9 +177,9 @@ class OneBotV11 /** * 撤回消息 - * @see https://github.com/howmanybots/onebot/blob/master/v11/specs/api/public.md#delete_msg-%E6%92%A4%E5%9B%9E%E6%B6%88%E6%81%AF - * @param $message_id - * @return null|array|bool + * @see https://github.com/botuniverse/onebot-11/blob/master/api/public.md#delete_msg-%E6%92%A4%E5%9B%9E%E6%B6%88%E6%81%AF + * @param int|string $message_id 消息ID + * @return array|bool 返回API调用结果(数组)或异步API调用状态(bool) */ public function deleteMsg($message_id) { @@ -176,9 +193,9 @@ class OneBotV11 /** * 获取消息 - * @see https://github.com/howmanybots/onebot/blob/master/v11/specs/api/public.md#get_msg-%E8%8E%B7%E5%8F%96%E6%B6%88%E6%81%AF - * @param $message_id - * @return null|array|bool + * @see https://github.com/botuniverse/onebot-11/blob/master/api/public.md#get_msg-%E8%8E%B7%E5%8F%96%E6%B6%88%E6%81%AF + * @param int|string $message_id 消息ID + * @return array|bool 返回API调用结果(数组)或异步API调用状态(bool) */ public function getMsg($message_id) { @@ -192,9 +209,9 @@ class OneBotV11 /** * 获取合并转发消息 - * @see https://github.com/howmanybots/onebot/blob/master/v11/specs/api/public.md#get_forward_msg-%E8%8E%B7%E5%8F%96%E5%90%88%E5%B9%B6%E8%BD%AC%E5%8F%91%E6%B6%88%E6%81%AF - * @param $id - * @return null|array|bool + * @see https://github.com/botuniverse/onebot-11/blob/master/api/public.md#get_forward_msg-%E8%8E%B7%E5%8F%96%E5%90%88%E5%B9%B6%E8%BD%AC%E5%8F%91%E6%B6%88%E6%81%AF + * @param int|string $id ID + * @return array|bool 返回API调用结果(数组)或异步API调用状态(bool) */ public function getForwardMsg($id) { @@ -208,9 +225,10 @@ class OneBotV11 /** * 发送好友赞 - * @see https://github.com/howmanybots/onebot/blob/master/v11/specs/api/public.md#send_like-%E5%8F%91%E9%80%81%E5%A5%BD%E5%8F%8B%E8%B5%9E - * @param $user_id - * @return null|array|bool + * @see https://github.com/botuniverse/onebot-11/blob/master/api/public.md#send_like-%E5%8F%91%E9%80%81%E5%A5%BD%E5%8F%8B%E8%B5%9E + * @param int|string $user_id 用户ID + * @param int $times 时间 + * @return array|bool 返回API调用结果(数组)或异步API调用状态(bool) */ public function sendLike($user_id, int $times = 1) { @@ -225,10 +243,10 @@ class OneBotV11 /** * 群组踢人 - * @see https://github.com/howmanybots/onebot/blob/master/v11/specs/api/public.md#set_group_kick-%E7%BE%A4%E7%BB%84%E8%B8%A2%E4%BA%BA - * @param $group_id - * @param $user_id - * @return null|array|bool + * @see https://github.com/botuniverse/onebot-11/blob/master/api/public.md#set_group_kick-%E7%BE%A4%E7%BB%84%E8%B8%A2%E4%BA%BA + * @param int|string $group_id 群ID + * @param int|string $user_id 用户ID + * @return array|bool 返回API调用结果(数组)或异步API调用状态(bool) */ public function setGroupKick($group_id, $user_id, bool $reject_add_request = false) { @@ -244,10 +262,11 @@ class OneBotV11 /** * 群组单人禁言 - * @see https://github.com/howmanybots/onebot/blob/master/v11/specs/api/public.md#set_group_ban-%E7%BE%A4%E7%BB%84%E5%8D%95%E4%BA%BA%E7%A6%81%E8%A8%80 - * @param $group_id - * @param $user_id - * @return null|array|bool + * @see https://github.com/botuniverse/onebot-11/blob/master/api/public.md#set_group_ban-%E7%BE%A4%E7%BB%84%E5%8D%95%E4%BA%BA%E7%A6%81%E8%A8%80 + * @param int|string $group_id 群ID + * @param int|string $user_id 用户ID + * @param int $duration 禁言时长 + * @return array|bool 返回API调用结果(数组)或异步API调用状态(bool) */ public function setGroupBan($group_id, $user_id, int $duration = 1800) { @@ -263,10 +282,10 @@ class OneBotV11 /** * 群组匿名用户禁言 - * @see https://github.com/howmanybots/onebot/blob/master/v11/specs/api/public.md#set_group_anonymous_ban-%E7%BE%A4%E7%BB%84%E5%8C%BF%E5%90%8D%E7%94%A8%E6%88%B7%E7%A6%81%E8%A8%80 - * @param $group_id - * @param $anonymous_or_flag - * @return null|array|bool + * @see https://github.com/botuniverse/onebot-11/blob/master/api/public.md#set_group_anonymous_ban-%E7%BE%A4%E7%BB%84%E5%8C%BF%E5%90%8D%E7%94%A8%E6%88%B7%E7%A6%81%E8%A8%80 + * @param int|string $group_id 群ID + * @param array|int|string $anonymous_or_flag 匿名禁言Flag或匿名用户对象 + * @return array|bool 返回API调用结果(数组)或异步API调用状态(bool) */ public function setGroupAnonymousBan($group_id, $anonymous_or_flag, int $duration = 1800) { @@ -282,9 +301,9 @@ class OneBotV11 /** * 群组全员禁言 - * @see https://github.com/howmanybots/onebot/blob/master/v11/specs/api/public.md#set_group_whole_ban-%E7%BE%A4%E7%BB%84%E5%85%A8%E5%91%98%E7%A6%81%E8%A8%80 - * @param $group_id - * @return null|array|bool + * @see https://github.com/botuniverse/onebot-11/blob/master/api/public.md#set_group_whole_ban-%E7%BE%A4%E7%BB%84%E5%85%A8%E5%91%98%E7%A6%81%E8%A8%80 + * @param int|string $group_id 群ID + * @return array|bool 返回API调用结果(数组)或异步API调用状态(bool) */ public function setGroupWholeBan($group_id, bool $enable = true) { @@ -299,10 +318,10 @@ class OneBotV11 /** * 群组设置管理员 - * @see https://github.com/howmanybots/onebot/blob/master/v11/specs/api/public.md#set_group_admin-%E7%BE%A4%E7%BB%84%E8%AE%BE%E7%BD%AE%E7%AE%A1%E7%90%86%E5%91%98 - * @param $group_id - * @param $user_id - * @return null|array|bool + * @see https://github.com/botuniverse/onebot-11/blob/master/api/public.md#set_group_admin-%E7%BE%A4%E7%BB%84%E8%AE%BE%E7%BD%AE%E7%AE%A1%E7%90%86%E5%91%98 + * @param int|string $group_id 群ID + * @param int|string $user_id 用户ID + * @return array|bool 返回API调用结果(数组)或异步API调用状态(bool) */ public function setGroupAdmin($group_id, $user_id, bool $enable = true) { @@ -318,9 +337,10 @@ class OneBotV11 /** * 群组匿名 - * @see https://github.com/howmanybots/onebot/blob/master/v11/specs/api/public.md#set_group_anonymous-%E7%BE%A4%E7%BB%84%E5%8C%BF%E5%90%8D - * @param $group_id - * @return null|array|bool + * @see https://github.com/botuniverse/onebot-11/blob/master/api/public.md#set_group_anonymous-%E7%BE%A4%E7%BB%84%E5%8C%BF%E5%90%8D + * @param int|string $group_id 群ID + * @param bool $enable 是否启用(默认为true) + * @return array|bool 返回API调用结果(数组)或异步API调用状态(bool) */ public function setGroupAnonymous($group_id, bool $enable = true) { @@ -335,10 +355,11 @@ class OneBotV11 /** * 设置群名片(群备注) - * @see https://github.com/howmanybots/onebot/blob/master/v11/specs/api/public.md#set_group_card-%E8%AE%BE%E7%BD%AE%E7%BE%A4%E5%90%8D%E7%89%87%E7%BE%A4%E5%A4%87%E6%B3%A8 - * @param $group_id - * @param $user_id - * @return null|array|bool + * @see https://github.com/botuniverse/onebot-11/blob/master/api/public.md#set_group_card-%E8%AE%BE%E7%BD%AE%E7%BE%A4%E5%90%8D%E7%89%87%E7%BE%A4%E5%A4%87%E6%B3%A8 + * @param int|string $group_id 群ID + * @param int|string $user_id 用户ID + * @param string $card 名片内容(默认为空) + * @return array|bool 返回API调用结果(数组)或异步API调用状态(bool) */ public function setGroupCard($group_id, $user_id, string $card = '') { @@ -354,12 +375,12 @@ class OneBotV11 /** * 设置群名 - * @see https://github.com/howmanybots/onebot/blob/master/v11/specs/api/public.md#set_group_name-%E8%AE%BE%E7%BD%AE%E7%BE%A4%E5%90%8D - * @param $group_id - * @param $group_name - * @return null|array|bool + * @see https://github.com/botuniverse/onebot-11/blob/master/api/public.md#set_group_name-%E8%AE%BE%E7%BD%AE%E7%BE%A4%E5%90%8D + * @param int|string $group_id 群ID + * @param string $group_name 群名 + * @return array|bool 返回API调用结果(数组)或异步API调用状态(bool) */ - public function setGroupName($group_id, $group_name) + public function setGroupName($group_id, string $group_name) { return $this->processAPI($this->connection, [ 'action' => $this->getActionName($this->prefix, __FUNCTION__), @@ -372,9 +393,10 @@ class OneBotV11 /** * 退出群组 - * @see https://github.com/howmanybots/onebot/blob/master/v11/specs/api/public.md#set_group_leave-%E9%80%80%E5%87%BA%E7%BE%A4%E7%BB%84 - * @param $group_id - * @return null|array|bool + * @see https://github.com/botuniverse/onebot-11/blob/master/api/public.md#set_group_leave-%E9%80%80%E5%87%BA%E7%BE%A4%E7%BB%84 + * @param int|string $group_id 群ID + * @param bool $is_dismiss 是否解散(默认为false) + * @return array|bool 返回API调用结果(数组)或异步API调用状态(bool) */ public function setGroupLeave($group_id, bool $is_dismiss = false) { @@ -389,10 +411,12 @@ class OneBotV11 /** * 设置群组专属头衔 - * @see https://github.com/howmanybots/onebot/blob/master/v11/specs/api/public.md#set_group_special_title-%E8%AE%BE%E7%BD%AE%E7%BE%A4%E7%BB%84%E4%B8%93%E5%B1%9E%E5%A4%B4%E8%A1%94 - * @param $group_id - * @param $user_id - * @return null|array|bool + * @see https://github.com/botuniverse/onebot-11/blob/master/api/public.md#set_group_special_title-%E8%AE%BE%E7%BD%AE%E7%BE%A4%E7%BB%84%E4%B8%93%E5%B1%9E%E5%A4%B4%E8%A1%94 + * @param int|string $group_id 群ID + * @param int|string $user_id 用户ID + * @param string $special_title 专属头衔内容 + * @param int $duration 持续时间(默认为-1,永久) + * @return array|bool 返回API调用结果(数组)或异步API调用状态(bool) */ public function setGroupSpecialTitle($group_id, $user_id, string $special_title = '', int $duration = -1) { @@ -409,9 +433,11 @@ class OneBotV11 /** * 处理加好友请求 - * @see https://github.com/howmanybots/onebot/blob/master/v11/specs/api/public.md#set_friend_add_request-%E5%A4%84%E7%90%86%E5%8A%A0%E5%A5%BD%E5%8F%8B%E8%AF%B7%E6%B1%82 - * @param $flag - * @return null|array|bool + * @see https://github.com/botuniverse/onebot-11/blob/master/api/public.md#set_friend_add_request-%E5%A4%84%E7%90%86%E5%8A%A0%E5%A5%BD%E5%8F%8B%E8%AF%B7%E6%B1%82 + * @param array|int|string $flag 处理加好友请求的flag + * @param bool $approve 是否同意(默认为true) + * @param string $remark 设置昵称(默认不设置) + * @return array|bool 返回API调用结果(数组)或异步API调用状态(bool) */ public function setFriendAddRequest($flag, bool $approve = true, string $remark = '') { @@ -427,12 +453,14 @@ class OneBotV11 /** * 处理加群请求/邀请 - * @see https://github.com/howmanybots/onebot/blob/master/v11/specs/api/public.md#set_group_add_request-%E5%A4%84%E7%90%86%E5%8A%A0%E7%BE%A4%E8%AF%B7%E6%B1%82%E9%82%80%E8%AF%B7 - * @param $flag - * @param $sub_type - * @return null|array|bool + * @see https://github.com/botuniverse/onebot-11/blob/master/api/public.md#set_group_add_request-%E5%A4%84%E7%90%86%E5%8A%A0%E7%BE%A4%E8%AF%B7%E6%B1%82%E9%82%80%E8%AF%B7 + * @param array|int|string $flag 处理加群请求的flag + * @param string $sub_type 处理请求类型(包含add和invite) + * @param bool $approve 是否同意(默认为true) + * @param string $reason 拒绝理由(仅在拒绝时有效,默认为空) + * @return array|bool 返回API调用结果(数组)或异步API调用状态(bool) */ - public function setGroupAddRequest($flag, $sub_type, bool $approve = true, string $reason = '') + public function setGroupAddRequest($flag, string $sub_type, bool $approve = true, string $reason = '') { return $this->processAPI($this->connection, [ 'action' => $this->getActionName($this->prefix, __FUNCTION__), @@ -447,8 +475,8 @@ class OneBotV11 /** * 获取登录号信息 - * @see https://github.com/howmanybots/onebot/blob/master/v11/specs/api/public.md#get_login_info-%E8%8E%B7%E5%8F%96%E7%99%BB%E5%BD%95%E5%8F%B7%E4%BF%A1%E6%81%AF - * @return null|array|bool + * @see https://github.com/botuniverse/onebot-11/blob/master/api/public.md#get_login_info-%E8%8E%B7%E5%8F%96%E7%99%BB%E5%BD%95%E5%8F%B7%E4%BF%A1%E6%81%AF + * @return array|bool 返回API调用结果(数组)或异步API调用状态(bool) */ public function getLoginInfo() { @@ -457,9 +485,10 @@ class OneBotV11 /** * 获取陌生人信息 - * @see https://github.com/howmanybots/onebot/blob/master/v11/specs/api/public.md#get_stranger_info-%E8%8E%B7%E5%8F%96%E9%99%8C%E7%94%9F%E4%BA%BA%E4%BF%A1%E6%81%AF - * @param $user_id - * @return null|array|bool + * @see https://github.com/botuniverse/onebot-11/blob/master/api/public.md#get_stranger_info-%E8%8E%B7%E5%8F%96%E9%99%8C%E7%94%9F%E4%BA%BA%E4%BF%A1%E6%81%AF + * @param int|string $user_id 用户ID + * @param bool $no_cache 是否不使用缓存(默认为false) + * @return array|bool 返回API调用结果(数组)或异步API调用状态(bool) */ public function getStrangerInfo($user_id, bool $no_cache = false) { @@ -474,8 +503,8 @@ class OneBotV11 /** * 获取好友列表 - * @see https://github.com/howmanybots/onebot/blob/master/v11/specs/api/public.md#get_friend_list-%E8%8E%B7%E5%8F%96%E5%A5%BD%E5%8F%8B%E5%88%97%E8%A1%A8 - * @return null|array|bool + * @see https://github.com/botuniverse/onebot-11/blob/master/api/public.md#get_friend_list-%E8%8E%B7%E5%8F%96%E5%A5%BD%E5%8F%8B%E5%88%97%E8%A1%A8 + * @return array|bool 返回API调用结果(数组)或异步API调用状态(bool) */ public function getFriendList() { @@ -486,9 +515,9 @@ class OneBotV11 /** * 获取群信息 - * @see https://github.com/howmanybots/onebot/blob/master/v11/specs/api/public.md#get_group_info-%E8%8E%B7%E5%8F%96%E7%BE%A4%E4%BF%A1%E6%81%AF - * @param $group_id - * @return null|array|bool + * @see https://github.com/botuniverse/onebot-11/blob/master/api/public.md#get_group_info-%E8%8E%B7%E5%8F%96%E7%BE%A4%E4%BF%A1%E6%81%AF + * @param int|string $group_id 群ID + * @return array|bool 返回API调用结果(数组)或异步API调用状态(bool) */ public function getGroupInfo($group_id, bool $no_cache = false) { @@ -503,8 +532,8 @@ class OneBotV11 /** * 获取群列表 - * @see https://github.com/howmanybots/onebot/blob/master/v11/specs/api/public.md#get_group_list-%E8%8E%B7%E5%8F%96%E7%BE%A4%E5%88%97%E8%A1%A8 - * @return null|array|bool + * @see https://github.com/botuniverse/onebot-11/blob/master/api/public.md#get_group_list-%E8%8E%B7%E5%8F%96%E7%BE%A4%E5%88%97%E8%A1%A8 + * @return array|bool 返回API调用结果(数组)或异步API调用状态(bool) */ public function getGroupList() { @@ -515,10 +544,10 @@ class OneBotV11 /** * 获取群成员信息 - * @see https://github.com/howmanybots/onebot/blob/master/v11/specs/api/public.md#get_group_member_info-%E8%8E%B7%E5%8F%96%E7%BE%A4%E6%88%90%E5%91%98%E4%BF%A1%E6%81%AF - * @param $group_id - * @param $user_id - * @return null|array|bool + * @see https://github.com/botuniverse/onebot-11/blob/master/api/public.md#get_group_member_info-%E8%8E%B7%E5%8F%96%E7%BE%A4%E6%88%90%E5%91%98%E4%BF%A1%E6%81%AF + * @param int|string $group_id 群ID + * @param int|string $user_id 用户ID + * @return array|bool 返回API调用结果(数组)或异步API调用状态(bool) */ public function getGroupMemberInfo($group_id, $user_id, bool $no_cache = false) { @@ -534,9 +563,9 @@ class OneBotV11 /** * 获取群成员列表 - * @see https://github.com/howmanybots/onebot/blob/master/v11/specs/api/public.md#get_group_member_list-%E8%8E%B7%E5%8F%96%E7%BE%A4%E6%88%90%E5%91%98%E5%88%97%E8%A1%A8 - * @param $group_id - * @return null|array|bool + * @see https://github.com/botuniverse/onebot-11/blob/master/api/public.md#get_group_member_list-%E8%8E%B7%E5%8F%96%E7%BE%A4%E6%88%90%E5%91%98%E5%88%97%E8%A1%A8 + * @param int|string $group_id 群ID + * @return array|bool 返回API调用结果(数组)或异步API调用状态(bool) */ public function getGroupMemberList($group_id) { @@ -550,12 +579,12 @@ class OneBotV11 /** * 获取群荣誉信息 - * @see https://github.com/howmanybots/onebot/blob/master/v11/specs/api/public.md#get_group_honor_info-%E8%8E%B7%E5%8F%96%E7%BE%A4%E8%8D%A3%E8%AA%89%E4%BF%A1%E6%81%AF - * @param $group_id - * @param $type - * @return null|array|bool + * @see https://github.com/botuniverse/onebot-11/blob/master/api/public.md#get_group_honor_info-%E8%8E%B7%E5%8F%96%E7%BE%A4%E8%8D%A3%E8%AA%89%E4%BF%A1%E6%81%AF + * @param int|string $group_id 群ID + * @param string $type 荣誉类型 + * @return array|bool 返回API调用结果(数组)或异步API调用状态(bool) */ - public function getGroupHonorInfo($group_id, $type) + public function getGroupHonorInfo($group_id, string $type) { return $this->processAPI($this->connection, [ 'action' => $this->getActionName($this->prefix, __FUNCTION__), @@ -568,8 +597,8 @@ class OneBotV11 /** * 获取 CSRF Token - * @see https://github.com/howmanybots/onebot/blob/master/v11/specs/api/public.md#get_csrf_token-%E8%8E%B7%E5%8F%96-csrf-token - * @return null|array|bool + * @see https://github.com/botuniverse/onebot-11/blob/master/api/public.md#get_csrf_token-%E8%8E%B7%E5%8F%96-csrf-token + * @return array|bool 返回API调用结果(数组)或异步API调用状态(bool) */ public function getCsrfToken() { @@ -580,8 +609,8 @@ class OneBotV11 /** * 获取 QQ 相关接口凭证 - * @see https://github.com/howmanybots/onebot/blob/master/v11/specs/api/public.md#get_credentials-%E8%8E%B7%E5%8F%96-qq-%E7%9B%B8%E5%85%B3%E6%8E%A5%E5%8F%A3%E5%87%AD%E8%AF%81 - * @return null|array|bool + * @see https://github.com/botuniverse/onebot-11/blob/master/api/public.md#get_credentials-%E8%8E%B7%E5%8F%96-qq-%E7%9B%B8%E5%85%B3%E6%8E%A5%E5%8F%A3%E5%87%AD%E8%AF%81 + * @return array|bool 返回API调用结果(数组)或异步API调用状态(bool) */ public function getCredentials(string $domain = '') { @@ -595,12 +624,12 @@ class OneBotV11 /** * 获取语音 - * @see https://github.com/howmanybots/onebot/blob/master/v11/specs/api/public.md#get_record-%E8%8E%B7%E5%8F%96%E8%AF%AD%E9%9F%B3 - * @param $file - * @param $out_format - * @return null|array|bool + * @see https://github.com/botuniverse/onebot-11/blob/master/api/public.md#get_record-%E8%8E%B7%E5%8F%96%E8%AF%AD%E9%9F%B3 + * @param string $file 文件 + * @param string $out_format 输出格式 + * @return array|bool 返回API调用结果(数组)或异步API调用状态(bool) */ - public function getRecord($file, $out_format) + public function getRecord(string $file, string $out_format) { return $this->processAPI($this->connection, [ 'action' => $this->getActionName($this->prefix, __FUNCTION__), @@ -613,11 +642,11 @@ class OneBotV11 /** * 获取图片 - * @see https://github.com/howmanybots/onebot/blob/master/v11/specs/api/public.md#get_image-%E8%8E%B7%E5%8F%96%E5%9B%BE%E7%89%87 - * @param $file - * @return null|array|bool + * @see https://github.com/botuniverse/onebot-11/blob/master/api/public.md#get_image-%E8%8E%B7%E5%8F%96%E5%9B%BE%E7%89%87 + * @param string $file 文件 + * @return array|bool 返回API调用结果(数组)或异步API调用状态(bool) */ - public function getImage($file) + public function getImage(string $file) { return $this->processAPI($this->connection, [ 'action' => $this->getActionName($this->prefix, __FUNCTION__), @@ -629,8 +658,8 @@ class OneBotV11 /** * 检查是否可以发送图片 - * @see https://github.com/howmanybots/onebot/blob/master/v11/specs/api/public.md#can_send_image-%E6%A3%80%E6%9F%A5%E6%98%AF%E5%90%A6%E5%8F%AF%E4%BB%A5%E5%8F%91%E9%80%81%E5%9B%BE%E7%89%87 - * @return null|array|bool + * @see https://github.com/botuniverse/onebot-11/blob/master/api/public.md#can_send_image-%E6%A3%80%E6%9F%A5%E6%98%AF%E5%90%A6%E5%8F%AF%E4%BB%A5%E5%8F%91%E9%80%81%E5%9B%BE%E7%89%87 + * @return array|bool 返回API调用结果(数组)或异步API调用状态(bool) */ public function canSendImage() { @@ -641,8 +670,8 @@ class OneBotV11 /** * 检查是否可以发送语音 - * @see https://github.com/howmanybots/onebot/blob/master/v11/specs/api/public.md#can_send_record-%E6%A3%80%E6%9F%A5%E6%98%AF%E5%90%A6%E5%8F%AF%E4%BB%A5%E5%8F%91%E9%80%81%E8%AF%AD%E9%9F%B3 - * @return null|array|bool + * @see https://github.com/botuniverse/onebot-11/blob/master/api/public.md#can_send_record-%E6%A3%80%E6%9F%A5%E6%98%AF%E5%90%A6%E5%8F%AF%E4%BB%A5%E5%8F%91%E9%80%81%E8%AF%AD%E9%9F%B3 + * @return array|bool 返回API调用结果(数组)或异步API调用状态(bool) */ public function canSendRecord() { @@ -653,8 +682,8 @@ class OneBotV11 /** * 获取运行状态 - * @see https://github.com/howmanybots/onebot/blob/master/v11/specs/api/public.md#get_status-%E8%8E%B7%E5%8F%96%E8%BF%90%E8%A1%8C%E7%8A%B6%E6%80%81 - * @return null|array|bool + * @see https://github.com/botuniverse/onebot-11/blob/master/api/public.md#get_status-%E8%8E%B7%E5%8F%96%E8%BF%90%E8%A1%8C%E7%8A%B6%E6%80%81 + * @return array|bool 返回API调用结果(数组)或异步API调用状态(bool) */ public function getStatus() { @@ -665,8 +694,8 @@ class OneBotV11 /** * 获取版本信息 - * @see https://github.com/howmanybots/onebot/blob/master/v11/specs/api/public.md#get_version_info-%E8%8E%B7%E5%8F%96%E7%89%88%E6%9C%AC%E4%BF%A1%E6%81%AF - * @return null|array|bool + * @see https://github.com/botuniverse/onebot-11/blob/master/api/public.md#get_version_info-%E8%8E%B7%E5%8F%96%E7%89%88%E6%9C%AC%E4%BF%A1%E6%81%AF + * @return array|bool 返回API调用结果(数组)或异步API调用状态(bool) */ public function getVersionInfo() { @@ -677,8 +706,9 @@ class OneBotV11 /** * 重启 OneBot 实现 - * @see https://github.com/howmanybots/onebot/blob/master/v11/specs/api/public.md#set_restart-%E9%87%8D%E5%90%AF-onebot-%E5%AE%9E%E7%8E%B0 - * @return null|array|bool + * @see https://github.com/botuniverse/onebot-11/blob/master/api/public.md#set_restart-%E9%87%8D%E5%90%AF-onebot-%E5%AE%9E%E7%8E%B0 + * @param int $delay 延迟时间(毫秒,默认为0) + * @return array|bool 返回API调用结果(数组)或异步API调用状态(bool) */ public function setRestart(int $delay = 0) { @@ -692,8 +722,8 @@ class OneBotV11 /** * 清理缓存 - * @see https://github.com/howmanybots/onebot/blob/master/v11/specs/api/public.md#clean_cache-%E6%B8%85%E7%90%86%E7%BC%93%E5%AD%98 - * @return null|array|bool + * @see https://github.com/botuniverse/onebot-11/blob/master/api/public.md#clean_cache-%E6%B8%85%E7%90%86%E7%BC%93%E5%AD%98 + * @return array|bool 返回API调用结果(数组)或异步API调用状态(bool) */ public function cleanCache() { @@ -705,8 +735,9 @@ class OneBotV11 /** * 获取内置支持的扩展API对象 * 现支持 go-cqhttp 的扩展API + * @params string $package_name 包名 * @throws ZMKnownException - * @return mixed + * @return mixed 返回包的操作对象 */ public function getExtendedAPI(string $package_name = 'go-cqhttp') { @@ -719,7 +750,12 @@ class OneBotV11 throw new ZMKnownException(zm_internal_errcode('E00071'), '无法找到对应的调用扩展类'); } - public function callExtendedAPI($action, $params = []) + /** + * @param string $action 动作(API)名称 + * @param array $params 参数 + * @return array|bool 返回API调用结果(数组)或异步API调用状态(bool) + */ + public function callExtendedAPI(string $action, array $params = []) { return $this->processAPI($this->connection, [ 'action' => $action, diff --git a/src/ZM/API/TuringAPI.php b/src/ZM/API/TuringAPI.php index 6b47ee97..4c6bf5ec 100644 --- a/src/ZM/API/TuringAPI.php +++ b/src/ZM/API/TuringAPI.php @@ -11,12 +11,12 @@ class TuringAPI { /** * 请求图灵API,返回图灵的消息 - * @param $msg - * @param $user_id - * @param $api - * @return string + * @param string $msg 消息 + * @param int|string $user_id 用户ID + * @param string $api API Key + * @return string 图灵的回复 */ - public static function getTuringMsg($msg, $user_id, $api) + public static function getTuringMsg(string $msg, $user_id, string $api): string { $origin = $msg; if (($cq = CQ::getCQ($msg)) !== null) {// 如有CQ码则去除 @@ -86,7 +86,11 @@ class TuringAPI return trim($final); } - public static function getResultStatus($r) + /** + * @param array $r 数据API回包 + * @return bool|string 错误消息或成功鸥鸟 + */ + public static function getResultStatus(array $r) { switch ($r['intent']['code']) { case 5000: diff --git a/src/ZM/Annotation/AnnotationParser.php b/src/ZM/Annotation/AnnotationParser.php index 3c4f38fd..a0cd37d1 100644 --- a/src/ZM/Annotation/AnnotationParser.php +++ b/src/ZM/Annotation/AnnotationParser.php @@ -116,7 +116,7 @@ class AnnotationParser $vs->class = $v; // 预处理1:将适用于每一个函数的注解到类注解重新注解到每个函数下面 - if ($vs instanceof ErgodicAnnotation) { + if (($vs instanceof ErgodicAnnotation) && ($vs instanceof AnnotationBase)) { foreach (($this->annotation_map[$v]['methods'] ?? []) as $method) { $copy = clone $vs; $copy->method = $method->getName(); @@ -208,10 +208,10 @@ class AnnotationParser } /** - * @param $path - * @param $indoor_name + * @param string $path 注册解析注解的路径 + * @param string $indoor_name 起始命名空间的名称 */ - public function addRegisterPath($path, $indoor_name) + public function addRegisterPath(string $path, string $indoor_name) { if (server()->worker_id === 0) { Console::verbose('Add register path: ' . $path . ' => ' . $indoor_name); @@ -220,10 +220,12 @@ class AnnotationParser } /** - * @param $events + * @param array $events 需要排序的 + * @param string $class_name 排序的类名 + * @param string $prefix 前缀 * @internal 用于 level 排序 */ - public function sortByLevel(&$events, string $class_name, string $prefix = '') + public function sortByLevel(array &$events, string $class_name, string $prefix = '') { if (is_a($class_name, Level::class, true)) { $class_name .= $prefix; diff --git a/src/ZM/Annotation/CQ/CQBefore.php b/src/ZM/Annotation/CQ/CQBefore.php index a56b40c6..7db292e6 100644 --- a/src/ZM/Annotation/CQ/CQBefore.php +++ b/src/ZM/Annotation/CQ/CQBefore.php @@ -35,7 +35,7 @@ class CQBefore extends AnnotationBase implements Level } /** - * @return mixed + * @return int 返回等级 */ public function getLevel(): int { diff --git a/src/ZM/Annotation/CQ/CQMetaEvent.php b/src/ZM/Annotation/CQ/CQMetaEvent.php index f6b7b6b6..90934f58 100644 --- a/src/ZM/Annotation/CQ/CQMetaEvent.php +++ b/src/ZM/Annotation/CQ/CQMetaEvent.php @@ -36,7 +36,7 @@ class CQMetaEvent extends AnnotationBase implements Level } /** - * @return mixed + * @return int 返回等级 */ public function getLevel(): int { diff --git a/src/ZM/Annotation/Swoole/OnTask.php b/src/ZM/Annotation/Swoole/OnTask.php index b0d8c361..10ed4ec7 100644 --- a/src/ZM/Annotation/Swoole/OnTask.php +++ b/src/ZM/Annotation/Swoole/OnTask.php @@ -38,7 +38,7 @@ class OnTask extends AnnotationBase implements Rule } /** - * @return mixed + * @return string 返回规则语句 */ public function getRule(): string { diff --git a/src/ZM/Context/Context.php b/src/ZM/Context/Context.php index a1f54638..16bffaeb 100644 --- a/src/ZM/Context/Context.php +++ b/src/ZM/Context/Context.php @@ -5,6 +5,7 @@ declare(strict_types=1); namespace ZM\Context; use Exception; +use Stringable; use Swoole\Coroutine; use Swoole\Http\Request; use Swoole\WebSocket\Frame; @@ -18,6 +19,7 @@ use ZM\Event\EventDispatcher; use ZM\Exception\InterruptException; use ZM\Exception\InvalidArgumentException; use ZM\Exception\WaitTimeoutException; +use ZM\Exception\ZMKnownException; use ZM\Http\Response; use ZM\Utils\CoMessage; use ZM\Utils\MessageUtil; @@ -168,9 +170,9 @@ class Context implements ContextInterface /** * only can used by cq->message event function - * @param $msg - * @param bool $yield - * @return array|bool + * @param array|string $msg 要回复的消息 + * @param bool $yield 是否协程挂起(true),是否绑定异步事件(Closure) + * @return array|bool 返回API调用结果 */ public function reply($msg, $yield = false) { @@ -199,9 +201,9 @@ class Context implements ContextInterface } /** - * @param $msg - * @param bool $yield - * @throws InterruptException + * @param array|string $msg 要回复的消息 + * @param bool $yield 是否协程挂起(true),是否绑定异步事件(Closure) + * @throws InterruptException 阻止消息被后续插件处理 */ public function finalReply($msg, $yield = false) { @@ -218,8 +220,7 @@ class Context implements ContextInterface * @param string $timeout_prompt * @throws WaitTimeoutException * @throws InvalidArgumentException - * @return string - * @noinspection PhpMissingReturnTypeInspection + * @return string 返回用户输入的内容 */ public function waitMessage($prompt = '', $timeout = 600, $timeout_prompt = '') { @@ -247,10 +248,12 @@ class Context implements ContextInterface } /** - * @param $mode - * @param $prompt_msg - * @throws WaitTimeoutException + * 根据选定的模式获取消息参数 + * @param int|string $mode 获取的模式 + * @param string|Stringable $prompt_msg 提示语回复 * @throws InvalidArgumentException + * @throws ZMKnownException + * @throws WaitTimeoutException * @return mixed|string */ public function getArgs($mode, $prompt_msg) @@ -282,10 +285,12 @@ class Context implements ContextInterface } /** - * @param string $prompt_msg - * @throws WaitTimeoutException + * 获取下一个参数 + * @param string $prompt_msg 提示语回复 * @throws InvalidArgumentException - * @return int|mixed|string + * @throws ZMKnownException + * @throws WaitTimeoutException + * @return int|mixed|string 返回获取的参数 */ public function getNextArg($prompt_msg = '') { @@ -293,10 +298,12 @@ class Context implements ContextInterface } /** - * @param string $prompt_msg - * @throws WaitTimeoutException + * 获取接下来所有的消息当成一个完整的参数(包含空格) + * @param string $prompt_msg 提示语回复 * @throws InvalidArgumentException - * @return int|mixed|string + * @throws ZMKnownException + * @throws WaitTimeoutException + * @return int|mixed|string 返回获取的参数 */ public function getFullArg($prompt_msg = '') { @@ -304,18 +311,23 @@ class Context implements ContextInterface } /** - * @param string $prompt_msg - * @throws WaitTimeoutException + * 获取下一个数字类型的参数 + * @param string $prompt_msg 提示语回复 * @throws InvalidArgumentException - * @return int|mixed|string + * @throws ZMKnownException + * @throws WaitTimeoutException + * @return int|mixed|string 返回获取的参数 */ public function getNumArg($prompt_msg = '') { return $this->getArgs(ZM_MATCH_NUMBER, $prompt_msg); } - /** @noinspection PhpMissingReturnTypeInspection */ - public function cloneFromParent() + /** + * @throws ZMKnownException + * @return ContextInterface 返回上下文 + */ + public function cloneFromParent(): ContextInterface { set_coroutine_params(self::$context[Coroutine::getPcid()] ?? self::$context[$this->cid]); return context(); diff --git a/src/ZM/DB/DB.php b/src/ZM/DB/DB.php index 92cc0d8e..3ed99675 100644 --- a/src/ZM/DB/DB.php +++ b/src/ZM/DB/DB.php @@ -25,10 +25,10 @@ class DB private static $table_list = []; /** - * @param $db_name + * @param string $db_name 数据库名称 * @throws DbException */ - public static function initTableList($db_name) + public static function initTableList(string $db_name) { if (!extension_loaded('mysqlnd')) { throw new DbException('Can not find mysqlnd PHP extension.'); @@ -40,10 +40,11 @@ class DB } /** - * @param $table_name + * @param string $table_name 表名 * @throws DbException + * @return Table 返回表对象 */ - public static function table($table_name): Table + public static function table(string $table_name): Table { if (Table::getTableInstance($table_name) === null) { if (in_array($table_name, self::$table_list)) { @@ -58,36 +59,34 @@ class DB } /** - * @param $line + * @param string $line SQL语句 * @throws DbException */ - public static function statement($line) + public static function statement(string $line) { self::rawQuery($line, []); } /** - * @param $line - * @throws DbException + * @param string $line SQL语句 + * @return bool 返回查询是否成功的结果 */ - public static function unprepared($line): bool + public static function unprepared(string $line): bool { - try { - $conn = SqlPoolStorage::$sql_pool->getConnection(); - if ($conn === false) { - SqlPoolStorage::$sql_pool->putConnection(null); - throw new DbException('无法连接SQL!' . $line); - } - $result = !($conn->query($line) === false); - SqlPoolStorage::$sql_pool->putConnection($conn); - return $result; - } catch (DBException $e) { - Console::warning($e->getMessage()); - throw $e; - } + $conn = SqlPoolStorage::$sql_pool->getConnection(); + $result = !($conn->query($line) === false); + SqlPoolStorage::$sql_pool->putConnection($conn); + return $result; } - public static function rawQuery(string $line, $params = [], $fetch_mode = ZM_DEFAULT_FETCH_MODE) + /** + * @param string $line SQL语句 + * @param array $params 查询参数 + * @param int $fetch_mode fetch规则 + * @throws DbException + * @return array|false 返回结果集或false + */ + public static function rawQuery(string $line, array $params = [], int $fetch_mode = ZM_DEFAULT_FETCH_MODE) { if (!is_array($params)) { $params = [$params]; @@ -98,15 +97,10 @@ class DB throw new DbException('未连接到任何数据库!'); } $conn = SqlPoolStorage::$sql_pool->getConnection(); - if ($conn === false) { - SqlPoolStorage::$sql_pool->putConnection(null); - throw new DbException('无法连接SQL!' . $line); - } $ps = $conn->prepare($line); if ($ps === false) { SqlPoolStorage::$sql_pool->putConnection(null); - /* @noinspection PhpUndefinedFieldInspection */ - throw new DbException('SQL语句查询错误,' . $line . ',错误信息:' . $conn->error); + throw new DbException('SQL语句查询错误,' . $line . ',错误信息:' . $conn->errorInfo()[2]); } if (!($ps instanceof PDOStatement) && !($ps instanceof PDOStatementProxy)) { var_dump($ps); @@ -129,7 +123,7 @@ class DB return $ps->fetchAll($fetch_mode); } catch (DbException $e) { if (mb_strpos($e->getMessage(), 'has gone away') !== false) { - zm_sleep(0.2); + zm_sleep(); Console::warning('Gone away of MySQL! retrying!'); return self::rawQuery($line, $params); } @@ -137,7 +131,7 @@ class DB throw $e; } catch (PDOException $e) { if (mb_strpos($e->getMessage(), 'has gone away') !== false) { - zm_sleep(0.2); + zm_sleep(); Console::warning('Gone away of MySQL! retrying!'); return self::rawQuery($line, $params); } diff --git a/src/ZM/DB/InsertBody.php b/src/ZM/DB/InsertBody.php index 860f195a..57b9712f 100644 --- a/src/ZM/DB/InsertBody.php +++ b/src/ZM/DB/InsertBody.php @@ -21,7 +21,8 @@ class InsertBody /** * InsertBody constructor. - * @param $row + * @param Table $table 表对象 + * @param array|string $row 行数据 */ public function __construct(Table $table, $row) { diff --git a/src/ZM/Event/SwooleEvent/OnManagerStart.php b/src/ZM/Event/SwooleEvent/OnManagerStart.php index 80d9e758..6571e4af 100644 --- a/src/ZM/Event/SwooleEvent/OnManagerStart.php +++ b/src/ZM/Event/SwooleEvent/OnManagerStart.php @@ -51,8 +51,7 @@ class OnManagerStart implements SwooleEvent if (Framework::$argv['watch']) { if (extension_loaded('inotify')) { Console::info('Enabled File watcher, framework will reload automatically.'); - /* @noinspection PhpUndefinedFieldInspection */ - Framework::$server->inotify = $fd = inotify_init(); + $fd = inotify_init(); $this->addWatcher(DataProvider::getSourceRootDir() . '/src', $fd); Event::add($fd, function () use ($fd) { $r = inotify_read($fd); diff --git a/src/ZM/Framework.php b/src/ZM/Framework.php index dcdbfa41..906c9ea1 100644 --- a/src/ZM/Framework.php +++ b/src/ZM/Framework.php @@ -659,10 +659,10 @@ class Framework /** * 解析命令行的 $argv 参数们 - * @param $args - * @param $add_port + * @param array $args 命令行参数 + * @param bool|string $add_port 是否添加端口号 */ - private function parseCliArgs($args, &$add_port) + private function parseCliArgs(array $args, &$add_port) { $coroutine_mode = true; global $terminal_id; diff --git a/src/ZM/Http/Response.php b/src/ZM/Http/Response.php index 20bdb1b0..f7d1187b 100644 --- a/src/ZM/Http/Response.php +++ b/src/ZM/Http/Response.php @@ -9,6 +9,8 @@ declare(strict_types=1); namespace ZM\Http; +use Stringable; + class Response { public $fd = 0; @@ -55,11 +57,11 @@ class Response } /** - * @param $name - * @param mixed ...$params - * @return mixed + * @param string $name 名称 + * @param mixed ...$params 参数 + * @return mixed 返回值 */ - public function cookie($name, ...$params) + public function cookie(string $name, ...$params) { return empty($params) ? $this->response->rawcookie($name) : $this->response->rawcookie($name, ...$params); } @@ -85,11 +87,10 @@ class Response } /** - * @param $http_code - * @param $reason + * @param mixed ...$params * @return mixed */ - public function status($http_code, ...$params) + public function status(int $http_code, ...$params) { $this->status_code = $http_code; if (!$this->is_end) { @@ -104,11 +105,10 @@ class Response } /** - * @param $http_code - * @param $reason + * @param mixed ...$params * @return mixed */ - public function setStatusCode($http_code, ...$params) + public function setStatusCode(int $http_code, ...$params) { if (!$this->is_end) { return empty($params) ? $this->response->status($http_code) : $this->response->status($http_code, ...$params); @@ -117,12 +117,11 @@ class Response } /** - * @param $key - * @param $value - * @param $ucwords + * @param array|string $value + * @param null|array|string $ucwords * @return mixed */ - public function header($key, $value, $ucwords = null) + public function header(string $key, $value, $ucwords = null) { if (!$this->is_end) { return $ucwords === null ? $this->response->header($key, $value) : $this->response->header($key, $value, $ucwords); @@ -131,12 +130,11 @@ class Response } /** - * @param $key - * @param $value - * @param $ucwords + * @param array|string $value + * @param null|array|string $ucwords * @return mixed */ - public function setHeader($key, $value, $ucwords = null) + public function setHeader(string $key, $value, $ucwords = null) { if (!$this->is_end) { return $ucwords === null ? $this->response->setHeader($key, $value) : $this->response->setHeader($key, $value, $ucwords); @@ -145,11 +143,10 @@ class Response } /** - * @param $key - * @param $value + * @param array|string $value * @return mixed */ - public function trailer($key, $value) + public function trailer(string $key, $value) { return $this->response->trailer($key, $value); } @@ -163,7 +160,7 @@ class Response } /** - * @param $content + * @param string|Stringable $content * @return mixed */ public function write($content) @@ -172,7 +169,7 @@ class Response } /** - * @param $content + * @param null|string|Stringable $content * @return mixed */ public function end($content = null) @@ -196,22 +193,19 @@ class Response } /** - * @param $filename - * @param $offset - * @param $length + * @param null|int|string $offset + * @param null|int|string $length * @return mixed */ - public function sendfile($filename, $offset = null, $length = null) + public function sendfile(string $filename, $offset = null, $length = null) { return $this->response->sendfile($filename, $offset, $length); } /** - * @param $location - * @param $http_code * @return mixed */ - public function redirect($location, $http_code = null) + public function redirect(string $location, ?int $http_code = null) { $this->is_end = true; return $this->response->redirect($location, $http_code); @@ -226,7 +220,7 @@ class Response } /** - * @param $fd + * @param mixed $fd * @return mixed */ public static function create($fd) @@ -243,9 +237,9 @@ class Response } /** - * @param $data - * @param null $opcode - * @param null $flags + * @param mixed $data + * @param mixed $opcode + * @param mixed $flags * @return mixed */ public function push($data, $opcode = null, $flags = null) diff --git a/src/ZM/Module/ModulePacker.php b/src/ZM/Module/ModulePacker.php index b6322f8d..054e6ba4 100644 --- a/src/ZM/Module/ModulePacker.php +++ b/src/ZM/Module/ModulePacker.php @@ -57,9 +57,9 @@ class ModulePacker /** * 设置输出文件夹 - * @param $path + * @param string $path 输出路径 */ - public function setOutputPath($path) + public function setOutputPath(string $path) { $this->output_path = $path; if (!is_dir($path)) { diff --git a/src/ZM/Module/QQBot.php b/src/ZM/Module/QQBot.php index c6838b7a..f398aed4 100644 --- a/src/ZM/Module/QQBot.php +++ b/src/ZM/Module/QQBot.php @@ -5,6 +5,7 @@ declare(strict_types=1); namespace ZM\Module; use Exception; +use Iterator; use ZM\Annotation\CQ\CQAfter; use ZM\Annotation\CQ\CQAPIResponse; use ZM\Annotation\CQ\CQBefore; @@ -17,6 +18,7 @@ use ZM\Config\ZMConfig; use ZM\Event\EventDispatcher; use ZM\Exception\InterruptException; use ZM\Exception\WaitTimeoutException; +use ZM\Exception\ZMKnownException; use ZM\Utils\CoMessage; use ZM\Utils\MessageUtil; use ZM\Utils\SingletonTrait; @@ -28,18 +30,24 @@ class QQBot { use SingletonTrait; + /** + * @throws ZMKnownException + * @throws InterruptException + */ public function handleByEvent() { - $data = json_decode(context()->getFrame()->data, true); + $data = json_decode(ctx()->getFrame()->data, true); $this->handle($data); } /** - * @param $data - * @param int $level + * @param array|Iterator $data 数据包 + * @param int $level 递归等级 * @throws InterruptException + * @throws ZMKnownException + * @throws Exception */ - public function handle($data, $level = 0) + public function handle($data, int $level = 0) { try { if ($level > 10) { @@ -102,11 +110,11 @@ class QQBot } /** - * @param $data - * @param $time + * @param array|Iterator $data 数据包 + * @param string $time 类型(pre或post) * @throws Exception */ - public function dispatchBeforeEvents($data, $time): EventDispatcher + public function dispatchBeforeEvents($data, string $time): EventDispatcher { $before = new EventDispatcher(CQBefore::class); if ($time === 'pre') { @@ -128,7 +136,7 @@ class QQBot } /** - * @param $data + * @param array|Iterator $data 数据包 * @throws InterruptException * @throws Exception */ @@ -147,7 +155,11 @@ class QQBot EventDispatcher::interrupt(); } }); - $s = MessageUtil::matchCommand(ctx()->getStringMessage(), ctx()->getData()); + $msg = $data['message']; + if (is_array($msg)) { + $msg = MessageUtil::arrayToStr($msg); + } + $s = MessageUtil::matchCommand($msg, ctx()->getData()); if ($s->status !== false) { if (!empty($s->match)) { ctx()->setCache('match', $s->match); @@ -173,7 +185,7 @@ class QQBot // 分发CQMessage事件 $msg_dispatcher = new EventDispatcher(CQMessage::class); $msg_dispatcher->setRuleFunction(function ($v) { - return ($v->message == '' || ($v->message == ctx()->getStringMessage())) + return ($v->message == '' || ($v->message == ctx()->getMessage())) && ($v->user_id == 0 || ($v->user_id == ctx()->getUserId())) && ($v->group_id == 0 || ($v->group_id == (ctx()->getGroupId() ?? 0))) && ($v->message_type == '' || ($v->message_type == ctx()->getMessageType())) @@ -190,7 +202,7 @@ class QQBot // Console::success("当前数据包:".json_encode(ctx()->getData())); $dispatcher = new EventDispatcher(CQMetaEvent::class); $dispatcher->setRuleFunction(function (CQMetaEvent $v) { - return $v->meta_event_type == '' || ($v->meta_event_type != '' && $v->meta_event_type == ctx()->getData()['meta_event_type']); + return $v->meta_event_type == '' || ($v->meta_event_type == ctx()->getData()['meta_event_type']); }); // eval(BP); $dispatcher->dispatchEvents(ctx()->getData()); @@ -199,26 +211,30 @@ class QQBot $dispatcher = new EventDispatcher(CQNotice::class); $dispatcher->setRuleFunction(function (CQNotice $v) { return - ($v->notice_type == '' || ($v->notice_type != '' && $v->notice_type == ctx()->getData()['notice_type'])) - && ($v->sub_type == '' || ($v->sub_type != '' && $v->sub_type == ctx()->getData()['sub_type'])) - && ($v->group_id == '' || ($v->group_id != '' && $v->group_id == ctx()->getData()['group_id'])) - && ($v->operator_id == '' || ($v->operator_id != '' && $v->operator_id == ctx()->getData()['operator_id'])); + ($v->notice_type == '' || ($v->notice_type == ctx()->getData()['notice_type'])) + && ($v->sub_type == '' || ($v->sub_type == ctx()->getData()['sub_type'])) + && ($v->group_id == '' || ($v->group_id == ctx()->getData()['group_id'])) + && ($v->operator_id == '' || ($v->operator_id == ctx()->getData()['operator_id'])); }); $dispatcher->dispatchEvents(ctx()->getData()); return; case 'request': $dispatcher = new EventDispatcher(CQRequest::class); $dispatcher->setRuleFunction(function (CQRequest $v) { - return ($v->request_type == '' || ($v->request_type != '' && $v->request_type == ctx()->getData()['request_type'])) - && ($v->sub_type == '' || ($v->sub_type != '' && $v->sub_type == ctx()->getData()['sub_type'])) - && ($v->user_id == 0 || ($v->user_id != 0 && $v->user_id == ctx()->getData()['user_id'])) - && ($v->comment == '' || ($v->comment != '' && $v->comment == ctx()->getData()['comment'])); + return ($v->request_type == '' || ($v->request_type == ctx()->getData()['request_type'])) + && ($v->sub_type == '' || ($v->sub_type == ctx()->getData()['sub_type'])) + && ($v->user_id == 0 || ($v->user_id == ctx()->getData()['user_id'])) + && ($v->comment == '' || ($v->comment == ctx()->getData()['comment'])); }); $dispatcher->dispatchEvents(ctx()->getData()); return; } } + /** + * @param mixed $data 分发事件数据包 + * @throws Exception + */ private function dispatchAfterEvents($data): EventDispatcher { $after = new EventDispatcher(CQAfter::class); @@ -230,7 +246,7 @@ class QQBot } /** - * @param $req + * @param mixed $req * @throws Exception */ private function dispatchAPIResponse($req) diff --git a/src/ZM/MySQL/MySQLStatement.php b/src/ZM/MySQL/MySQLStatement.php index 248b995b..d6c85a39 100644 --- a/src/ZM/MySQL/MySQLStatement.php +++ b/src/ZM/MySQL/MySQLStatement.php @@ -14,11 +14,10 @@ use Doctrine\DBAL\ParameterType; use IteratorAggregate; use PDO; use PDOStatement; -use Swoole\Database\PDOStatementProxy; class MySQLStatement implements IteratorAggregate, Statement { - /** @var PDOStatement|PDOStatementProxy */ + /** @var PDOStatement */ private $statement; public function __construct($obj) @@ -108,8 +107,15 @@ class MySQLStatement implements IteratorAggregate, Statement return new StatementIterator($this); } + /** + * @deprecated 最好不使用此方法,此方法可能存在 Bug + * @return mixed + */ public function current() { - return $this->statement->current(); + if (method_exists($this->statement, 'current')) { + return $this->statement->current(); + } + return null; } } diff --git a/src/ZM/MySQL/MySQLWrapper.php b/src/ZM/MySQL/MySQLWrapper.php index 7e213a39..1d3997a5 100644 --- a/src/ZM/MySQL/MySQLWrapper.php +++ b/src/ZM/MySQL/MySQLWrapper.php @@ -57,9 +57,8 @@ class MySQLWrapper /** * wrapper method - * @param $autoCommit */ - public function setAutoCommit($autoCommit) + public function setAutoCommit(bool $autoCommit) { $this->connection->setAutoCommit($autoCommit); } @@ -114,7 +113,7 @@ class MySQLWrapper } /** - * @param $table + * @param mixed $table * @throws DbException */ public function delete($table, array $criteria, array $types = []): int @@ -128,7 +127,7 @@ class MySQLWrapper /** * wrapper method - * @param $level + * @param mixed $level */ public function setTransactionIsolation($level): int { @@ -145,7 +144,7 @@ class MySQLWrapper /** * wrapper method - * @param $table + * @param mixed $table * @throws DbException */ public function update($table, array $data, array $criteria, array $types = []): int @@ -159,7 +158,7 @@ class MySQLWrapper /** * wrapper method - * @param $table + * @param mixed $table * @throws DbException */ public function insert($table, array $data, array $types = []): int @@ -173,7 +172,7 @@ class MySQLWrapper /** * wrapper method - * @param $str + * @param mixed $str */ public function quoteIdentifier($str): string { @@ -182,7 +181,7 @@ class MySQLWrapper /** * wrapper method - * @param $value + * @param mixed $value * @param int $type * @return mixed */ @@ -323,7 +322,7 @@ class MySQLWrapper /** * wrapper method - * @param $sql + * @param mixed $sql * @param array $types * @throws DbException */ @@ -339,9 +338,9 @@ class MySQLWrapper /** * wrapper method - * @param $sql - * @param $params - * @param $types + * @param mixed $sql + * @param mixed $params + * @param mixed $types * @throws DbException */ public function executeCacheQuery($sql, $params, $types, QueryCacheProfile $qcp): MySQLStatementWrapper @@ -356,7 +355,7 @@ class MySQLWrapper /** * wrapper method - * @param $sql + * @param mixed $sql * @throws DbException */ public function executeStatement($sql, array $params = [], array $types = []): int @@ -405,7 +404,7 @@ class MySQLWrapper /** * wrapper method - * @param $nestTransactionsWithSavepoints + * @param mixed $nestTransactionsWithSavepoints * @throws DbException */ public function setNestTransactionsWithSavepoints($nestTransactionsWithSavepoints) @@ -461,7 +460,7 @@ class MySQLWrapper /** * wrapper method - * @param $savepoint + * @param mixed $savepoint * @throws DbException */ public function createSavepoint($savepoint) @@ -475,7 +474,7 @@ class MySQLWrapper /** * wrapper method - * @param $savepoint + * @param mixed $savepoint * @throws DbException */ public function releaseSavepoint($savepoint) @@ -489,7 +488,7 @@ class MySQLWrapper /** * wrapper method - * @param $savepoint + * @param mixed $savepoint * @throws DbException */ public function rollbackSavepoint($savepoint) diff --git a/src/ZM/Store/LightCache.php b/src/ZM/Store/LightCache.php index db5546e8..7868845c 100644 --- a/src/ZM/Store/LightCache.php +++ b/src/ZM/Store/LightCache.php @@ -24,11 +24,11 @@ class LightCache private static $config = []; /** - * @param $config + * @param array $config 配置 * @throws Exception - * @return bool|mixed + * @return bool|mixed 返回失败(false)或创建SwooleTable成功结果 */ - public static function init($config) + public static function init(array $config) { self::$config = $config; self::$kv_table = new Table($config['size'], $config['hash_conflict_proportion']); @@ -169,7 +169,7 @@ class LightCache } /** - * @param $value + * @param mixed $value * @throws ZMException * @return bool */ diff --git a/src/ZM/Store/LightCacheInside.php b/src/ZM/Store/LightCacheInside.php index 8b90ba5c..edd86990 100644 --- a/src/ZM/Store/LightCacheInside.php +++ b/src/ZM/Store/LightCacheInside.php @@ -38,8 +38,7 @@ class LightCacheInside } /** - * @param array|int|string $value - * @return mixed + * @param array|int|string $value */ public static function set(string $table, string $key, $value): bool { @@ -58,13 +57,10 @@ class LightCacheInside } /** - * @param $name - * @param $size - * @param $str_size - * @param int $conflict_proportion + * @param float|int $conflict_proportion * @throws ZMException */ - private static function createTable($name, $size, $str_size, $conflict_proportion = 0) + private static function createTable(string $name, int $size, int $str_size, $conflict_proportion = 0) { self::$kv_table[$name] = new Table($size, $conflict_proportion); self::$kv_table[$name]->column('value', Table::TYPE_STRING, $str_size); diff --git a/src/ZM/Store/ZMAtomic.php b/src/ZM/Store/ZMAtomic.php index e8b38579..6a2977b2 100644 --- a/src/ZM/Store/ZMAtomic.php +++ b/src/ZM/Store/ZMAtomic.php @@ -12,10 +12,7 @@ class ZMAtomic /** @var Atomic[] */ public static $atomics; - /** - * @param $name - */ - public static function get($name): ?Atomic + public static function get(string $name): ?Atomic { return self::$atomics[$name] ?? null; } diff --git a/src/ZM/Utils/DataProvider.php b/src/ZM/Utils/DataProvider.php index 4c73c65f..034d450b 100644 --- a/src/ZM/Utils/DataProvider.php +++ b/src/ZM/Utils/DataProvider.php @@ -5,6 +5,9 @@ declare(strict_types=1); namespace ZM\Utils; +use Iterator; +use JsonSerializable; +use Traversable; use ZM\Config\ZMConfig; use ZM\Console\Console; @@ -76,11 +79,11 @@ class DataProvider /** * 将变量保存在zm_data下的数据目录,传入数组 - * @param $filename - * @param $file_array - * @return false|int + * @param string $filename 文件名 + * @param array|int|Iterator|JsonSerializable|string|Traversable $file_array 文件内容数组 + * @return false|int 返回文件大小或false */ - public static function saveToJson($filename, $file_array) + public static function saveToJson(string $filename, $file_array) { $path = ZMConfig::get('global', 'config_dir'); $r = explode('/', $filename); @@ -101,10 +104,10 @@ class DataProvider /** * 从json加载变量到内存 - * @param $filename - * @return null|mixed + * @param string $filename 文件名 + * @return null|mixed 返回文件内容数据或null */ - public static function loadFromJson($filename) + public static function loadFromJson(string $filename) { $path = ZMConfig::get('global', 'config_dir'); if (file_exists($path . $filename . '.json')) { @@ -115,12 +118,13 @@ class DataProvider /** * 递归或非递归扫描目录,可返回相对目录的文件列表或绝对目录的文件列表 - * @param $dir - * @param bool|string $relative + * @param string $dir 目录 + * @param bool $recursive 是否递归扫描子目录 + * @param bool $relative 是否返回相对目录,如果为true则返回相对目录,如果为false则返回绝对目录 * @return array|false * @since 2.5 */ - public static function scanDirFiles($dir, bool $recursive = true, $relative = false) + public static function scanDirFiles(string $dir, bool $recursive = true, bool $relative = false) { $dir = rtrim($dir, '/'); if (!is_dir($dir)) { @@ -157,11 +161,11 @@ class DataProvider /** * 检查路径是否为相对路径(根据第一个字符是否为"/"来判断) - * @param $path - * @return bool + * @param string $path 路径 + * @return bool 返回结果 * @since 2.5 */ - public static function isRelativePath($path) + public static function isRelativePath(string $path): bool { return strlen($path) > 0 && $path[0] !== '/'; } diff --git a/src/ZM/Utils/HttpUtil.php b/src/ZM/Utils/HttpUtil.php index 14999049..7918a030 100644 --- a/src/ZM/Utils/HttpUtil.php +++ b/src/ZM/Utils/HttpUtil.php @@ -58,11 +58,9 @@ class HttpUtil } /** - * @param $uri - * @param Response|\Swoole\Http\Response $response - * @return bool + * @param Response|\Swoole\Http\Response $response */ - public static function handleStaticPage($uri, $response, array $settings = []) + public static function handleStaticPage(string $uri, $response, array $settings = []): bool { $base_dir = $settings['document_root'] ?? ZMConfig::get('global', 'static_file_server')['document_root']; $base_index = $settings['document_index'] ?? ZMConfig::get('global', 'static_file_server')['document_index']; diff --git a/src/ZM/Utils/Manager/ModuleManager.php b/src/ZM/Utils/Manager/ModuleManager.php index 8da20af6..36cc9839 100644 --- a/src/ZM/Utils/Manager/ModuleManager.php +++ b/src/ZM/Utils/Manager/ModuleManager.php @@ -4,6 +4,7 @@ declare(strict_types=1); namespace ZM\Utils\Manager; +use Iterator; use ZM\Config\ZMConfig; use ZM\Console\Console; use ZM\Exception\ModulePackException; @@ -168,8 +169,8 @@ class ModuleManager /** * 解包模块 - * @param $module - * @return array|false + * @param array|Iterator $module 模块信息 + * @return array|false 返回截包的信息或false */ public static function unpackModule($module, array $options = []) { diff --git a/src/ZM/Utils/Manager/TaskManager.php b/src/ZM/Utils/Manager/TaskManager.php index 2e4d0100..195a0b57 100644 --- a/src/ZM/Utils/Manager/TaskManager.php +++ b/src/ZM/Utils/Manager/TaskManager.php @@ -11,11 +11,12 @@ use ZM\Console\Console; class TaskManager { /** - * @param $task_name - * @param mixed ...$params - * @return false|mixed + * @param string $task_name 任务名称 + * @param int $timeout 超时时间 + * @param mixed ...$params 传递参数 + * @return false|mixed 执行结果(如果执行失败返回false,否则为其他任意值) */ - public static function runTask($task_name, int $timeout = -1, ...$params) + public static function runTask(string $task_name, int $timeout = -1, ...$params) { if (!isset(server()->setting['task_worker_num'])) { Console::warning(zm_internal_errcode('E00056') . '未开启 TaskWorker 进程,请先修改 global 配置文件启用!'); diff --git a/src/ZM/Utils/Manager/WorkerManager.php b/src/ZM/Utils/Manager/WorkerManager.php index 097d36b6..af370764 100644 --- a/src/ZM/Utils/Manager/WorkerManager.php +++ b/src/ZM/Utils/Manager/WorkerManager.php @@ -19,11 +19,11 @@ class WorkerManager { /** * Worker 进程间通信触发的动作类型函数 - * @param $src_worker_id - * @param $data + * @param int $src_worker_id 源 Worker 进程 ID + * @param array $data 数据 * @throws Exception */ - public static function workerAction($src_worker_id, $data) + public static function workerAction(int $src_worker_id, array $data) { $server = server(); switch ($data['action'] ?? '') { @@ -106,12 +106,12 @@ class WorkerManager /** * 给 Worker 进程发送动作指令(包括自身,自身将直接执行) - * @param $worker_id - * @param $action - * @param $data + * @param int $worker_id 进程ID + * @param string $action 动作 + * @param mixed $data 参数 * @throws Exception */ - public static function sendActionToWorker($worker_id, $action, $data) + public static function sendActionToWorker(int $worker_id, string $action, $data) { $obj = ['action' => $action, 'data' => $data]; if (server()->worker_id === -1 && server()->getManagerPid() != posix_getpid()) { diff --git a/src/ZM/Utils/MessageUtil.php b/src/ZM/Utils/MessageUtil.php index 11edbc49..dba96f55 100644 --- a/src/ZM/Utils/MessageUtil.php +++ b/src/ZM/Utils/MessageUtil.php @@ -5,6 +5,7 @@ declare(strict_types=1); namespace ZM\Utils; use Exception; +use Iterator; use ReflectionException; use ReflectionMethod; use ZM\Annotation\CQ\CQCommand; @@ -21,8 +22,9 @@ class MessageUtil { /** * 下载消息中 CQ 码的所有图片,通过 url - * @param $msg - * @return array|false + * @param array|string $msg 消息或消息数组 + * @param null|string $path 保存路径 + * @return array|false 返回图片信息或失败返回false */ public static function downloadCQImage($msg, ?string $path = null) { @@ -54,7 +56,7 @@ class MessageUtil /** * 检查消息中是否含有图片 CQ 码 - * @param $msg + * @param array|string $msg 消息或消息数组 */ public static function containsImage($msg): bool { @@ -77,9 +79,10 @@ class MessageUtil * type == 0 : 返回图片的 base64 CQ 码 * type == 1 : 返回图片的 file://路径 CQ 码(路径必须为绝对路径) * type == 2 : 返回图片的 http://xxx CQ 码(默认为 /images/ 路径就是文件对应所在的目录) - * @param $file + * @param string $file 文件数据 + * @param int $type 文件类型(0,1,2可选,默认为0) */ - public static function getImageCQFromLocal($file, int $type = 0): string + public static function getImageCQFromLocal(string $file, int $type = 0): string { switch ($type) { case 0: @@ -95,10 +98,10 @@ class MessageUtil /** * 分割字符,将用户消息通过空格或换行分割为数组 - * @param $msg + * @param string $msg 消息内容 * @return array|string[] */ - public static function splitCommand($msg): array + public static function splitCommand(string $msg): array { $word = explode_msg(str_replace("\r", '', $msg)); if (empty($word)) { @@ -116,8 +119,9 @@ class MessageUtil } /** - * @param $msg - * @param $obj + * 根据CQCommand的规则匹配消息,获取是否匹配到对应的注解事件 + * @param array|string $msg 消息内容 + * @param array|Iterator $obj 数据对象 */ public static function matchCommand($msg, $obj): MatchResult { @@ -182,10 +186,11 @@ class MessageUtil } /** - * @param $command + * @param string $command 命令内容 + * @param string $reply 回复内容 * @throws Exception */ - public static function addShortCommand($command, string $reply) + public static function addShortCommand(string $command, string $reply) { for ($i = 0; $i < ZM_WORKER_NUM; ++$i) { WorkerManager::sendActionToWorker($i, 'add_short_command', [$command, $reply]); @@ -194,10 +199,12 @@ class MessageUtil /** * 字符串转数组 - * @param $msg - * @param false $trim_text + * @param string $msg 消息内容 + * @param bool $ignore_space 是否忽略空行 + * @param bool $trim_text 是否去除空格 + * @return array 返回数组 */ - public static function strToArray($msg, bool $ignore_space = true, bool $trim_text = false): array + public static function strToArray(string $msg, bool $ignore_space = true, bool $trim_text = false): array { $arr = []; while (($rear = mb_strstr($msg, '[CQ:')) !== false && ($end = mb_strstr($rear, ']', true)) !== false) { diff --git a/src/ZM/Utils/SignalListener.php b/src/ZM/Utils/SignalListener.php index 17cafe4e..875a2eb5 100644 --- a/src/ZM/Utils/SignalListener.php +++ b/src/ZM/Utils/SignalListener.php @@ -63,7 +63,7 @@ class SignalListener /** * 监听Worker/TaskWorker进程的Ctrl+C - * @param $worker_id + * @param int|string $worker_id 当前进程的ID */ public static function signalWorker(Server $server, $worker_id) { diff --git a/src/ZM/Utils/Terminal.php b/src/ZM/Utils/Terminal.php index 18f101cb..18b902d6 100644 --- a/src/ZM/Utils/Terminal.php +++ b/src/ZM/Utils/Terminal.php @@ -31,7 +31,7 @@ class Terminal if (self::$default_commands === false) { self::init(); } - $it = explodeMsg($cmd); + $it = explode_msg($cmd); $dispatcher = new EventDispatcher(TerminalCommand::class); $dispatcher->setRuleFunction(function ($v) use ($it) { /* @var TerminalCommand $v */ @@ -105,22 +105,22 @@ class Terminal /** * @TerminalCommand(command="status",description="显示Swoole Server运行状态(需要安装league/climate组件)") - * @noinspection PhpFullyQualifiedNameUsageInspection */ public function status() { - if (!class_exists('\\League\\CLImate\\CLImate')) { - Console::warning('你还没有安装 league/climate 组件,无法使用此功能!'); + if (class_exists('\\League\\CLImate\\CLImate')) { + $class = '\\League\\CLImate\\CLImate'; + $climate = new $class(); + $climate->output->addDefault('buffer'); + + $objs = server()->stats(); + $climate->columns($objs); + $obj = $climate->output->get('buffer')->get(); + $climate->output->get('buffer')->clean(); + echo $obj; return; } - $climate = new \League\CLImate\CLImate(); - $climate->output->addDefault('buffer'); - - $objs = server()->stats(); - $climate->columns($objs); - $obj = $climate->output->get('buffer')->get(); - $climate->output->get('buffer')->clean(); - echo $obj; + Console::warning('你还没有安装 league/climate 组件,无法使用此功能!'); } /** @@ -139,9 +139,8 @@ class Terminal /** * @TerminalCommand(command="call",description="用于执行不需要参数的动态函数,比如 `call \Module\Example\Hello hitokoto`") - * @param $it */ - public function call($it) + public function call(array $it) { $class_name = $it[1]; $function_name = $it[2]; @@ -154,9 +153,8 @@ class Terminal /** * @TerminalCommand(command="level",description="设置log等级,例如 `level 0|1|2|3|4`") - * @param $it */ - public function level($it) + public function level(array $it) { $level = intval(is_numeric($it[1] ?? 99) ? ($it[1] ?? 99) : 99); if ($level > 4 || $level < 0) { @@ -168,9 +166,8 @@ class Terminal /** * @TerminalCommand(command="bc",description="eval执行代码,但输入必须是将代码base64之后的,如 `bc em1faW5mbygn5L2g5aW9Jyk7`") - * @param $it */ - public function bc($it) + public function bc(array $it) { $code = base64_decode($it[1] ?? '', true); try { @@ -181,9 +178,8 @@ class Terminal /** * @TerminalCommand(command="echo",description="输出内容,用法:`echo hello`") - * @param $it */ - public function echoI($it) + public function echoI(array $it) { Console::info($it[1]); } diff --git a/src/ZM/Utils/ZMUtil.php b/src/ZM/Utils/ZMUtil.php index 6f2d825c..32c4328f 100644 --- a/src/ZM/Utils/ZMUtil.php +++ b/src/ZM/Utils/ZMUtil.php @@ -21,7 +21,6 @@ use function md5_file; use function pathinfo; use function server; use function str_replace; -use function substr; class ZMUtil { @@ -77,13 +76,13 @@ class ZMUtil /** * 使用Psr-4标准获取目录下的所有类 - * @param $dir - * @param $base_namespace - * @param null|mixed $rule - * @param bool $return_path_value + * @param string $dir 目录 + * @param string $base_namespace 基础命名空间 + * @param null|mixed $rule 规则 + * @param bool $return_path_value 是否返回文件路径 * @return string[] */ - public static function getClassesPsr4($dir, $base_namespace, $rule = null, $return_path_value = false): array + public static function getClassesPsr4(string $dir, string $base_namespace, $rule = null, bool $return_path_value = false): array { // 预先读取下composer的file列表 $composer = json_decode(file_get_contents(DataProvider::getSourceRootDir() . '/composer.json'), true); diff --git a/src/ZM/global_functions.php b/src/ZM/global_functions.php index 54ee5efc..bd0b8bbe 100644 --- a/src/ZM/global_functions.php +++ b/src/ZM/global_functions.php @@ -111,6 +111,10 @@ function match_pattern(string $pattern, string $subject): bool return false; } +/** + * @requires symfony/polyfill-ctype + * @return array|string[] + */ function split_explode(string $del, string $string, bool $divide_en = false): array { $str = explode($del, $string); @@ -521,7 +525,7 @@ function zm_dump($var, ...$moreVars) * * 与 {@link Console::info()} 一致 * - * @param $obj + * @param mixed $obj */ function zm_info($obj): void { @@ -533,7 +537,7 @@ function zm_info($obj): void * * 与 {@link Console::warning()} 一致 * - * @param $obj + * @param mixed $obj */ function zm_warning($obj): void { @@ -545,7 +549,7 @@ function zm_warning($obj): void * * 与 {@link Console::success()} 一致 * - * @param $obj + * @param mixed $obj */ function zm_success($obj): void { @@ -557,7 +561,7 @@ function zm_success($obj): void * * 与 {@link Console::debug()} 一致 * - * @param $obj + * @param mixed $obj */ function zm_debug($obj): void { @@ -569,7 +573,7 @@ function zm_debug($obj): void * * 与 {@link Console::verbose()} 一致 * - * @param $obj + * @param mixed $obj */ function zm_verbose($obj): void { @@ -581,7 +585,7 @@ function zm_verbose($obj): void * * 与 {@link Console::error()} 一致 * - * @param $obj + * @param mixed $obj */ function zm_error($obj): void { @@ -603,7 +607,7 @@ function zm_config(string $name, ?string $key = null) /** * 生成快速回复闭包 * - * @param $reply + * @param mixed $reply */ function quick_reply_closure($reply): Closure { @@ -615,7 +619,7 @@ function quick_reply_closure($reply): Closure /** * 获取内部错误码 * - * @param $code + * @param int|string $code */ function zm_internal_errcode($code): string { @@ -711,7 +715,7 @@ function matchPattern($pattern, $context): bool } /** - * @param mixed $message_type + * @param string $message_type 消息类型 * @deprecated 已废弃,请使用 {@link get_onebot_target_id_name()} */ function onebot_target_id_name(string $message_type): string