From 0bad3d09855b18f5c12bb6ec64120dce52bf5c9e Mon Sep 17 00:00:00 2001 From: Demin Yin Date: Fri, 8 Dec 2023 23:48:37 -0800 Subject: [PATCH] code quality improvements Code quality improvements at level 4 of PHPStan (part I) --- CHANGELOG.md | 1 + src/alias.php | 2 +- src/alias_ns.php | 2 +- src/core/ArrayObject.php | 87 ++++++++------------------- src/core/Coroutine/FastCGI/Client.php | 54 ++++++----------- src/core/Server/Admin.php | 16 ++--- src/core/Server/Helper.php | 2 +- src/functions.php | 3 +- 8 files changed, 55 insertions(+), 112 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9ab34b7f..7fe1af64 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ Fixed: * Fix return type of method _\Swoole\FastCGI\HttpRequest::withBody()_. ([commit](https://github.com/swoole/library/commit/d204c4407357436a73157c454c471916b563ec63)) * Fix return value of method _\Swoole\Server\Admin::start()_. ([commit](https://github.com/swoole/library/commit/f211ae16cb3075b5977c52d7fd8f4896a8c51dc7)) * Fix method _\Swoole\MultibyteStringObject::ipos()_. ([commit](https://github.com/swoole/library/commit/3a543c1dc5f116f3fbd96c69b83413193f050086)) +* Fix incorrect operator precedence used in method _\Swoole\Coroutine\Admin::start()_. ([commit](https://github.com/swoole/library/commit/49ed9a7b7ad1678a602310c50149f0e46ec0927a)) Changed: diff --git a/src/alias.php b/src/alias.php index 306b8b26..0db2bc9d 100644 --- a/src/alias.php +++ b/src/alias.php @@ -9,7 +9,7 @@ declare(strict_types=1); -if (SWOOLE_USE_SHORTNAME) { +if (SWOOLE_USE_SHORTNAME) { // @phpstan-ignore if.alwaysTrue class_alias(Swoole\Coroutine\WaitGroup::class, Co\WaitGroup::class, true); class_alias(Swoole\Coroutine\Server::class, Co\Server::class, true); class_alias(Swoole\Coroutine\Server\Connection::class, Co\Server\Connection::class, true); diff --git a/src/alias_ns.php b/src/alias_ns.php index 47a4b69f..da6b9e1b 100644 --- a/src/alias_ns.php +++ b/src/alias_ns.php @@ -13,7 +13,7 @@ use Swoole\Coroutine; -if (SWOOLE_USE_SHORTNAME) { +if (SWOOLE_USE_SHORTNAME) { // @phpstan-ignore if.alwaysTrue function run(callable $fn, ...$args) { return \Swoole\Coroutine\run($fn, ...$args); diff --git a/src/core/ArrayObject.php b/src/core/ArrayObject.php index 1d9e46ff..38bb6d99 100644 --- a/src/core/ArrayObject.php +++ b/src/core/ArrayObject.php @@ -399,14 +399,10 @@ public function randomGet() return static::detectType($this->array[array_rand($this->array, 1)]); } - /** - * @return $this - */ public function each(callable $fn): self { - if (array_walk($this->array, $fn) === false) { - throw new \RuntimeException('array_walk() failed'); - } + array_walk($this->array, $fn); + return $this; } @@ -476,7 +472,7 @@ public function filter(callable $fn, int $flag = 0): static /** * | Function name | Sorts by | Maintains key association | Order of sort | Related functions | * | :---------------- | :------- | :-------------------------- | :-------------------------- | :---------------- | - * | array_multisort() | value | associative yes, numeric no | first array or sort options | array_walk() | + * | array_multisort() | value | associative yes, numeric no | first array or sort options | array_walk() | * | asort() | value | yes | low to high | arsort() | * | arsort() | value | yes | high to low | asort() | * | krsort() | key | yes | high to low | ksort() | @@ -484,11 +480,11 @@ public function filter(callable $fn, int $flag = 0): static * | natcasesort() | value | yes | natural, case insensitive | natsort() | * | natsort() | value | yes | natural | natcasesort() | * | rsort() | value | no | high to low | sort() | - * | shuffle() | value | no | random | array_rand() | + * | shuffle() | value | no | random | array_rand() | * | sort() | value | no | low to high | rsort() | - * | uasort() | value | yes | user defined | uksort() | - * | uksort() | key | yes | user defined | uasort() | - * | usort() | value | no | user defined | uasort() | + * | uasort() | value | yes | user defined | uksort() | + * | uksort() | key | yes | user defined | uasort() | + * | usort() | value | no | user defined | uasort() | */ /** @@ -496,42 +492,29 @@ public function filter(callable $fn, int $flag = 0): static */ public function asort(int $sort_flags = SORT_REGULAR): self { - if (asort($this->array, $sort_flags) !== true) { - throw new \RuntimeException('asort() failed'); - } + asort($this->array, $sort_flags); + return $this; } - /** - * @return $this - */ public function arsort(int $sort_flags = SORT_REGULAR): self { - if (arsort($this->array, $sort_flags) !== true) { - throw new \RuntimeException('arsort() failed'); - } + arsort($this->array, $sort_flags); + return $this; } - /** - * @return $this - */ public function krsort(int $sort_flags = SORT_REGULAR): self { - if (krsort($this->array, $sort_flags) !== true) { - throw new \RuntimeException('krsort() failed'); - } + krsort($this->array, $sort_flags); + return $this; } - /** - * @return $this - */ public function ksort(int $sort_flags = SORT_REGULAR): self { - if (ksort($this->array, $sort_flags) !== true) { - throw new \RuntimeException('ksort() failed'); - } + ksort($this->array, $sort_flags); + return $this; } @@ -568,58 +551,38 @@ public function rsort(int $sort_flags = SORT_REGULAR): self return $this; } - /** - * @return $this - */ public function shuffle(): self { - if (shuffle($this->array) !== true) { - throw new \RuntimeException('shuffle() failed'); - } + shuffle($this->array); + return $this; } - /** - * @return $this - */ public function sort(int $sort_flags = SORT_REGULAR): self { - if (sort($this->array, $sort_flags) !== true) { - throw new \RuntimeException('sort() failed'); - } + sort($this->array, $sort_flags); + return $this; } - /** - * @return $this - */ public function uasort(callable $value_compare_func): self { - if (uasort($this->array, $value_compare_func) !== true) { - throw new \RuntimeException('uasort() failed'); - } + uasort($this->array, $value_compare_func); + return $this; } - /** - * @return $this - */ public function uksort(callable $value_compare_func): self { - if (uksort($this->array, $value_compare_func) !== true) { - throw new \RuntimeException('uksort() failed'); - } + uksort($this->array, $value_compare_func); + return $this; } - /** - * @return $this - */ public function usort(callable $value_compare_func): self { - if (usort($this->array, $value_compare_func) !== true) { - throw new \RuntimeException('usort() failed'); - } + usort($this->array, $value_compare_func); + return $this; } diff --git a/src/core/Coroutine/FastCGI/Client.php b/src/core/Coroutine/FastCGI/Client.php index 72547e01..d50173ab 100644 --- a/src/core/Coroutine/FastCGI/Client.php +++ b/src/core/Coroutine/FastCGI/Client.php @@ -22,20 +22,15 @@ class Client { - /** @var int */ - protected $af; + protected int $af; - /** @var string */ - protected $host; + protected string $host; - /** @var int */ - protected $port; + protected int $port; - /** @var bool */ - protected $ssl; + protected bool $ssl; - /** @var Socket */ - protected $socket; + protected ?Socket $socket; public function __construct(string $host, int $port = 0, bool $ssl = false) { @@ -77,33 +72,17 @@ public function execute(Request $request, float $timeout = -1): Response } $records = []; while (true) { - if (SWOOLE_VERSION_ID < 40500) { - $recvData = ''; - while (true) { - $tmp = $socket->recv(8192, $timeout); - if (!$tmp) { - if ($tmp === '') { - $this->ioException(SOCKET_ECONNRESET); - } - $this->ioException(); - } - $recvData .= $tmp; - if (FrameParser::hasFrame($recvData)) { - break; - } - } - } else { - $recvData = $socket->recvPacket($timeout); - if (!$recvData) { - if ($recvData === '') { - $this->ioException(SOCKET_ECONNRESET); - } - $this->ioException(); - } - if (!FrameParser::hasFrame($recvData)) { - $this->ioException(SOCKET_EPROTO); + $recvData = $socket->recvPacket($timeout); + if (!$recvData) { + if ($recvData === '') { + $this->ioException(SOCKET_ECONNRESET); } + $this->ioException(); } + if (!FrameParser::hasFrame($recvData)) { + $this->ioException(SOCKET_EPROTO); + } + do { $records[] = $record = FrameParser::parseFrame($recvData); } while (strlen($recvData) !== 0); @@ -118,8 +97,9 @@ public function execute(Request $request, float $timeout = -1): Response }; } } - /* never here */ - exit(1); + + // Code execution should never reach here. However, we still put an exit() statement here for safe purpose. + exit(1); // @phpstan-ignore deadCode.unreachable } public static function parseUrl(string $url): array diff --git a/src/core/Server/Admin.php b/src/core/Server/Admin.php index a287c168..0361b8f8 100644 --- a/src/core/Server/Admin.php +++ b/src/core/Server/Admin.php @@ -242,7 +242,7 @@ function ($server, $msg) { $list[$key] = [ 'id' => ++$key, 'name' => $extension, - 'version' => $ext->getVersion() ?? '', + 'version' => (string) $ext->getVersion(), ]; } return self::json($list); @@ -785,7 +785,7 @@ public static function handlerGetFunctionInfo($server, $msg) $result = [ 'filename' => $ref->getFileName(), - 'line' => $ref->getStartLine() ?? '', + 'line' => $ref->getStartLine() ?: '', 'num' => $ref->getNumberOfParameters(), 'user_defined' => $ref->isUserDefined(), 'extension' => $ref->getExtensionName(), @@ -905,13 +905,13 @@ public static function handlerGetVersionInfo($server, $msg) return self::json($data); } - public static function handlerGetDefinedFunctions($server, $msg) + public static function handlerGetDefinedFunctions() { $functions = get_defined_functions(); - $arr = []; - if ($functions) { - $arr['internal'] = $functions['internal']; - + $arr = [ + 'internal' => $functions['internal'], + ]; + if (!empty($functions['user'])) { foreach ($functions['user'] as $function_name) { $function = new \ReflectionFunction($function_name); $filename = $function->getFileName(); @@ -926,7 +926,7 @@ public static function handlerGetDefinedFunctions($server, $msg) return self::json($arr); } - public static function handlerGetDeclaredClasses($server, $msg) + public static function handlerGetDeclaredClasses() { $classes = get_declared_classes(); $arr = []; diff --git a/src/core/Server/Helper.php b/src/core/Server/Helper.php index e35b11da..a7e45273 100644 --- a/src/core/Server/Helper.php +++ b/src/core/Server/Helper.php @@ -224,7 +224,7 @@ public static function onBeforeStart(Server $server) public static function onBeforeShutdown(Server $server) { - if ($server->admin_server) { + if ($server->admin_server) { // @phpstan-ignore if.alwaysTrue $server->admin_server->shutdown(); $server->admin_server = null; } diff --git a/src/functions.php b/src/functions.php index 496bf5c8..c726e720 100644 --- a/src/functions.php +++ b/src/functions.php @@ -13,7 +13,7 @@ throw new RuntimeException('require PHP version 7.2 or later'); } -if (SWOOLE_USE_SHORTNAME) { +if (SWOOLE_USE_SHORTNAME) { // @phpstan-ignore if.alwaysTrue function _string(string $string = ''): Swoole\StringObject { return new Swoole\StringObject($string); @@ -103,7 +103,6 @@ function swoole_table(int $size, string $fields): Swoole\Table break; default: throw new RuntimeException("unknown field type[{$type}]"); - break; } }