Skip to content

Commit

Permalink
code quality improvements
Browse files Browse the repository at this point in the history
Code quality improvements at level 4 of PHPStan (part I)
  • Loading branch information
deminy committed Dec 9, 2023
1 parent 49ed9a7 commit 0bad3d0
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 112 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down
2 changes: 1 addition & 1 deletion src/alias.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/alias_ns.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
87 changes: 25 additions & 62 deletions src/core/ArrayObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -476,62 +472,49 @@ 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() |
* | ksort() | key | yes | low to high | asort() |
* | 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() |
*/

/**
* @return $this
*/
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;
}

Expand Down Expand Up @@ -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;
}

Expand Down
54 changes: 17 additions & 37 deletions src/core/Coroutine/FastCGI/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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);
Expand All @@ -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
Expand Down
16 changes: 8 additions & 8 deletions src/core/Server/Admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ function ($server, $msg) {
$list[$key] = [
'id' => ++$key,
'name' => $extension,
'version' => $ext->getVersion() ?? '',
'version' => (string) $ext->getVersion(),
];
}
return self::json($list);
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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();
Expand All @@ -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 = [];
Expand Down
2 changes: 1 addition & 1 deletion src/core/Server/Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
3 changes: 1 addition & 2 deletions src/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -103,7 +103,6 @@ function swoole_table(int $size, string $fields): Swoole\Table
break;
default:
throw new RuntimeException("unknown field type[{$type}]");
break;
}
}

Expand Down

0 comments on commit 0bad3d0

Please sign in to comment.