fix code to phpstan-level-2

This commit is contained in:
crazywhalecc
2022-04-02 23:37:22 +08:00
committed by Jerry Ma
parent be9874a9ad
commit eecbe49955
31 changed files with 450 additions and 384 deletions

View File

@@ -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] !== '/';
}

View File

@@ -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'];

View File

@@ -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 = [])
{

View File

@@ -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 配置文件启用!');

View File

@@ -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()) {

View File

@@ -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 文件类型012可选默认为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) {

View File

@@ -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)
{

View File

@@ -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]);
}

View File

@@ -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);