PHP80 小修 (#187)

* migrate-php80

fix styles
fix static analyse

* fix some bugs
This commit is contained in:
sunxyw
2022-12-19 20:22:47 +08:00
committed by GitHub
parent da516b487d
commit 8ff7da4d23
46 changed files with 180 additions and 366 deletions

View File

@@ -4,7 +4,7 @@ declare(strict_types=1);
namespace ZM\Annotation;
abstract class AnnotationBase implements \IteratorAggregate
abstract class AnnotationBase implements \IteratorAggregate, \Stringable
{
/** @var array|\Closure|string 方法名或闭包 */
public \Closure|string|array $method = '';
@@ -13,9 +13,9 @@ abstract class AnnotationBase implements \IteratorAggregate
public array $group = [];
public function __toString()
public function __toString(): string
{
$str = __CLASS__ . ': ';
$str = self::class . ': ';
foreach ($this as $k => $v) {
$str .= "\n\t" . $k . ' => ';
if (is_string($v)) {
@@ -39,10 +39,8 @@ abstract class AnnotationBase implements \IteratorAggregate
/**
* 在 InstantPlugin 下调用,设置回调或匿名函数
*
* @param \Closure|string $method
*/
public function on($method): AnnotationBase
public function on(\Closure|callable|string $method): AnnotationBase
{
$this->method = $method;
return $this;

View File

@@ -58,8 +58,7 @@ class AnnotationHandler
/**
* 设置执行前判断注解是否应该被执行的检查回调函数
*
* @param callable $rule 回调函数
* @return $this
* @param callable $rule 回调函数
*/
public function setRuleCallback(callable $rule): AnnotationHandler
{
@@ -71,8 +70,7 @@ class AnnotationHandler
/**
* 设置成功执行后有返回值时执行的返回值后续逻辑回调函数
*
* @param callable $return 回调函数
* @return $this
* @param callable $return 回调函数
*/
public function setReturnCallback(callable $return): AnnotationHandler
{
@@ -88,7 +86,7 @@ class AnnotationHandler
* @param mixed ...$params 传入的参数们
* @throws \Throwable
*/
public function handleAll(...$params)
public function handleAll(mixed ...$params)
{
try {
// 遍历注册的注解

View File

@@ -138,7 +138,7 @@ class AnnotationParser
// 预处理0排除所有非继承于 AnnotationBase 的注解
if (!$vs instanceof AnnotationBase) {
logger()->notice(get_class($vs) . ' is not extended from ' . AnnotationBase::class);
logger()->notice($vs::class . ' is not extended from ' . AnnotationBase::class);
continue;
}
@@ -172,7 +172,7 @@ class AnnotationParser
foreach ($methods_annotations as $method_anno) {
// 预处理3.0:排除所有非继承于 AnnotationBase 的注解
if (!$method_anno instanceof AnnotationBase) {
logger()->notice('Binding annotation ' . get_class($method_anno) . ' to ' . $v . '::' . $method_name . ' is not extended from ' . AnnotationBase::class);
logger()->notice('Binding annotation ' . $method_anno::class . ' to ' . $v . '::' . $method_name . ' is not extended from ' . AnnotationBase::class);
continue;
}
@@ -225,11 +225,11 @@ class AnnotationParser
if ($class_annotation instanceof ErgodicAnnotation) {
continue;
}
$o[get_class($class_annotation)][] = $class_annotation;
$o[$class_annotation::class][] = $class_annotation;
}
foreach (($obj['methods_annotations'] ?? []) as $methods_annotations) {
foreach ($methods_annotations as $annotation) {
$o[get_class($annotation)][] = $annotation;
$o[$annotation::class][] = $annotation;
}
}
}
@@ -238,7 +238,7 @@ class AnnotationParser
public function parseSpecial($annotation, $same_method_annotations = null): ?bool
{
foreach (($this->special_parsers[get_class($annotation)] ?? []) as $parser) {
foreach (($this->special_parsers[$annotation::class] ?? []) as $parser) {
$result = $parser($annotation, $same_method_annotations);
if (is_bool($result)) {
return $result;

View File

@@ -23,20 +23,16 @@ use ZM\Annotation\Interfaces\Level;
#[\Attribute(\Attribute::IS_REPEATABLE | \Attribute::TARGET_METHOD)]
class BindEvent extends AnnotationBase implements Level
{
/**
* @Required()
*/
public string $event_class;
public int $level = 800;
/**
* @param string $event_class 绑定事件的类型
*/
public function __construct(string $event_class, int $level = 800)
{
$this->event_class = $event_class;
$this->level = $level;
public function __construct(
/**
* @Required()
*/
public string $event_class,
public int $level = 800
) {
}
public function getLevel(): int

View File

@@ -19,10 +19,7 @@ use ZM\Annotation\AnnotationBase;
#[\Attribute(\Attribute::IS_REPEATABLE | \Attribute::TARGET_METHOD)]
class Init extends AnnotationBase
{
public int $worker = 0;
public function __construct(int $worker = 0)
public function __construct(public int $worker = 0)
{
$this->worker = $worker;
}
}

View File

@@ -20,13 +20,11 @@ use ZM\Annotation\Interfaces\ErgodicAnnotation;
#[\Attribute(\Attribute::IS_REPEATABLE | \Attribute::TARGET_CLASS)]
class Controller extends AnnotationBase implements ErgodicAnnotation
{
/**
* @Required()
*/
public string $prefix = '';
public function __construct(string $prefix)
{
$this->prefix = $prefix;
public function __construct(
/**
* @Required()
*/
public string $prefix
) {
}
}

View File

@@ -19,26 +19,18 @@ use ZM\Annotation\AnnotationBase;
#[\Attribute(\Attribute::IS_REPEATABLE | \Attribute::TARGET_METHOD)]
class Route extends AnnotationBase
{
/**
* @Required()
*/
public string $route = '';
public string $name = '';
public array $request_method = ['GET', 'POST'];
/**
* Routing path params binding. eg. {"id"="\d+"}
*/
public array $params = [];
public function __construct($route, $name = '', $request_method = ['GET', 'POST'], $params = [])
{
$this->route = $route;
$this->name = $name;
$this->request_method = $request_method;
$this->params = $params;
public function __construct(
/**
* @Required()
*/
public $route,
public $name = '',
public $request_method = ['GET', 'POST'],
/**
* Routing path params binding. eg. {"id"="\d+"}
*/
public $params = []
) {
}
public static function make($route, $name = '', $request_method = ['GET', 'POST'], $params = []): static

View File

@@ -21,18 +21,14 @@ use ZM\Annotation\Interfaces\ErgodicAnnotation;
class Middleware extends AnnotationBase implements ErgodicAnnotation
{
/**
* @Required()
* @param string[] $params
*/
public string $name;
/**
* @var string[]
*/
public array $params = [];
public function __construct($name, $params = [])
{
$this->name = $name;
$this->params = $params;
public function __construct(
/**
* @Required()
*/
public $name,
public array $params = []
) {
}
}

View File

@@ -22,59 +22,13 @@ use ZM\Exception\ZMKnownException;
#[\Attribute(\Attribute::IS_REPEATABLE | \Attribute::TARGET_METHOD)]
class BotCommand extends AnnotationBase implements Level
{
public string $name = '';
public string $match = '';
public string $pattern = '';
public string $regex = '';
public string $start_with = '';
public string $end_with = '';
public string $keyword = '';
/** @var string[] */
public array $alias = [];
public string $message_type = '';
public string $user_id = '';
public string $group_id = '';
public int $level = 20;
private array $arguments = [];
public function __construct(
$name = '',
$match = '',
$pattern = '',
$regex = '',
$start_with = '',
$end_with = '',
$keyword = '',
$alias = [],
$message_type = '',
$user_id = '',
$group_id = '',
$level = 20
) {
$this->name = $name;
$this->match = $match;
$this->pattern = $pattern;
$this->regex = $regex;
$this->start_with = $start_with;
$this->end_with = $end_with;
$this->keyword = $keyword;
$this->alias = $alias;
$this->message_type = $message_type;
$this->user_id = $user_id;
$this->group_id = $group_id;
$this->level = $level;
/**
* @param string[] $alias
*/
public function __construct(public $name = '', public $match = '', public $pattern = '', public $regex = '', public $start_with = '', public $end_with = '', public $keyword = '', public $alias = [], public $message_type = '', public $user_id = '', public $group_id = '', public $level = 20)
{
}
public static function make(
@@ -95,7 +49,6 @@ class BotCommand extends AnnotationBase implements Level
}
/**
* @return $this
* @throws InvalidArgumentException
* @throws ZMKnownException
*/

View File

@@ -19,32 +19,8 @@ use ZM\Annotation\AnnotationBase;
#[\Attribute(\Attribute::TARGET_METHOD | \Attribute::IS_REPEATABLE)]
class BotEvent extends AnnotationBase
{
public ?string $type;
public ?string $detail_type;
public ?string $impl;
public ?string $platform;
public ?string $self_id;
public ?string $sub_type;
public function __construct(
?string $type = null,
?string $detail_type = null,
?string $impl = null,
?string $platform = null,
?string $self_id = null,
?string $sub_type = null
) {
$this->type = $type;
$this->detail_type = $detail_type;
$this->impl = $impl;
$this->platform = $platform;
$this->self_id = $self_id;
$this->sub_type = $sub_type;
public function __construct(public ?string $type = null, public ?string $detail_type = null, public ?string $impl = null, public ?string $platform = null, public ?string $self_id = null, public ?string $sub_type = null)
{
}
public static function make(

View File

@@ -20,25 +20,8 @@ use ZM\Exception\ZMKnownException;
#[\Attribute(\Attribute::IS_REPEATABLE | \Attribute::TARGET_ALL)]
class CommandArgument extends AnnotationBase implements ErgodicAnnotation
{
/**
* @Required()
*/
public string $name;
public string $description = '';
public string $type = 'string';
public bool $required = false;
public string $prompt = '';
public string $default = '';
public int $timeout = 60;
public int $error_prompt_policy = 1;
/**
* @param string $name 参数名称(可以是中文)
* @param string $description 参数描述(默认为空)
@@ -49,23 +32,19 @@ class CommandArgument extends AnnotationBase implements ErgodicAnnotation
* @throws InvalidArgumentException|ZMKnownException
*/
public function __construct(
string $name,
string $description = '',
/**
* @Required()
*/
public string $name,
public string $description = '',
string $type = 'string',
bool $required = false,
string $prompt = '',
string $default = '',
int $timeout = 60,
int $error_prompt_policy = 1
public bool $required = false,
public string $prompt = '',
public string $default = '',
public int $timeout = 60,
public int $error_prompt_policy = 1
) {
$this->name = $name;
$this->description = $description;
$this->type = $this->fixTypeName($type);
$this->required = $required;
$this->prompt = $prompt;
$this->default = $default;
$this->timeout = $timeout;
$this->error_prompt_policy = $error_prompt_policy;
if ($this->type === 'bool') {
if ($this->default === '') {
$this->default = 'yes';