diff --git a/phpstan.neon b/phpstan.neon index 65c6048c..f5f4499d 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -1,8 +1,10 @@ parameters: reportUnmatchedIgnoredErrors: false - level: 2 + level: 4 paths: - ./src/ ignoreErrors: - '#Used constant OS_TYPE_(LINUX|WINDOWS) not found#' - '#Constant .* not found#' + dynamicConstantNames: + - SWOOLE_VERSION diff --git a/src/Module/Example/Hello.php b/src/Module/Example/Hello.php index 5db9d750..1e3de096 100644 --- a/src/Module/Example/Hello.php +++ b/src/Module/Example/Hello.php @@ -15,6 +15,7 @@ use ZM\Annotation\Swoole\OnRequestEvent; use ZM\API\CQ; use ZM\API\OneBotV11; use ZM\API\TuringAPI; +use ZM\Config\ZMConfig; use ZM\ConnectionManager\ConnectionObject; use ZM\Console\Console; use ZM\Event\EventDispatcher; @@ -95,11 +96,11 @@ class Hello public function turingAPI() { $user_id = ctx()->getUserId(); - $api = ''; // 请在这里填入你的图灵机器人的apikey + $api = ZMConfig::get('global', 'custom.turing_apikey') ?? ''; // 请在这里填入你的图灵机器人的apikey if ($api === '') { return false; } // 如果没有填入apikey则此功能关闭 - if (($this->_running_annotation ?? null) instanceof CQCommand) { + if (property_exists($this, '_running_annotation') && ($this->_running_annotation instanceof CQCommand)) { $msg = ctx()->getFullArg('我在!有什么事吗?'); } else { $msg = ctx()->getMessage(); diff --git a/src/ZM/API/CQ.php b/src/ZM/API/CQ.php index 43190bba..eec46a6f 100644 --- a/src/ZM/API/CQ.php +++ b/src/ZM/API/CQ.php @@ -379,9 +379,9 @@ class CQ /** * 获取消息中第一个CQ码 - * @param string $msg 消息内容 - * @param bool $is_object 是否以对象形式返回,如果为False的话,返回数组形式(默认为false) - * @return array|CQObject 返回的CQ码(数组或对象) + * @param string $msg 消息内容 + * @param bool $is_object 是否以对象形式返回,如果为False的话,返回数组形式(默认为false) + * @return null|array|CQObject 返回的CQ码(数组或对象) */ public static function getCQ(string $msg, bool $is_object = false) { diff --git a/src/ZM/Annotation/AnnotationBase.php b/src/ZM/Annotation/AnnotationBase.php index 51bc966d..f7e313a1 100644 --- a/src/ZM/Annotation/AnnotationBase.php +++ b/src/ZM/Annotation/AnnotationBase.php @@ -4,9 +4,12 @@ declare(strict_types=1); namespace ZM\Annotation; +use ArrayIterator; use Closure; +use IteratorAggregate; +use Traversable; -abstract class AnnotationBase +abstract class AnnotationBase implements IteratorAggregate { public $method = ''; @@ -35,4 +38,9 @@ abstract class AnnotationBase } return $str; } + + public function getIterator(): Traversable + { + return new ArrayIterator($this); + } } diff --git a/src/ZM/Annotation/AnnotationParser.php b/src/ZM/Annotation/AnnotationParser.php index a0cd37d1..1bfcfb07 100644 --- a/src/ZM/Annotation/AnnotationParser.php +++ b/src/ZM/Annotation/AnnotationParser.php @@ -38,7 +38,7 @@ class AnnotationParser private $middlewares = []; - /** @var null|AnnotationReader */ + /** @var null|AnnotationReader|DualReader */ private $reader; private $req_mapping = []; diff --git a/src/ZM/DB/DB.php b/src/ZM/DB/DB.php index 3ed99675..3b29dc8b 100644 --- a/src/ZM/DB/DB.php +++ b/src/ZM/DB/DB.php @@ -9,8 +9,6 @@ declare(strict_types=1); namespace ZM\DB; use PDOException; -use PDOStatement; -use Swoole\Database\PDOStatementProxy; use ZM\Console\Console; use ZM\Exception\DbException; use ZM\MySQL\MySQLManager; @@ -102,11 +100,6 @@ class DB SqlPoolStorage::$sql_pool->putConnection(null); throw new DbException('SQL语句查询错误,' . $line . ',错误信息:' . $conn->errorInfo()[2]); } - if (!($ps instanceof PDOStatement) && !($ps instanceof PDOStatementProxy)) { - var_dump($ps); - SqlPoolStorage::$sql_pool->putConnection(null); - throw new DbException('语句查询错误!返回的不是 PDOStatement' . $line); - } if ($params == []) { $result = $ps->execute(); } elseif (!is_array($params)) { diff --git a/src/ZM/DB/SelectBody.php b/src/ZM/DB/SelectBody.php index e324179f..0427b016 100644 --- a/src/ZM/DB/SelectBody.php +++ b/src/ZM/DB/SelectBody.php @@ -47,7 +47,7 @@ class SelectBody } /** - * @param int $fetch_mode + * @param mixed $fetch_mode * @throws DbException */ public function fetchAll($fetch_mode = ZM_DEFAULT_FETCH_MODE) @@ -66,7 +66,7 @@ class SelectBody } /** - * @param null $key + * @param null|mixed $key * @throws DbException * @return null|mixed */ @@ -83,10 +83,9 @@ class SelectBody } /** - * @param int $fetch_mode * @throws DbException */ - public function execute($fetch_mode = ZM_DEFAULT_FETCH_MODE) + public function execute(int $fetch_mode = ZM_DEFAULT_FETCH_MODE) { $str = $this->queryPrepare(); $this->result = DB::rawQuery($str[0], $str[1], $fetch_mode); diff --git a/src/ZM/Event/EventDispatcher.php b/src/ZM/Event/EventDispatcher.php index 90bf2d76..72940e4c 100644 --- a/src/ZM/Event/EventDispatcher.php +++ b/src/ZM/Event/EventDispatcher.php @@ -145,10 +145,11 @@ class EventDispatcher /** * @param mixed $v - * @param null $rule_func + * @param null|mixed $rule_func * @param mixed ...$params * @throws InterruptException * @throws AnnotationException + * @throws Error * @return bool * @noinspection PhpMissingReturnTypeInspection */ diff --git a/src/ZM/Event/SwooleEvent/OnManagerStart.php b/src/ZM/Event/SwooleEvent/OnManagerStart.php index 6571e4af..4c324f1a 100644 --- a/src/ZM/Event/SwooleEvent/OnManagerStart.php +++ b/src/ZM/Event/SwooleEvent/OnManagerStart.php @@ -1,7 +1,6 @@ dispatchEvents($conn); $dispatcher->dispatchEvents($conn); // Console::success("Used ".round((microtime(true) - $starttime) * 1000, 3)." ms!"); - } catch (Exception $e) { - $error_msg = $e->getMessage() . ' at ' . $e->getFile() . '(' . $e->getLine() . ')'; - Console::error(zm_internal_errcode('E00017') . 'Uncaught exception ' . get_class($e) . ' when calling "message": ' . $error_msg); - Console::trace(); - } catch (Error $e) { + } catch (Throwable $e) { $error_msg = $e->getMessage() . ' at ' . $e->getFile() . '(' . $e->getLine() . ')'; Console::error(zm_internal_errcode('E00017') . 'Uncaught ' . get_class($e) . ' when calling "message": ' . $error_msg); Console::trace(); diff --git a/src/ZM/Event/SwooleEvent/OnPipeMessage.php b/src/ZM/Event/SwooleEvent/OnPipeMessage.php index 4bfa420a..2c08812b 100644 --- a/src/ZM/Event/SwooleEvent/OnPipeMessage.php +++ b/src/ZM/Event/SwooleEvent/OnPipeMessage.php @@ -6,9 +6,8 @@ declare(strict_types=1); namespace ZM\Event\SwooleEvent; -use Error; -use Exception; use Swoole\Server; +use Throwable; use ZM\Annotation\Swoole\SwooleHandler; use ZM\Console\Console; use ZM\Event\SwooleEvent; @@ -25,11 +24,7 @@ class OnPipeMessage implements SwooleEvent $data = json_decode($data, true); try { WorkerManager::workerAction($src_worker_id, $data); - } catch (Exception $e) { - $error_msg = $e->getMessage() . ' at ' . $e->getFile() . '(' . $e->getLine() . ')'; - Console::error(zm_internal_errcode('E00021') . 'Uncaught exception ' . get_class($e) . ' when calling "pipeMessage": ' . $error_msg); - Console::trace(); - } catch (Error $e) { + } catch (Throwable $e) { $error_msg = $e->getMessage() . ' at ' . $e->getFile() . '(' . $e->getLine() . ')'; Console::error(zm_internal_errcode('E00021') . 'Uncaught ' . get_class($e) . ' when calling "pipeMessage": ' . $error_msg); Console::trace(); diff --git a/src/ZM/Event/SwooleEvent/OnWorkerStart.php b/src/ZM/Event/SwooleEvent/OnWorkerStart.php index 26fbfb5f..18cdc98c 100644 --- a/src/ZM/Event/SwooleEvent/OnWorkerStart.php +++ b/src/ZM/Event/SwooleEvent/OnWorkerStart.php @@ -13,7 +13,7 @@ use ReflectionException; use Swoole\Coroutine; use Swoole\Database\PDOConfig; use Swoole\Process; -use Swoole\Server; +use Swoole\WebSocket\Server; use ZM\Annotation\AnnotationParser; use ZM\Annotation\Swoole\OnMessageEvent; use ZM\Annotation\Swoole\OnStart; @@ -68,12 +68,7 @@ class OnWorkerStart implements SwooleEvent return; } // DataProvider::saveBuffer(); - /* @var Server $server */ - if (server() === null) { - $server->shutdown(); - } else { - server()->shutdown(); - } + $server->shutdown(); }); Console::verbose("Worker #{$server->worker_id} starting"); diff --git a/src/ZM/Framework.php b/src/ZM/Framework.php index 906c9ea1..e651476b 100644 --- a/src/ZM/Framework.php +++ b/src/ZM/Framework.php @@ -447,7 +447,7 @@ class Framework } /** - * @param null $id_or_name + * @param null|int|string $id_or_name * @throws ZMKnownException * @internal */ diff --git a/src/ZM/Http/StaticFileHandler.php b/src/ZM/Http/StaticFileHandler.php index e3c07f59..b4e3fef3 100644 --- a/src/ZM/Http/StaticFileHandler.php +++ b/src/ZM/Http/StaticFileHandler.php @@ -19,17 +19,16 @@ class StaticFileHandler if (strpos($full_path, $path) !== 0) { $response->status(403); $response->end('403 Forbidden'); - return true; + return; } if (is_file($full_path)) { $exp = strtolower(pathinfo($full_path)['extension'] ?? 'unknown'); $response->setHeader('Content-Type', ZMConfig::get('file_header')[$exp] ?? 'application/octet-stream'); $response->end(file_get_contents($full_path)); - return true; + return; } } $response->status(404); $response->end(HttpUtil::getHttpCodePage(404)); - return true; } } diff --git a/src/ZM/MySQL/MySQLConnection.php b/src/ZM/MySQL/MySQLConnection.php index bb40044e..5e505633 100644 --- a/src/ZM/MySQL/MySQLConnection.php +++ b/src/ZM/MySQL/MySQLConnection.php @@ -10,9 +10,7 @@ use Doctrine\DBAL\Driver\Connection; use Doctrine\DBAL\ParameterType; use PDO; use PDOException; -use PDOStatement; use Swoole\Database\PDOProxy; -use Swoole\Database\PDOStatementProxy; use ZM\Console\Console; use ZM\Exception\DbException; use ZM\Store\MySQL\SqlPoolStorage; @@ -34,23 +32,31 @@ class MySQLConnection implements Connection SqlPoolStorage::$sql_pool->putConnection($this->conn); } + /** + * @param mixed $sql + * @param mixed $options + * @throws DbException + */ public function prepare($sql, $options = []) { try { Console::debug('Running SQL prepare: ' . $sql); $statement = $this->conn->prepare($sql, $options); - assert(($statement instanceof PDOStatementProxy) || ($statement instanceof PDOStatement)); + assert($statement !== false); } catch (PDOException $exception) { throw new DbException($exception->getMessage(), $exception->getCode(), $exception); } return new MySQLStatement($statement); } + /** + * @throws DbException + */ public function query(...$args) { try { $statement = $this->conn->query(...$args); - assert(($statement instanceof PDOStatementProxy) || ($statement instanceof PDOStatement)); + assert($statement !== false); } catch (PDOException $exception) { throw new DbException($exception->getMessage(), $exception->getCode(), $exception); } @@ -62,6 +68,10 @@ class MySQLConnection implements Connection return $this->conn->quote($value, $type); } + /** + * @param mixed $sql + * @throws DbException + */ public function exec($sql) { try { @@ -74,6 +84,10 @@ class MySQLConnection implements Connection } } + /** + * @param null|mixed $name + * @throws DbException + */ public function lastInsertId($name = null) { try { diff --git a/src/ZM/Store/MySQL/SqlPoolStorage.php b/src/ZM/Store/MySQL/SqlPoolStorage.php index 584ede36..c335798d 100644 --- a/src/ZM/Store/MySQL/SqlPoolStorage.php +++ b/src/ZM/Store/MySQL/SqlPoolStorage.php @@ -8,6 +8,6 @@ use ZM\MySQL\MySQLPool; class SqlPoolStorage { - /** @var MySQLPool */ + /** @var null|MySQLPool */ public static $sql_pool; } diff --git a/src/ZM/Utils/Terminal.php b/src/ZM/Utils/Terminal.php index 18b902d6..92fd41fc 100644 --- a/src/ZM/Utils/Terminal.php +++ b/src/ZM/Utils/Terminal.php @@ -7,6 +7,7 @@ declare(strict_types=1); namespace ZM\Utils; use Doctrine\Common\Annotations\AnnotationReader; +use Error; use Exception; use ReflectionClass; use Swoole\Process; @@ -22,6 +23,7 @@ class Terminal /** * @throws Exception + * @throws Error * @return bool * @noinspection PhpMissingReturnTypeInspection * @noinspection PhpUnused diff --git a/src/ZM/Utils/ZMUtil.php b/src/ZM/Utils/ZMUtil.php index 32c4328f..47df4fc9 100644 --- a/src/ZM/Utils/ZMUtil.php +++ b/src/ZM/Utils/ZMUtil.php @@ -76,13 +76,13 @@ class ZMUtil /** * 使用Psr-4标准获取目录下的所有类 - * @param string $dir 目录 - * @param string $base_namespace 基础命名空间 - * @param null|mixed $rule 规则 - * @param bool $return_path_value 是否返回文件路径 + * @param string $dir 目录 + * @param string $base_namespace 基础命名空间 + * @param null|mixed $rule 规则 + * @param bool|string $return_path_value 是否返回文件路径,返回文件路径的话传入字符串 * @return string[] */ - public static function getClassesPsr4(string $dir, string $base_namespace, $rule = null, bool $return_path_value = false): array + public static function getClassesPsr4(string $dir, string $base_namespace, $rule = null, $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 bd0b8bbe..296565b5 100644 --- a/src/ZM/global_functions.php +++ b/src/ZM/global_functions.php @@ -409,7 +409,7 @@ function hash_message(array $message): string * * 与 {@link Framework::$server} 一致 */ -function server(): ?Server +function server(): Server { return Framework::$server; }