From 84c253a9f6186685bf6a24c03e7df31f55d4ea51 Mon Sep 17 00:00:00 2001 From: whwyy Date: Tue, 12 Dec 2023 15:35:38 +0800 Subject: [PATCH] eee --- Kiri.php | 8 +- ReturnTypeWillChange.php | 15 -- composer.json | 3 +- kiri-engine/Abstracts/BaseApplication.php | 10 +- kiri-engine/Abstracts/Component.php | 6 +- kiri-engine/Abstracts/LocalService.php | 4 +- kiri-engine/Application.php | 20 +- kiri-engine/Coordinator.php | 2 +- kiri-engine/Core/ArrayAccess.php | 148 +++++++------ kiri-engine/Core/HashMap.php | 8 +- kiri-engine/Core/Help.php | 4 +- kiri-engine/Core/Json.php | 242 +++++++++++----------- kiri-engine/Core/Str.php | 12 +- kiri-engine/Core/Xml.php | 68 +++--- kiri-engine/Error/ErrorHandler.php | 7 +- kiri-engine/Error/StdoutLogger.php | 2 +- kiri-engine/Redis/Redis.php | 14 +- 17 files changed, 271 insertions(+), 302 deletions(-) delete mode 100644 ReturnTypeWillChange.php diff --git a/Kiri.php b/Kiri.php index c6549df5..3c38b7b3 100644 --- a/Kiri.php +++ b/Kiri.php @@ -50,7 +50,7 @@ class Kiri * @param string|array $className * @param array $construct * @return mixed - * @throws Exception + * @throws */ public static function createObject(string|array $className, array $construct = []): mixed { @@ -72,7 +72,7 @@ class Kiri /** * @return \Kiri\Pool\Pool - * @throws ReflectionException|Exception + * @throws */ public static function getPool(): \Kiri\Pool\Pool { @@ -99,7 +99,7 @@ class Kiri /** * @return string - * @throws Exception + * @throws */ public static function getStoragePath(): string { @@ -123,7 +123,7 @@ class Kiri /** * @return StdoutLogger - * @throws ReflectionException|Exception + * @throws */ public static function getLogger(): StdoutLogger { diff --git a/ReturnTypeWillChange.php b/ReturnTypeWillChange.php deleted file mode 100644 index 3f034cf8..00000000 --- a/ReturnTypeWillChange.php +++ /dev/null @@ -1,15 +0,0 @@ -=8.0", + "php": ">=8.3", "ext-json": "*", "ext-fileinfo": "*", "ext-pdo": "*", @@ -53,7 +53,6 @@ }, "files": [ "Kiri.php", - "ReturnTypeWillChange.php", "function.php" ] } diff --git a/kiri-engine/Abstracts/BaseApplication.php b/kiri-engine/Abstracts/BaseApplication.php index ed25bccd..ea517048 100644 --- a/kiri-engine/Abstracts/BaseApplication.php +++ b/kiri-engine/Abstracts/BaseApplication.php @@ -41,9 +41,7 @@ abstract class BaseApplication extends LocalService * @param EventProvider $provider * @param ConfigProvider $config * @param ContainerInterface $container - * @throws ContainerExceptionInterface - * @throws InitException - * @throws NotFoundExceptionInterface + * @throws */ public function __construct(public EventProvider $provider, public ConfigProvider $config, public ContainerInterface $container) { @@ -71,7 +69,7 @@ abstract class BaseApplication extends LocalService /** * @param ConfigProvider $config * @return void - * @throws InitException + * @throws */ public function parseStorage(ConfigProvider $config): void { @@ -91,9 +89,7 @@ abstract class BaseApplication extends LocalService /** * @param ConfigProvider $config * @return void - * @throws ContainerExceptionInterface - * @throws NotFoundExceptionInterface - * @throws Exception + * @throws */ public function parseEvents(ConfigProvider $config): void { diff --git a/kiri-engine/Abstracts/Component.php b/kiri-engine/Abstracts/Component.php index cb174302..c8689d95 100644 --- a/kiri-engine/Abstracts/Component.php +++ b/kiri-engine/Abstracts/Component.php @@ -51,7 +51,7 @@ class Component implements Configure /** * @return StdoutLogger - * @throws ReflectionException + * @throws */ public function getLogger(): StdoutLogger { @@ -62,7 +62,7 @@ class Component implements Configure /** * @param string $name * @return mixed - * @throws Exception + * @throws */ public function __get(string $name) { @@ -81,7 +81,7 @@ class Component implements Configure * @param string $name * @param $value * @return void - * @throws Exception + * @throws */ public function __set(string $name, $value): void { diff --git a/kiri-engine/Abstracts/LocalService.php b/kiri-engine/Abstracts/LocalService.php index dcb6794e..6fc13d10 100644 --- a/kiri-engine/Abstracts/LocalService.php +++ b/kiri-engine/Abstracts/LocalService.php @@ -33,7 +33,7 @@ class LocalService extends Component implements LocalServiceInterface /** * @param string $name * @return mixed - * @throws Exception + * @throws */ public function get(string $name): mixed { @@ -66,7 +66,7 @@ class LocalService extends Component implements LocalServiceInterface /** * @param string $name * @return mixed - * @throws Exception + * @throws */ public function __get(string $name) { diff --git a/kiri-engine/Application.php b/kiri-engine/Application.php index 09278d44..9b250fa2 100644 --- a/kiri-engine/Application.php +++ b/kiri-engine/Application.php @@ -55,7 +55,7 @@ class Application extends BaseApplication /** * @return void - * @throws Exception + * @throws */ public function init(): void { @@ -71,9 +71,7 @@ class Application extends BaseApplication /** * @param OnBeforeCommandExecute $beforeCommandExecute * @return void - * @throws ContainerExceptionInterface - * @throws NotFoundExceptionInterface - * @throws ReflectionException + * @throws */ public function beforeCommandExecute(OnBeforeCommandExecute $beforeCommandExecute): void { @@ -90,8 +88,7 @@ class Application extends BaseApplication /** * @param string ...$services * @return $this - * @throws ContainerExceptionInterface - * @throws NotFoundExceptionInterface + * @throws */ public function import(string ...$services): static { @@ -112,8 +109,7 @@ class Application extends BaseApplication /** * @param Kernel $kernel * @return $this - * @throws ContainerExceptionInterface - * @throws NotFoundExceptionInterface + * @throws */ public function commands(Kernel $kernel): static { @@ -127,8 +123,7 @@ class Application extends BaseApplication /** * @param string ...$command * @return void - * @throws ContainerExceptionInterface - * @throws NotFoundExceptionInterface + * @throws */ public function command(string ...$command): void { @@ -142,10 +137,7 @@ class Application extends BaseApplication /** * @param array $argv * @return void - * @throws ContainerExceptionInterface - * @throws NotFoundExceptionInterface - * @throws ReflectionException - * @throws Exception|ExceptionInterface + * @throws */ public function execute(array $argv): void { diff --git a/kiri-engine/Coordinator.php b/kiri-engine/Coordinator.php index 9940347e..8be74e3a 100644 --- a/kiri-engine/Coordinator.php +++ b/kiri-engine/Coordinator.php @@ -7,7 +7,7 @@ namespace Kiri; class Coordinator { - const WORKER_START = 'worker:start'; + const string WORKER_START = 'worker:start'; private bool $waite = false; diff --git a/kiri-engine/Core/ArrayAccess.php b/kiri-engine/Core/ArrayAccess.php index 4ff52417..aa037d26 100644 --- a/kiri-engine/Core/ArrayAccess.php +++ b/kiri-engine/Core/ArrayAccess.php @@ -10,8 +10,6 @@ declare(strict_types=1); namespace Kiri\Core; -use Exception; - /** * Class ArrayAccess * @package Kiri\Core @@ -19,82 +17,82 @@ use Exception; class ArrayAccess { - /** - * @param $data - * @return array - * @throws Exception - */ - public static function toArray($data): array - { - if (!is_object($data) && !is_array($data)) { - return []; - } - if (is_object($data)) { - $data = self::objToArray($data); - } - $tmp = []; - if (!is_array($data)) { - return $tmp; - } - foreach ($data as $key => $val) { - if (is_array($val) || is_object($val)) { - $tmp[$key] = self::toArray($val); - } else { - $tmp[$key] = $val; - } - } - return $tmp; - } + /** + * @param $data + * @return array + * @throws + */ + public static function toArray($data): array + { + if (!is_object($data) && !is_array($data)) { + return []; + } + if (is_object($data)) { + $data = self::objToArray($data); + } + $tmp = []; + if (!is_array($data)) { + return $tmp; + } + foreach ($data as $key => $val) { + if (is_array($val) || is_object($val)) { + $tmp[$key] = self::toArray($val); + } else { + $tmp[$key] = $val; + } + } + return $tmp; + } - /** - * @param $data - * @return array - * @throws Exception - */ - public static function objToArray($data): array - { - if (!is_object($data)) { - return $data; - } - if (method_exists($data, 'get')) { - $data = $data->get(); - if (is_array($data)) { - return $data; - } - } - if (method_exists($data, 'toArray')) { - $data = $data->toArray(); - } else { - $data = get_object_vars((object)$data); - } - return $data; - } + /** + * @param $data + * @return array + * @throws + */ + public static function objToArray($data): array + { + if (!is_object($data)) { + return $data; + } + if (method_exists($data, 'get')) { + $data = $data->get(); + if (is_array($data)) { + return $data; + } + } + if (method_exists($data, 'toArray')) { + $data = $data->toArray(); + } else { + $data = get_object_vars((object)$data); + } + return $data; + } - /** - * @param array $oldArray - * @param array $newArray - * @return array - */ - public static function merge(array $oldArray, array $newArray): array - { - if (empty($oldArray)) { - return $newArray; - } else if (empty($newArray)) { - return $oldArray; - } - foreach ($newArray as $item => $value) { - if (!isset($oldArray[$item])) { - $oldArray[$item] = $value; - } - if (is_array($value)) { - $oldArray[$item] = self::merge($oldArray[$item], $value); - } else { - $oldArray[$item] = $value; - } - } - return $oldArray; - } + /** + * @param array $oldArray + * @param array $newArray + * @return array + */ + public static function merge(array $oldArray, array $newArray): array + { + if (empty($oldArray)) { + return $newArray; + } else if (empty($newArray)) { + return $oldArray; + } + foreach ($newArray as $item => $value) { + if (!isset($oldArray[$item])) { + $oldArray[$item] = $value; + } + if (is_array($value)) { + $oldArray[$item] = self::merge($oldArray[$item], $value); + } else { + $oldArray[$item] = $value; + } + } + return $oldArray; + } } diff --git a/kiri-engine/Core/HashMap.php b/kiri-engine/Core/HashMap.php index 8aa9bd0c..238ce070 100644 --- a/kiri-engine/Core/HashMap.php +++ b/kiri-engine/Core/HashMap.php @@ -38,7 +38,7 @@ class HashMap implements \ArrayAccess, \IteratorAggregate * @param string $key * @param $value */ - public function put(string $key, $value) + public function put(string $key, $value): void { $this->lists[$key] = $value; } @@ -78,7 +78,7 @@ class HashMap implements \ArrayAccess, \IteratorAggregate /** * @param string $key */ - public function del(string $key) + public function del(string $key): void { if (!$this->has($key)) { return; @@ -122,7 +122,7 @@ class HashMap implements \ArrayAccess, \IteratorAggregate * @param mixed $value */ #[ReturnTypeWillChange] - public function offsetSet(mixed $offset, mixed $value) + public function offsetSet(mixed $offset, mixed $value): void { $this->put($offset, $value); } @@ -132,7 +132,7 @@ class HashMap implements \ArrayAccess, \IteratorAggregate * @param mixed $offset */ #[ReturnTypeWillChange] - public function offsetUnset(mixed $offset) + public function offsetUnset(mixed $offset): void { unset($this->lists[$offset]); } diff --git a/kiri-engine/Core/Help.php b/kiri-engine/Core/Help.php index cb9b603e..a14ced1a 100644 --- a/kiri-engine/Core/Help.php +++ b/kiri-engine/Core/Help.php @@ -61,7 +61,7 @@ class Help /** * @param $xml * @return mixed - * @throws Exception + * @throws */ public static function toArray($xml): mixed { @@ -111,7 +111,7 @@ class Help /** * @param $parameter * @return array|false|string - * @throws Exception + * @throws */ public static function toString($parameter): bool|array|string { diff --git a/kiri-engine/Core/Json.php b/kiri-engine/Core/Json.php index ee1e4e4e..d8d673e2 100644 --- a/kiri-engine/Core/Json.php +++ b/kiri-engine/Core/Json.php @@ -20,125 +20,125 @@ use Throwable; */ class Json { - - - /** - * @param $data - * @return false|string - */ - public static function encode($data): bool|string - { - if (empty($data)) { - return false; - } - if (is_array($data)) { - return json_encode($data, JSON_UNESCAPED_UNICODE); - } - return $data; - } - - - /** - * @param $data - * @param bool $asArray - * @return mixed - */ - public static function decode($data, bool $asArray = true): mixed - { - if (is_array($data) || is_numeric($data)) { - return $data; - } - if (!is_string($data)) return null; - return json_decode($data, $asArray); - } - - - /** - * @param string $message - * @param int $code - * @param array|string $data - * @param int $count - * @return string - */ - public static function jsonFail(string $message, int $code = 500, array|string $data = [], int $count = 0): string - { - return json_encode(['code' => $code, 'param' => $data, 'message' => $message, 'count' => $count], JSON_UNESCAPED_UNICODE); - } - - - /** - * @param string $message - * @param array|string $data - * @param int $count - * @return string - */ - public static function jsonSuccess(array|string $data = [], string $message = "ok", int $count = 0): string - { - return json_encode(['code' => 0, 'param' => $data, 'message' => $message, 'count' => $count], JSON_UNESCAPED_UNICODE); - } - - - /** - * @param $code - * @param string|array $message - * @param array|int $data - * @param int $count - * @param array $exPageInfo - * @return string|bool - */ - public static function to($code, string|array $message = '', array|int $data = [], int $count = 0, array $exPageInfo = []): string|bool - { - $params['code'] = $code; - if (!is_string($message)) { - $params['message'] = 'System success.'; - $params['param'] = $message; - $params['exPageInfo'] = $data; - } else { - $params['message'] = $message; - $params['param'] = $data; - } - if (!empty($exPageInfo)) { - $params['exPageInfo'] = $exPageInfo; - } - $params['count'] = $count; - if (is_numeric($data) || !is_numeric($count)) { - $params['count'] = $data; - $params['exPageInfo'] = $count; - } - if ((int)$params['count'] == -100) { - $params['count'] = 1; - } - return static::encode($params); - } - - - /** - * @param Throwable|Error $throwable - * @return bool|string - */ - public static function error(Throwable|Error $throwable): bool|string - { - $array['code'] = $throwable->getCode() == 0 ? 500 : $throwable->getCode(); - $array['message'] = $throwable->getMessage(); - $array['param'] = [ - 'file' => $throwable->getFile(), - 'line' => $throwable->getLine() - ]; - return Json::encode($array); - } - - - /** - * @param $state - * @param $body - * @return false|int|string - * @throws Exception - */ - public static function output($state, $body): bool|int|string - { - $params['state'] = $state; - $params['body'] = ArrayAccess::toArray($body); - - return static::encode($params); - } + + + /** + * @param $data + * @return false|string + */ + public static function encode($data): bool|string + { + if (empty($data)) { + return false; + } + if (is_array($data)) { + return json_encode($data, JSON_UNESCAPED_UNICODE); + } + return $data; + } + + + /** + * @param $data + * @param bool $asArray + * @return mixed + */ + public static function decode($data, bool $asArray = true): mixed + { + if (is_array($data) || is_numeric($data)) { + return $data; + } + if (!is_string($data)) return null; + return json_decode($data, $asArray); + } + + + /** + * @param string $message + * @param int $code + * @param array|string $data + * @param int $count + * @return string + */ + public static function jsonFail(string $message, int $code = 500, array|string $data = [], int $count = 0): string + { + return json_encode(['code' => $code, 'param' => $data, 'message' => $message, 'count' => $count], JSON_UNESCAPED_UNICODE); + } + + + /** + * @param string $message + * @param array|string $data + * @param int $count + * @return string + */ + public static function jsonSuccess(array|string $data = [], string $message = "ok", int $count = 0): string + { + return json_encode(['code' => 0, 'param' => $data, 'message' => $message, 'count' => $count], JSON_UNESCAPED_UNICODE); + } + + + /** + * @param $code + * @param string|array $message + * @param array|int $data + * @param int $count + * @param array $exPageInfo + * @return string|bool + */ + public static function to($code, string|array $message = '', array|int $data = [], int $count = 0, array $exPageInfo = []): string|bool + { + $params['code'] = $code; + if (!is_string($message)) { + $params['message'] = 'System success.'; + $params['param'] = $message; + $params['exPageInfo'] = $data; + } else { + $params['message'] = $message; + $params['param'] = $data; + } + if (!empty($exPageInfo)) { + $params['exPageInfo'] = $exPageInfo; + } + $params['count'] = $count; + if (is_numeric($data) || !is_numeric($count)) { + $params['count'] = $data; + $params['exPageInfo'] = $count; + } + if ((int)$params['count'] == -100) { + $params['count'] = 1; + } + return static::encode($params); + } + + + /** + * @param Throwable|Error $throwable + * @return bool|string + */ + public static function error(Throwable|Error $throwable): bool|string + { + $array['code'] = $throwable->getCode() == 0 ? 500 : $throwable->getCode(); + $array['message'] = $throwable->getMessage(); + $array['param'] = [ + 'file' => $throwable->getFile(), + 'line' => $throwable->getLine() + ]; + return Json::encode($array); + } + + + /** + * @param $state + * @param $body + * @return false|int|string + * @throws + */ + public static function output($state, $body): bool|int|string + { + $params['state'] = $state; + $params['body'] = ArrayAccess::toArray($body); + + return static::encode($params); + } } diff --git a/kiri-engine/Core/Str.php b/kiri-engine/Core/Str.php index 6e3ea2c8..48c43d79 100644 --- a/kiri-engine/Core/Str.php +++ b/kiri-engine/Core/Str.php @@ -14,9 +14,9 @@ use Exception; class Str { - const STRING = 'abcdefghijklmnopqrstuvwxyz'; + const string STRING = 'abcdefghijklmnopqrstuvwxyz'; - const NUMBER = '01234567890'; + const string NUMBER = '01234567890'; /** * @param int $length @@ -107,7 +107,7 @@ class Str */ public static function isSerialize($data, $callBack = NULL): bool { - $false = !empty($data) && swoole_unserialize($data) !== FALSE; + $false = !empty($data) && unserialize($data) !== FALSE; if ($false && is_callable($callBack, TRUE)) { return call_user_func($callBack, $data); } @@ -172,10 +172,10 @@ class Str public static function filename($file, $type): string { return match ($type) { - 'image/png' => md5_file($file) . '.png', + 'image/png' => md5_file($file) . '.png', 'image/jpeg', 'image/jpg' => md5_file($file) . '.jpg', - 'image/gif' => md5_file($file) . '.gif', - default => md5_file($file), + 'image/gif' => md5_file($file) . '.gif', + default => md5_file($file), }; } diff --git a/kiri-engine/Core/Xml.php b/kiri-engine/Core/Xml.php index 89e0ca64..5fcffe53 100644 --- a/kiri-engine/Core/Xml.php +++ b/kiri-engine/Core/Xml.php @@ -18,40 +18,40 @@ use Exception; class Xml { - /** - * @param $data - * @param bool $asArray - * @return array|object - * @throws Exception - */ - public static function toArray($data, bool $asArray = true): object|array - { - $data = \simplexml_load_string($data, 'SimpleXMLElement', LIBXML_NOCDATA); - if ($data === false) { - throw new Exception('Parameter format error.'); - } - $array = \get_object_vars($data); - if (isset($array[0])) { - $array[$data->getName()] = $array[0]; - unset($array[0]); - } - return $array; - } + /** + * @param $data + * @param bool $asArray + * @return array|object + * @throws + */ + public static function toArray($data, bool $asArray = true): object|array + { + $data = \simplexml_load_string($data, 'SimpleXMLElement', LIBXML_NOCDATA); + if ($data === false) { + throw new Exception('Parameter format error.'); + } + $array = \get_object_vars($data); + if (isset($array[0])) { + $array[$data->getName()] = $array[0]; + unset($array[0]); + } + return $array; + } - /** - * @param $str - * @return array|bool|object - * @throws Exception - */ - public static function isXml($str): object|bool|array - { - $xml_parser = \xml_parser_create(); - if (!\xml_parse($xml_parser, $str, true)) { - \xml_parser_free($xml_parser); - return false; - } else { - return self::toArray($str); - } - } + /** + * @param $str + * @return array|bool|object + * @throws + */ + public static function isXml($str): object|bool|array + { + $xml_parser = \xml_parser_create(); + if (!\xml_parse($xml_parser, $str, true)) { + \xml_parser_free($xml_parser); + return false; + } else { + return self::toArray($str); + } + } } diff --git a/kiri-engine/Error/ErrorHandler.php b/kiri-engine/Error/ErrorHandler.php index cd6e4ccf..80e19ee4 100644 --- a/kiri-engine/Error/ErrorHandler.php +++ b/kiri-engine/Error/ErrorHandler.php @@ -92,7 +92,7 @@ class ErrorHandler extends Component implements ErrorInterface /** * @return void * @throws ReflectionException - * @throws Exception + * @throws */ public function shutdown(): void { @@ -108,7 +108,7 @@ class ErrorHandler extends Component implements ErrorInterface /** * @param Throwable $exception * - * @throws Exception + * @throws */ public function exceptionHandler(Throwable $exception): void { @@ -121,8 +121,7 @@ class ErrorHandler extends Component implements ErrorInterface /** - * @throws ErrorException - * @throws ReflectionException + * @throws */ public function errorHandler() { diff --git a/kiri-engine/Error/StdoutLogger.php b/kiri-engine/Error/StdoutLogger.php index 56fe1761..a8873773 100644 --- a/kiri-engine/Error/StdoutLogger.php +++ b/kiri-engine/Error/StdoutLogger.php @@ -99,7 +99,7 @@ class StdoutLogger extends Component * @param string $name * @param array $arguments * @return void - * @throws ReflectionException + * @throws */ public function __call(string $name, array $arguments) { diff --git a/kiri-engine/Redis/Redis.php b/kiri-engine/Redis/Redis.php index 0400fdf0..8689440f 100644 --- a/kiri-engine/Redis/Redis.php +++ b/kiri-engine/Redis/Redis.php @@ -58,7 +58,7 @@ class Redis /** * @return void - * @throws Exception + * @throws */ public function init(): void { @@ -85,7 +85,7 @@ class Redis * @param $key * @param int $timeout * @return bool - * @throws RedisException + * @throws */ public function waite($key, int $timeout = 5): bool { @@ -105,7 +105,7 @@ class Redis * @param $key * @param int $timeout * @return bool|int - * @throws Exception + * @throws */ public function lock($key, int $timeout = 5): bool|int { @@ -124,7 +124,7 @@ SCRIPT; /** * @param $key * @return int - * @throws Exception + * @throws */ public function unlock($key): int { @@ -165,7 +165,7 @@ SCRIPT; /** * @return \Redis - * @throws Exception + * @throws */ private function getClient(): \Redis { @@ -175,7 +175,7 @@ SCRIPT; /** * @return Pool - * @throws Exception + * @throws */ protected function pool(): Pool { @@ -189,7 +189,7 @@ SCRIPT; /** * @return \Redis - * @throws RedisConnectException|RedisException + * @throws */ protected function connect(): \Redis {