use solution repository instead of built-in (#209)

* use solution repository instead of built-in

* suppress static analysis
This commit is contained in:
sunxyw 2022-12-27 20:39:24 +08:00 committed by GitHub
parent 05b3321af7
commit 87840930e0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 75 additions and 30 deletions

View File

@ -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#'

View File

@ -50,13 +50,13 @@ class InitCommand extends Command
$section->write('<fg=gray>更新 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('<info>完成</info>');
@ -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}");
}
}

View File

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

View File

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

View File

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

View File

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

View File

@ -10,7 +10,6 @@ class SingletonViolationException extends ZMException
{
parent::__construct(
"{$singleton_class_name} 是单例模式,不允许初始化多个实例。",
"请检查代码,确保只初始化了一个 {$singleton_class_name} 实例。",
69
);
}

View File

@ -0,0 +1,30 @@
<?php
declare(strict_types=1);
namespace ZM\Exception\Solution;
class Solution
{
public function __construct(
private string $title,
private string $description,
private array $links,
) {
}
public function getSolutionTitle(): string
{
return $this->title;
}
public function getSolutionDescription(): string
{
return $this->description;
}
public function getDocumentationLinks(): array
{
return $this->links;
}
}

View File

@ -0,0 +1,22 @@
<?php
/** @noinspection PhpUndefinedClassInspection */
declare(strict_types=1);
namespace ZM\Exception\Solution;
use NunoMaduro\Collision\Contracts\SolutionsRepository;
class SolutionRepository implements SolutionsRepository
{
/**
* @return Solution[]
*/
public function getFromThrowable(\Throwable $throwable): array
{
return match ($throwable::class) {
default => [],
};
}
}

View File

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

View File

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

View File

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

View File

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