diff --git a/phpstan.neon b/phpstan.neon index 5e555e8a..a00aa75e 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -4,6 +4,8 @@ parameters: paths: - ./src/ - ./tests/ + excludePaths: + - ./src/ZM/Exception/Solution/SolutionRepository.php ignoreErrors: - '#Constant .* not found#' - '#PHPDoc tag @throws with type Psr\\Container\\ContainerExceptionInterface is not subtype of Throwable#' diff --git a/src/ZM/Command/InitCommand.php b/src/ZM/Command/InitCommand.php index 73ab2bd6..5a1d73d7 100644 --- a/src/ZM/Command/InitCommand.php +++ b/src/ZM/Command/InitCommand.php @@ -50,13 +50,13 @@ class InitCommand extends Command $section->write('更新 composer.json ... '); if (!file_exists($this->base_path . '/composer.json')) { - throw new InitException('未找到 composer.json 文件', '请检查当前目录是否为项目根目录', 41); + throw new InitException('未找到 composer.json 文件', 41); } try { $composer = json_decode(file_get_contents($this->base_path . '/composer.json'), true, 512, JSON_THROW_ON_ERROR); } catch (\JsonException $e) { - throw new InitException('解析 composer.json 文件失败', '请检查 composer.json 文件是否存在语法错误', 42, $e); + throw new InitException('解析 composer.json 文件失败', 42, $e); } if (!isset($composer['autoload'])) { @@ -68,7 +68,7 @@ class InitCommand extends Command try { file_put_contents($this->base_path . '/composer.json', json_encode($composer, JSON_THROW_ON_ERROR | JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES)); } catch (\JsonException $e) { - throw new InitException('写入 composer.json 文件失败', '', 0, $e); + throw new InitException('写入 composer.json 文件失败', 0, $e); } $section->writeln('完成'); @@ -146,7 +146,7 @@ class InitCommand extends Command if (file_exists($phar_link . '/vendor/autoload.php')) { $this->base_path = $current_dir; } else { - throw new InitException('框架启动模式不是 Composer 模式,无法进行初始化', '如果您是从 Github 下载的框架,请参阅文档进行源码模式启动', 42); + throw new InitException('框架启动模式不是 Composer 模式,无法进行初始化', 42); } } } @@ -166,11 +166,11 @@ class InitCommand extends Command && !mkdir($concurrent_dir = $this->base_path . $info['dirname'], 0777, true) && !is_dir($concurrent_dir) ) { - throw new InitException("无法创建目录 {$concurrent_dir}", '请检查目录权限'); + throw new InitException("无法创建目录 {$concurrent_dir}"); } if (copy($this->getVendorPath($file), $this->base_path . $file) === false) { - throw new InitException("无法复制文件 {$file}", '请检查目录权限'); + throw new InitException("无法复制文件 {$file}"); } } diff --git a/src/ZM/Exception/ConfigException.php b/src/ZM/Exception/ConfigException.php index 86233125..f32cb26b 100644 --- a/src/ZM/Exception/ConfigException.php +++ b/src/ZM/Exception/ConfigException.php @@ -12,11 +12,11 @@ class ConfigException extends ZMException public static function unsupportedFileType(string $file_path): ConfigException { - return new self("不支持的配置文件类型:{$file_path}", '请检查配置文件的后缀名是否正确', self::UNSUPPORTED_FILE_TYPE); + return new self("不支持的配置文件类型:{$file_path}", self::UNSUPPORTED_FILE_TYPE); } public static function loadConfigFailed(string $file_path, string $message): ConfigException { - return new self("加载配置文件失败:{$file_path},{$message}", '请检查配置文件的格式是否正确,并尝试按照错误信息排查', self::LOAD_CONFIG_FAILED); + return new self("加载配置文件失败:{$file_path},{$message}", self::LOAD_CONFIG_FAILED); } } diff --git a/src/ZM/Exception/Handler.php b/src/ZM/Exception/Handler.php index 24358f56..2d599b7c 100644 --- a/src/ZM/Exception/Handler.php +++ b/src/ZM/Exception/Handler.php @@ -5,22 +5,23 @@ declare(strict_types=1); namespace ZM\Exception; use OneBot\Exception\ExceptionHandler; -use OneBot\Exception\ExceptionHandlerInterface; +use ZM\Exception\Solution\SolutionRepository; -class Handler extends ExceptionHandler implements ExceptionHandlerInterface +class Handler extends ExceptionHandler { public function __construct() { parent::__construct(); + /** @noinspection ClassConstantCanBeUsedInspection */ + $ns = 'NunoMaduro\Collision\Handler'; + // TODO: 在 LibOB 发布新版时移除检查 + if (class_exists($ns) && method_exists($this, 'tryEnableCollision')) { + $this->tryEnableCollision(new SolutionRepository()); + } } public function handle(\Throwable $e): void { - if ($e instanceof ZMKnownException) { - // 如果是已知异常,则可以输出问题说明和解决方案 - // TODO - } - $this->handle0($e); } } diff --git a/src/ZM/Exception/InterruptException.php b/src/ZM/Exception/InterruptException.php index 073ffd4b..f2afa810 100644 --- a/src/ZM/Exception/InterruptException.php +++ b/src/ZM/Exception/InterruptException.php @@ -8,6 +8,6 @@ class InterruptException extends ZMException { public function __construct(public $return_var = null, $message = '', $code = 0, \Throwable $previous = null) { - parent::__construct($message, '', $code, $previous); + parent::__construct($message, $code, $previous); } } diff --git a/src/ZM/Exception/InvalidArgumentException.php b/src/ZM/Exception/InvalidArgumentException.php index 355d93e2..cb2f8190 100644 --- a/src/ZM/Exception/InvalidArgumentException.php +++ b/src/ZM/Exception/InvalidArgumentException.php @@ -6,9 +6,4 @@ namespace ZM\Exception; class InvalidArgumentException extends ZMException { - public function __construct($message = '', $code = 0, \Throwable $previous = null) - { - // TODO: change this to a better error message - parent::__construct($message, '', $code ?: 74, $previous); - } } diff --git a/src/ZM/Exception/SingletonViolationException.php b/src/ZM/Exception/SingletonViolationException.php index 3e5ed60c..f7ec9765 100644 --- a/src/ZM/Exception/SingletonViolationException.php +++ b/src/ZM/Exception/SingletonViolationException.php @@ -10,7 +10,6 @@ class SingletonViolationException extends ZMException { parent::__construct( "类 {$singleton_class_name} 是单例模式,不允许初始化多个实例。", - "请检查代码,确保只初始化了一个 {$singleton_class_name} 实例。", 69 ); } diff --git a/src/ZM/Exception/Solution/Solution.php b/src/ZM/Exception/Solution/Solution.php new file mode 100644 index 00000000..be0bba29 --- /dev/null +++ b/src/ZM/Exception/Solution/Solution.php @@ -0,0 +1,30 @@ +title; + } + + public function getSolutionDescription(): string + { + return $this->description; + } + + public function getDocumentationLinks(): array + { + return $this->links; + } +} diff --git a/src/ZM/Exception/Solution/SolutionRepository.php b/src/ZM/Exception/Solution/SolutionRepository.php new file mode 100644 index 00000000..7350b4a6 --- /dev/null +++ b/src/ZM/Exception/Solution/SolutionRepository.php @@ -0,0 +1,22 @@ + [], + }; + } +} diff --git a/src/ZM/Exception/WaitTimeoutException.php b/src/ZM/Exception/WaitTimeoutException.php index 6c0595a5..2c9e453c 100644 --- a/src/ZM/Exception/WaitTimeoutException.php +++ b/src/ZM/Exception/WaitTimeoutException.php @@ -10,7 +10,7 @@ class WaitTimeoutException extends ZMException public function __construct($module, $message = '', $code = 0, \Throwable $previous = null) { - parent::__construct($message, '', $code, $previous); + parent::__construct($message, $code, $previous); $this->module = $module; } } diff --git a/src/ZM/Exception/ZMException.php b/src/ZM/Exception/ZMException.php index 67abda11..e5fef9a8 100644 --- a/src/ZM/Exception/ZMException.php +++ b/src/ZM/Exception/ZMException.php @@ -6,8 +6,4 @@ namespace ZM\Exception; abstract class ZMException extends \Exception { - public function __construct(string $description, string $solution = '', int $code = 0, ?\Throwable $previous = null) - { - parent::__construct($description . PHP_EOL . $solution, $code, $previous); - } } diff --git a/src/ZM/Exception/ZMKnownException.php b/src/ZM/Exception/ZMKnownException.php index d26a4019..ade17285 100644 --- a/src/ZM/Exception/ZMKnownException.php +++ b/src/ZM/Exception/ZMKnownException.php @@ -11,7 +11,7 @@ class ZMKnownException extends ZMException { public function __construct($err_code, $message = '', $code = 0, \Throwable $previous = null) { - parent::__construct(zm_internal_errcode($err_code) . $message, '', $code, $previous); + parent::__construct(zm_internal_errcode($err_code) . $message, $code, $previous); if ($err_code === 'E99999') { $code = 0; // 这也太懒了吧 @@ -19,6 +19,6 @@ class ZMKnownException extends ZMException // 取最后两数 $code = (int) substr($err_code, -2); } - parent::__construct($message, '', $code, $previous); + parent::__construct($message, $code, $previous); } } diff --git a/src/ZM/Store/Database/DBException.php b/src/ZM/Store/Database/DBException.php index 49530821..7f87e8ed 100644 --- a/src/ZM/Store/Database/DBException.php +++ b/src/ZM/Store/Database/DBException.php @@ -10,6 +10,6 @@ class DBException extends ZMException { public function __construct(string $description, int $code = 0, ?\Throwable $previous = null) { - parent::__construct($description, '', $code, $previous); + parent::__construct($description, $code, $previous); } }