From c2c5efa88b29cc62bfc7756dfab54d3bcc5d630f Mon Sep 17 00:00:00 2001 From: Leo Cavalcante Date: Wed, 12 Jul 2023 03:32:35 -0300 Subject: [PATCH] Upgrade to Hyperf 3 (#62) --- composer.json | 30 +++++------ src/Config/DomainConfig.php | 11 ++-- src/ConfigProvider.php | 4 +- src/GoTaskConnection.php | 20 +++---- src/GoTaskConnectionPool.php | 2 + src/GoTaskFactory.php | 2 +- src/GoTaskProxy.php | 18 ++----- src/IPC/IPCSenderInterface.php | 4 +- src/IPC/PipeIPCSender.php | 13 ++--- src/IPC/SocketIPCReceiver.php | 36 ++++--------- src/IPC/SocketIPCSender.php | 9 ++-- src/Listener/CommandListener.php | 22 ++------ src/Listener/Go2PhpListener.php | 27 +++------- src/Listener/LogRedirectListener.php | 22 ++------ src/Listener/PipeLockListener.php | 11 ++-- src/MongoClient/Collection.php | 63 +++++++---------------- src/MongoClient/Database.php | 40 ++++---------- src/MongoClient/MongoClient.php | 27 +++------- src/MongoClient/MongoTrait.php | 4 +- src/MongoClient/Type/BulkWriteResult.php | 27 +++------- src/MongoClient/Type/DeleteResult.php | 7 +-- src/MongoClient/Type/IndexInfo.php | 51 ++++++------------ src/MongoClient/Type/InsertManyResult.php | 4 +- src/MongoClient/Type/InsertOneResult.php | 10 +--- src/MongoClient/Type/UpdateResult.php | 22 ++------ src/PipeGoTask.php | 32 ++++-------- src/Process/GoTaskProcess.php | 3 -- src/Relay/ConnectionRelay.php | 13 ++--- src/Relay/CoroutineSocketRelay.php | 31 +++-------- src/Relay/ProcessPipeRelay.php | 18 ++----- src/Relay/SocketTransporter.php | 12 ++--- src/SocketGoTask.php | 18 +++---- src/SocketIPCFactory.php | 15 +++--- src/Wrapper/ByteWrapper.php | 8 ++- src/Wrapper/ConfigWrapper.php | 17 +++--- src/Wrapper/LoggerWrapper.php | 13 ++--- tests/Cases/AbstractTestCase.php | 5 +- tests/Cases/CoroutineSocketTest.php | 10 ++-- tests/Cases/MongoDBTest.php | 12 ++--- tests/Cases/ProcessPipeTest.php | 10 ++-- tests/Cases/SocketIPCReceiverTest.php | 6 ++- tests/TestServer.php | 6 +-- 42 files changed, 222 insertions(+), 493 deletions(-) diff --git a/composer.json b/composer.json index e5f205a..6f708a6 100644 --- a/composer.json +++ b/composer.json @@ -18,23 +18,23 @@ } }, "require": { - "php": ">=7.4", - "ext-swoole": ">=4.4", - "hyperf/pool": "^2.2|^3.0", - "hyperf/process": "^2.2|^3.0", - "spiral/goridge": "^2.4.1", - "symfony/event-dispatcher": "^5.1" + "php": ">=8.1", + "ext-swoole": ">=5.0", + "hyperf/pool": "^3.0", + "hyperf/process": "^3.0", + "spiral/goridge": "^2.4", + "symfony/event-dispatcher": "^6.3" }, "require-dev": { - "friendsofphp/php-cs-fixer": "^3.0", - "hyperf/command": "^2.2|^3.0", - "hyperf/config": "^2.2|^3.0", - "hyperf/di": "^2.2|^3.0", - "hyperf/framework": "^2.2|^3.0", - "hyperf/testing": "^2.2|^3.0", - "mockery/mockery": "^1.3", - "phpstan/phpstan": "^1.0", - "swoole/ide-helper": "^4.5" + "friendsofphp/php-cs-fixer": "^3.21", + "hyperf/command": "^3.0", + "hyperf/config": "^3.0", + "hyperf/di": "^3.0", + "hyperf/framework": "^3.0", + "hyperf/testing": "^3.0", + "mockery/mockery": "^1.6", + "phpstan/phpstan": "^1.10", + "swoole/ide-helper": "^5.0" }, "config": { "sort-packages": true diff --git a/src/Config/DomainConfig.php b/src/Config/DomainConfig.php index f0bb170..26b268e 100644 --- a/src/Config/DomainConfig.php +++ b/src/Config/DomainConfig.php @@ -15,14 +15,9 @@ class DomainConfig { - /** - * @var ConfigInterface - */ - private $config; - - public function __construct(ConfigInterface $config) - { - $this->config = $config; + public function __construct( + private ConfigInterface $config + ) { } public function getProcessName(): string diff --git a/src/ConfigProvider.php b/src/ConfigProvider.php index a15dbbc..455e8e9 100644 --- a/src/ConfigProvider.php +++ b/src/ConfigProvider.php @@ -17,6 +17,8 @@ use Hyperf\GoTask\Listener\PipeLockListener; use Hyperf\GoTask\Process\GoTaskProcess; +use function Hyperf\Support\env; + class ConfigProvider { public function __invoke(): array @@ -69,7 +71,7 @@ public function __invoke(): array ]; } - public static function address() + public static function address(): string { if (defined('BASE_PATH')) { $root = BASE_PATH . '/runtime'; diff --git a/src/GoTaskConnection.php b/src/GoTaskConnection.php index 2e92436..b324e5e 100644 --- a/src/GoTaskConnection.php +++ b/src/GoTaskConnection.php @@ -13,6 +13,7 @@ use Hyperf\Contract\ConnectionInterface; use Hyperf\Contract\StdoutLoggerInterface; +use Hyperf\GoTask\IPC\SocketIPCSender; use Hyperf\Pool\Connection; use Hyperf\Pool\Exception\ConnectionException; use Hyperf\Pool\Pool; @@ -26,24 +27,15 @@ */ class GoTaskConnection extends Connection implements ConnectionInterface { - /** - * @var RPC - */ - private $connection; + private SocketIPCSender $connection; - /** - * @var SocketIPCFactory - */ - private $factory; - - public function __construct(ContainerInterface $container, Pool $pool, SocketIPCFactory $factory) + public function __construct(ContainerInterface $container, Pool $pool, private SocketIPCFactory $factory) { parent::__construct($container, $pool); - $this->factory = $factory; $this->reconnect(); } - public function __call($name, $arguments) + public function __call(string $name, array $arguments): mixed { try { $result = $this->connection->{$name}(...$arguments); @@ -67,7 +59,7 @@ public function reconnect(): bool return true; } - public function getActiveConnection() + public function getActiveConnection(): self { if ($this->check()) { return $this; @@ -80,7 +72,7 @@ public function getActiveConnection() return $this; } - protected function retry($name, $arguments, Throwable $exception) + protected function retry($name, $arguments, Throwable $exception): mixed { $logger = $this->container->get(StdoutLoggerInterface::class); $logger->warning(sprintf('RemoteGoTask::__call failed, because ' . $exception->getMessage())); diff --git a/src/GoTaskConnectionPool.php b/src/GoTaskConnectionPool.php index bea484f..18aa887 100644 --- a/src/GoTaskConnectionPool.php +++ b/src/GoTaskConnectionPool.php @@ -17,6 +17,8 @@ use Hyperf\Pool\Pool; use Psr\Container\ContainerInterface; +use function Hyperf\Support\make; + class GoTaskConnectionPool extends Pool { public function __construct(ContainerInterface $container, DomainConfig $config) diff --git a/src/GoTaskFactory.php b/src/GoTaskFactory.php index 4daa250..8e7ca61 100644 --- a/src/GoTaskFactory.php +++ b/src/GoTaskFactory.php @@ -16,7 +16,7 @@ class GoTaskFactory { - public function __invoke(ContainerInterface $container) + public function __invoke(ContainerInterface $container): GoTask { $config = $container->get(DomainConfig::class); if ($config->getAddress()) { diff --git a/src/GoTaskProxy.php b/src/GoTaskProxy.php index e317887..118f48b 100644 --- a/src/GoTaskProxy.php +++ b/src/GoTaskProxy.php @@ -13,17 +13,12 @@ class GoTaskProxy implements GoTask { - /** - * @var GoTask - */ - private $goTask; - - public function __construct(GoTask $goTask) - { - $this->goTask = $goTask; + public function __construct( + private GoTask $goTask + ) { } - public function __call($name, $arguments) + public function __call(string $name, array $arguments): mixed { $method = ucfirst($name); $path = explode('\\', static::class); @@ -31,10 +26,7 @@ public function __call($name, $arguments) return $this->call($class . '.' . $method, ...$arguments); } - /** - * {@inheritdoc} - */ - public function call(string $method, $payload, int $flags = 0) + public function call(string $method, mixed $payload, int $flags = 0): mixed { return $this->goTask->call($method, $payload, $flags); } diff --git a/src/IPC/IPCSenderInterface.php b/src/IPC/IPCSenderInterface.php index ebfbb51..ccf76be 100644 --- a/src/IPC/IPCSenderInterface.php +++ b/src/IPC/IPCSenderInterface.php @@ -25,8 +25,6 @@ interface IPCSenderInterface /** * @param mixed $payload an binary data or array of arguments for complex types * @param int $flags payload control flags - * - * @return mixed */ - public function call(string $method, $payload, int $flags = 0); + public function call(string $method, mixed $payload, int $flags = 0): mixed; } diff --git a/src/IPC/PipeIPCSender.php b/src/IPC/PipeIPCSender.php index f7c6933..92a2f39 100644 --- a/src/IPC/PipeIPCSender.php +++ b/src/IPC/PipeIPCSender.php @@ -14,6 +14,7 @@ use Hyperf\GoTask\GoTask; use Hyperf\GoTask\Relay\ProcessPipeRelay; use Spiral\Goridge\RPC; +use Swoole\Process; /** * Class PipeIPC uses pipes to communicate. @@ -21,29 +22,25 @@ */ class PipeIPCSender implements IPCSenderInterface, GoTask { - /** - * @var RPC - */ - private $handler; + private RPC $handler; /** * PipeIPC constructor. * @mixin RPC - * @param mixed $process */ - public function __construct($process) + public function __construct(Process $process) { $this->handler = new RPC( new ProcessPipeRelay($process) ); } - public function __call($name, $arguments) + public function __call(string $name, array $arguments): void { $this->handler->{$name}(...$arguments); } - public function call(string $method, $payload, int $flags = 0) + public function call(string $method, $payload, int $flags = 0): mixed { return $this->handler->call($method, $payload, $flags); } diff --git a/src/IPC/SocketIPCReceiver.php b/src/IPC/SocketIPCReceiver.php index 597d6f6..3bd14b0 100644 --- a/src/IPC/SocketIPCReceiver.php +++ b/src/IPC/SocketIPCReceiver.php @@ -11,39 +11,28 @@ */ namespace Hyperf\GoTask\IPC; +use Hyperf\Context\ApplicationContext; use Hyperf\ExceptionHandler\Formatter\FormatterInterface; use Hyperf\GoTask\GoTask; use Hyperf\GoTask\Relay\ConnectionRelay; use Hyperf\GoTask\Wrapper\ByteWrapper; -use Hyperf\Utils\ApplicationContext; use Spiral\Goridge\Exceptions\PrefixException; use Spiral\Goridge\Exceptions\ServiceException; use Spiral\Goridge\Exceptions\TransportException; use Spiral\Goridge\RelayInterface as Relay; +use Swoole\Coroutine\Server; use Swoole\Coroutine\Server\Connection; use Throwable; class SocketIPCReceiver { - /** - * @var string - */ - private $address; + private string $address; - /** - * @var \Swoole\Coroutine\Server - */ - private $server; + private ?Server $server = null; - /** - * @var int - */ - private $port; + private int $port; - /** - * @var bool - */ - private $quit; + private bool $quit; public function __construct(string $address = '127.0.0.1:6001') { @@ -114,7 +103,7 @@ public function start(): bool return true; } - public function close() + public function close(): void { if ($this->server !== null) { $this->quit = true; @@ -123,7 +112,7 @@ public function close() $this->server = null; } - protected function dispatch($method, $payload) + protected function dispatch(string $method, mixed $payload): mixed { [$class, $handler] = explode('::', $method); if (ApplicationContext::hasContainer()) { @@ -135,7 +124,7 @@ protected function dispatch($method, $payload) return $instance->{$handler}($payload); } - protected function isStarted() + protected function isStarted(): bool { return $this->server !== null; } @@ -143,12 +132,9 @@ protected function isStarted() /** * Handle response body. * - * @param string $body - * - * @return mixed * @throws ServiceException */ - protected function handleBody($body, int $flags) + protected function handleBody(string $body, int $flags): mixed { if ($flags & GoTask::PAYLOAD_ERROR && $flags & GoTask::PAYLOAD_RAW) { throw new ServiceException("error '{$body}' on '{$this->server}'"); @@ -161,7 +147,7 @@ protected function handleBody($body, int $flags) return json_decode($body, true); } - private function formatError(Throwable $error) + private function formatError(Throwable $error): string { $simpleFormat = $error->getMessage() . ':' . $error->getTraceAsString(); if (! ApplicationContext::hasContainer()) { diff --git a/src/IPC/SocketIPCSender.php b/src/IPC/SocketIPCSender.php index 810d013..eca2f4b 100644 --- a/src/IPC/SocketIPCSender.php +++ b/src/IPC/SocketIPCSender.php @@ -21,10 +21,7 @@ */ class SocketIPCSender implements IPCSenderInterface, GoTask { - /** - * @var RPC - */ - private $handler; + private RPC $handler; /** * PipeIPC constructor. @@ -45,12 +42,12 @@ public function __construct(string $address = '127.0.0.1:6001') ); } - public function __call($name, $arguments) + public function __call($name, $arguments): void { $this->handler->{$name}(...$arguments); } - public function call(string $method, $payload, int $flags = 0) + public function call(string $method, $payload, int $flags = 0): mixed { return $this->handler->call($method, $payload, $flags); } diff --git a/src/Listener/CommandListener.php b/src/Listener/CommandListener.php index 0cb69cf..e59b541 100644 --- a/src/Listener/CommandListener.php +++ b/src/Listener/CommandListener.php @@ -19,24 +19,13 @@ class CommandListener implements ListenerInterface { - /** - * @var Process - */ - private $process; + private Process $process; - /** - * @var DomainConfig - */ - private $config; - - public function __construct(DomainConfig $config) - { - $this->config = $config; + public function __construct( + private DomainConfig $config + ) { } - /** - * {@inheritdoc} - */ public function listen(): array { return [ @@ -44,9 +33,6 @@ public function listen(): array ]; } - /** - * {@inheritdoc} - */ public function process(object $event): void { if (! $this->config->isEnabled()) { diff --git a/src/Listener/Go2PhpListener.php b/src/Listener/Go2PhpListener.php index 179d0c0..d893b88 100644 --- a/src/Listener/Go2PhpListener.php +++ b/src/Listener/Go2PhpListener.php @@ -17,37 +17,22 @@ use Hyperf\GoTask\Config\DomainConfig; use Hyperf\GoTask\IPC\SocketIPCReceiver; use Hyperf\GoTask\WithGoTask; -use Psr\Container\ContainerInterface; + +use function Hyperf\Coroutine\go; +use function Hyperf\Support\make; class Go2PhpListener implements ListenerInterface { - /** - * @var DomainConfig - */ - private $config; - - /** - * @var ContainerInterface - */ - private $container; - - public function __construct(ContainerInterface $container) - { - $this->container = $container; - $this->config = $container->get(DomainConfig::class); + public function __construct( + private DomainConfig $config + ) { } - /** - * {@inheritdoc} - */ public function listen(): array { return [AfterWorkerStart::class, BeforeHandle::class]; } - /** - * {@inheritdoc} - */ public function process(object $event): void { if (! $this->config->shouldEnableGo2Php()) { diff --git a/src/Listener/LogRedirectListener.php b/src/Listener/LogRedirectListener.php index d77558a..36a416d 100644 --- a/src/Listener/LogRedirectListener.php +++ b/src/Listener/LogRedirectListener.php @@ -24,33 +24,19 @@ class LogRedirectListener implements ListenerInterface { - /** - * @var ContainerInterface - */ - private $container; + private DomainConfig $config; - /** - * @var DomainConfig - */ - private $config; - - public function __construct(ContainerInterface $container) - { - $this->container = $container; + public function __construct( + private ContainerInterface $container + ) { $this->config = $container->get(DomainConfig::class); } - /** - * {@inheritdoc} - */ public function listen(): array { return [MainWorkerStart::class]; } - /** - * {@inheritdoc} - */ public function process(object $event): void { if (! $this->config->shouldLogRedirect()) { diff --git a/src/Listener/PipeLockListener.php b/src/Listener/PipeLockListener.php index ad3dd5e..0811d40 100644 --- a/src/Listener/PipeLockListener.php +++ b/src/Listener/PipeLockListener.php @@ -18,14 +18,9 @@ class PipeLockListener implements ListenerInterface { - /** - * @var ContainerInterface - */ - private $container; - - public function __construct(ContainerInterface $container) - { - $this->container = $container; + public function __construct( + private ContainerInterface $container + ) { } /** diff --git a/src/MongoClient/Collection.php b/src/MongoClient/Collection.php index 20c8e11..c506d96 100644 --- a/src/MongoClient/Collection.php +++ b/src/MongoClient/Collection.php @@ -26,38 +26,13 @@ class Collection { use MongoTrait; - /** - * @var string - */ - protected $database; - - /** - * @var string - */ - protected $collection; - - /** - * @var MongoProxy - */ - private $mongo; - - /** - * @var ConfigInterface - */ - private $config; - - /** - * @var array - */ - private $typeMap; - - public function __construct(MongoProxy $mongo, ConfigInterface $config, string $database, string $collection, array $typeMap) - { - $this->mongo = $mongo; - $this->config = $config; - $this->database = $database; - $this->collection = $collection; - $this->typeMap = $typeMap; + public function __construct( + private MongoProxy $mongo, + private ConfigInterface $config, + protected string $database, + protected string $collection, + private array $typeMap, + ) { } public function insertOne($document = [], array $opts = []): InsertOneResult @@ -78,7 +53,7 @@ public function insertMany($documents = [], array $opts = []): InsertManyResult return toPHP($data, ['root' => InsertManyResult::class]); } - public function find($filter = [], array $opts = []) + public function find($filter = [], array $opts = []): array|object { $filter = $this->sanitize($filter); $data = $this->mongo->find($this->makePayload([ @@ -88,7 +63,7 @@ public function find($filter = [], array $opts = []) return $data !== '' ? toPHP($data, $typeMap) : []; } - public function findOne($filter = [], array $opts = []) + public function findOne($filter = [], array $opts = []): array|object { $filter = $this->sanitize($filter); $data = $this->mongo->findOne($this->makePayload([ @@ -98,7 +73,7 @@ public function findOne($filter = [], array $opts = []) return $data !== '' ? toPHP($data, $typeMap) : []; } - public function findOneAndDelete($filter = [], array $opts = []) + public function findOneAndDelete($filter = [], array $opts = []): array|object { $filter = $this->sanitize($filter); $data = $this->mongo->findOneAndDelete($this->makePayload([ @@ -108,7 +83,7 @@ public function findOneAndDelete($filter = [], array $opts = []) return $data !== '' ? toPHP($data, $typeMap) : []; } - public function findOneAndUpdate($filter = [], $update = [], array $opts = []) + public function findOneAndUpdate($filter = [], $update = [], array $opts = []): array|object { $filter = $this->sanitize($filter); $data = $this->mongo->findOneAndUpdate($this->makePayload([ @@ -119,7 +94,7 @@ public function findOneAndUpdate($filter = [], $update = [], array $opts = []) return $data !== '' ? toPHP($data, $typeMap) : []; } - public function findOneAndReplace($filter = [], $replace = [], array $opts = []) + public function findOneAndReplace($filter = [], $replace = [], array $opts = []): array|object { $filter = $this->sanitize($filter); $data = $this->mongo->findOneAndReplace($this->makePayload([ @@ -190,7 +165,7 @@ public function deleteMany($filter = [], array $opts = []): DeleteResult return toPHP($data, ['root' => DeleteResult::class]); } - public function aggregate($pipeline = [], array $opts = []) + public function aggregate($pipeline = [], array $opts = []): array|object { $pipeline = $this->sanitize($pipeline); $data = $this->mongo->aggregate($this->makePayload([ @@ -209,7 +184,7 @@ public function bulkWrite($operations = [], array $opts = []): BulkWriteResult return toPHP($data, ['root' => BulkWriteResult::class]); } - public function distinct(string $fieldName, $filter = [], array $opts = []) + public function distinct(string $fieldName, $filter = [], array $opts = []): array|object { $filter = $this->sanitize($filter); $data = $this->mongo->distinct($this->makePayload([ @@ -228,7 +203,7 @@ public function createIndex($index = [], array $opts = []): string ], $opts)); } - public function createIndexes($indexes = [], array $opts = []): array + public function createIndexes($indexes = [], array $opts = []): array|object { $indexes = $this->sanitize($indexes); $data = $this->mongo->createIndexes($this->makePayload([ @@ -237,13 +212,13 @@ public function createIndexes($indexes = [], array $opts = []): array return $data === '' ? [] : toPHP($data, ['root' => 'array']); } - public function listIndexes($indexes = [], array $opts = []): array + public function listIndexes($indexes = [], array $opts = []): array|object { $data = $this->mongo->listIndexes($this->makePayload([], $opts)); return $data === '' ? [] : toPHP($data, ['root' => 'array', 'document' => IndexInfo::class, 'fieldPaths' => ['$.key' => 'array']]); } - public function dropIndex(string $name, array $opts = []) + public function dropIndex(string $name, array $opts = []): array|object { $data = $this->mongo->dropIndex($this->makePayload([ 'Name' => $name, @@ -252,7 +227,7 @@ public function dropIndex(string $name, array $opts = []) return $data === '' ? [] : toPHP($data, $typeMap); } - public function dropIndexes(array $opts = []) + public function dropIndexes(array $opts = []): array|object { $data = $this->mongo->dropIndexes($this->makePayload([ ], $opts)); @@ -260,7 +235,7 @@ public function dropIndexes(array $opts = []) return $data === '' ? [] : toPHP($data, $typeMap); } - public function drop() + public function drop(): string { return $this->mongo->drop(fromPHP([ 'Database' => $this->database, diff --git a/src/MongoClient/Database.php b/src/MongoClient/Database.php index ceec4ac..68a62f1 100644 --- a/src/MongoClient/Database.php +++ b/src/MongoClient/Database.php @@ -20,45 +20,25 @@ class Database { use MongoTrait; - /** - * @var string - */ - protected $database; - - /** - * @var MongoProxy - */ - private $mongo; - - /** - * @var ConfigInterface - */ - private $config; - - /** - * @var array - */ - private $typeMap; - - public function __construct(MongoProxy $mongo, ConfigInterface $config, string $database, array $typeMap) - { - $this->mongo = $mongo; - $this->config = $config; - $this->database = $database; - $this->typeMap = $typeMap; + public function __construct( + private MongoProxy $mongo, + private ConfigInterface $config, + protected string $database, + private array $typeMap, + ) { } - public function __get($collName) + public function __get(string $collName): Collection { return new Collection($this->mongo, $this->config, $this->database, $collName, $this->typeMap); } - public function collection($collName) + public function collection(string $collName): Collection { return new Collection($this->mongo, $this->config, $this->database, $collName, $this->typeMap); } - public function runCommand($command = [], $opts = []) + public function runCommand(array $command = [], array $opts = []): array|object|string { $payload = [ 'Database' => $this->database, @@ -73,7 +53,7 @@ public function runCommand($command = [], $opts = []) return ''; } - public function runCommandCursor($command = [], $opts = []) + public function runCommandCursor(array $command = [], array $opts = []): array|object|string { $payload = [ 'Database' => $this->database, diff --git a/src/MongoClient/MongoClient.php b/src/MongoClient/MongoClient.php index a9ec60f..012d223 100644 --- a/src/MongoClient/MongoClient.php +++ b/src/MongoClient/MongoClient.php @@ -17,34 +17,21 @@ class MongoClient { use MongoTrait; - /** - * @var MongoProxy - */ - private $mongo; + private array $typeMap; - /** - * @var ConfigInterface - */ - private $config; - - /** - * @var array - */ - private $typeMap; - - public function __construct(MongoProxy $mongo, ConfigInterface $config) - { - $this->mongo = $mongo; - $this->config = $config; + public function __construct( + private MongoProxy $mongo, + private ConfigInterface $config, + ) { $this->typeMap = $this->config->get('mongodb.type_map', ['document' => 'array', 'root' => 'array']); } - public function __get($dbName) + public function __get(string $dbName): Database { return new Database($this->mongo, $this->config, $dbName, $this->typeMap); } - public function database(string $dbName) + public function database(string $dbName): Database { return new Database($this->mongo, $this->config, $dbName, $this->typeMap); } diff --git a/src/MongoClient/MongoTrait.php b/src/MongoClient/MongoTrait.php index f71a9d2..c464d04 100644 --- a/src/MongoClient/MongoTrait.php +++ b/src/MongoClient/MongoTrait.php @@ -15,12 +15,12 @@ trait MongoTrait { - private function sanitize($input) + private function sanitize(mixed $input): mixed { return $input ?: new stdClass(); } - private function sanitizeOpts($opts) + private function sanitizeOpts(mixed $opts): mixed { return $this->sanitize($opts); } diff --git a/src/MongoClient/Type/BulkWriteResult.php b/src/MongoClient/Type/BulkWriteResult.php index cf70d24..5a56f08 100644 --- a/src/MongoClient/Type/BulkWriteResult.php +++ b/src/MongoClient/Type/BulkWriteResult.php @@ -16,35 +16,20 @@ class BulkWriteResult implements Unserializable { - /** - * @var int - */ - private $matchedCount; + private int $matchedCount; - /** - * @var int - */ - private $modifiedCount; + private int $modifiedCount; - /** - * @var int - */ - private $upsertedCount; + private int $upsertedCount; - /** - * @var int - */ - private $deletedCount; + private int $deletedCount; - /** - * @var int - */ - private $insertedCount; + private int $insertedCount; /** * @var array */ - private $upsertedIds; + private array $upsertedIds; public function bsonUnserialize(array $data): void { diff --git a/src/MongoClient/Type/DeleteResult.php b/src/MongoClient/Type/DeleteResult.php index 3a19488..2f3dfd1 100644 --- a/src/MongoClient/Type/DeleteResult.php +++ b/src/MongoClient/Type/DeleteResult.php @@ -15,12 +15,9 @@ class DeleteResult implements Unserializable { - /** - * @var int - */ - private $n; + private int $n; - public function bsonUnserialize(array $data) + public function bsonUnserialize(array $data): void { $this->n = $data['n']; } diff --git a/src/MongoClient/Type/IndexInfo.php b/src/MongoClient/Type/IndexInfo.php index 1f8846f..325c45c 100644 --- a/src/MongoClient/Type/IndexInfo.php +++ b/src/MongoClient/Type/IndexInfo.php @@ -15,42 +15,21 @@ class IndexInfo implements Unserializable { - /** - * @var int - */ - private $v; - - /** - * @var array - */ - private $key; - - /** - * @var string - */ - private $name; - - /** - * @var string - */ - private $ns; - - /** - * @var bool - */ - private $sparse; - - /** - * @var bool - */ - private $unique; - - /** - * @var bool - */ - private $ttl; - - public function bsonUnserialize(array $data) + private int $v; + + private array $key; + + private string $name; + + private string $ns; + + private bool $sparse; + + private bool $unique; + + private bool $ttl; + + public function bsonUnserialize(array $data): void { $this->v = $data['v'] ?? 0; $this->key = $data['key'] ?? []; diff --git a/src/MongoClient/Type/InsertManyResult.php b/src/MongoClient/Type/InsertManyResult.php index bc1c00b..c3a3400 100644 --- a/src/MongoClient/Type/InsertManyResult.php +++ b/src/MongoClient/Type/InsertManyResult.php @@ -19,9 +19,9 @@ class InsertManyResult implements Unserializable /** * @var array */ - private $insertedIDs; + private array $insertedIDs; - public function bsonUnserialize(array $data) + public function bsonUnserialize(array $data): void { $this->insertedIDs = $data['insertedids']; } diff --git a/src/MongoClient/Type/InsertOneResult.php b/src/MongoClient/Type/InsertOneResult.php index dc98b15..082ff25 100644 --- a/src/MongoClient/Type/InsertOneResult.php +++ b/src/MongoClient/Type/InsertOneResult.php @@ -15,19 +15,13 @@ class InsertOneResult implements \MongoDB\BSON\Unserializable { - /** - * @var null|ObjectId - */ - private $insertedId; + private ?ObjectId $insertedId; - public function bsonUnserialize(array $data) + public function bsonUnserialize(array $data): void { $this->insertedId = $data['insertedid']; } - /** - * @return ?string - */ public function getInsertedId(): ?ObjectId { return $this->insertedId; diff --git a/src/MongoClient/Type/UpdateResult.php b/src/MongoClient/Type/UpdateResult.php index b87c8a7..66e068b 100644 --- a/src/MongoClient/Type/UpdateResult.php +++ b/src/MongoClient/Type/UpdateResult.php @@ -15,27 +15,15 @@ class UpdateResult implements Unserializable { - /** - * @var int - */ - private $matchedCount; + private int $matchedCount; - /** - * @var int - */ - private $modifiedCount; + private int $modifiedCount; - /** - * @var int - */ - private $upsertedCount; + private int $upsertedCount; - /** - * @var null|string - */ - private $upsertedId; + private ?string $upsertedId; - public function bsonUnserialize(array $data) + public function bsonUnserialize(array $data): void { $this->matchedCount = $data['matchedcount']; $this->modifiedCount = $data['modifiedcount']; diff --git a/src/PipeGoTask.php b/src/PipeGoTask.php index 863933e..1e97fd1 100644 --- a/src/PipeGoTask.php +++ b/src/PipeGoTask.php @@ -19,6 +19,9 @@ use Swoole\Process; use Throwable; +use function Hyperf\Coroutine\go; +use function Hyperf\Support\make; + /** * Class PipeGoTask uses stdin/stdout pipes to communicate. * This class can be used as a singleton. @@ -26,31 +29,18 @@ */ class PipeGoTask implements GoTask { - /** - * @var Lock - */ - public $lock; - - private $taskChannel; + public Lock $lock; - /** - * @var null|Process - */ - private $process; + private ?Channel $taskChannel; - /** - * @var DomainConfig - */ - private $config; - - public function __construct(DomainConfig $config, ?Process $process = null) - { - $this->process = $process; - $this->config = $config; + public function __construct( + private DomainConfig $config, + private ?Process $process = null + ) { $this->lock = new Lock(); } - public function call(string $method, $payload, int $flags = 0) + public function call(string $method, mixed $payload, int $flags = 0): mixed { if ($this->taskChannel == null) { $this->taskChannel = new Channel(100); @@ -67,7 +57,7 @@ public function call(string $method, $payload, int $flags = 0) return $result; } - private function start() + private function start(): void { if ($this->process == null) { $processName = $this->config->getProcessName(); diff --git a/src/Process/GoTaskProcess.php b/src/Process/GoTaskProcess.php index 5aedcae..ed9d3e1 100644 --- a/src/Process/GoTaskProcess.php +++ b/src/Process/GoTaskProcess.php @@ -54,9 +54,6 @@ public function bind($server): void parent::bind($server); } - /** - * {@inheritdoc} - */ public function handle(): void { $executable = $this->config->getExecutable(); diff --git a/src/Relay/ConnectionRelay.php b/src/Relay/ConnectionRelay.php index 705a6d3..360a41a 100644 --- a/src/Relay/ConnectionRelay.php +++ b/src/Relay/ConnectionRelay.php @@ -20,16 +20,11 @@ class ConnectionRelay implements RelayInterface { use SocketTransporter; - private $conn; + private ?Socket $socket = null; - /** - * @var Socket - */ - private $socket; - - public function __construct(Connection $conn) - { - $this->conn = $conn; + public function __construct( + private Connection $conn + ) { } /** diff --git a/src/Relay/CoroutineSocketRelay.php b/src/Relay/CoroutineSocketRelay.php index 80a5877..fa9ca39 100644 --- a/src/Relay/CoroutineSocketRelay.php +++ b/src/Relay/CoroutineSocketRelay.php @@ -39,17 +39,7 @@ class CoroutineSocketRelay implements RelayInterface // @deprecated public const SOCK_TPC = self::SOCK_TCP; - /** @var string */ - private $address; - - /** @var null|int */ - private $port; - - /** @var int */ - private $type; - - /** @var Socket */ - private $socket; + private ?Socket $socket = null; /** * Example: @@ -62,8 +52,11 @@ class CoroutineSocketRelay implements RelayInterface * * @throws InvalidArgumentException */ - public function __construct(string $address, int $port = null, int $type = self::SOCK_TCP) - { + public function __construct( + private string $address, + private ?int $port = null, + private int $type = self::SOCK_TCP + ) { switch ($type) { case self::SOCK_TCP: if ($port === null) { @@ -83,10 +76,6 @@ public function __construct(string $address, int $port = null, int $type = self: $address )); } - - $this->address = $address; - $this->port = $port; - $this->type = $type; } public function __toString(): string @@ -103,10 +92,7 @@ public function getAddress(): string return $this->address; } - /** - * @return null|int - */ - public function getPort() + public function getPort(): ?int { return $this->port; } @@ -143,10 +129,9 @@ public function connect(): bool } /** - * @return Socket * @throws GoridgeException */ - private function createSocket() + private function createSocket(): Socket { if ($this->type === self::SOCK_UNIX) { if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') { diff --git a/src/Relay/ProcessPipeRelay.php b/src/Relay/ProcessPipeRelay.php index 8fcc7ab..08f247c 100644 --- a/src/Relay/ProcessPipeRelay.php +++ b/src/Relay/ProcessPipeRelay.php @@ -30,21 +30,14 @@ class ProcessPipeRelay implements RelayInterface { use SocketTransporter; - /** @var Socket */ - private $socket; - - /** - * @var Process - */ - private $process; + private ?Socket $socket = null; /** * export a socket from swoole process. */ - public function __construct(Process $process) - { - $this->process = $process; - $this->socket = null; + public function __construct( + private Process $process + ) { } public function __toString(): string @@ -70,10 +63,9 @@ public function connect(): bool } /** - * @return Socket * @throws GoridgeException */ - private function createSocket() + private function createSocket(): Socket { return $this->process->exportSocket(); } diff --git a/src/Relay/SocketTransporter.php b/src/Relay/SocketTransporter.php index c2db172..4a7ede9 100644 --- a/src/Relay/SocketTransporter.php +++ b/src/Relay/SocketTransporter.php @@ -27,10 +27,7 @@ public function __destruct() } } - /** - * {@inheritdoc} - */ - public function send($payload, int $flags = null) + public function send(?string $payload, int $flags = null) { $this->connect(); @@ -49,10 +46,7 @@ public function send($payload, int $flags = null) return $this; } - /** - * {@inheritdoc} - */ - public function receiveSync(int &$flags = null) + public function receiveSync(int &$flags = null): ?string { $this->connect(); @@ -84,7 +78,7 @@ public function isConnected(): bool * * @throws RelayException */ - public function close() + public function close(): void { if (! $this->isConnected()) { throw new RelayException("unable to close socket '{$this}', socket already closed"); diff --git a/src/SocketGoTask.php b/src/SocketGoTask.php index 259ab0c..12b030c 100644 --- a/src/SocketGoTask.php +++ b/src/SocketGoTask.php @@ -14,6 +14,8 @@ use Hyperf\Context\Context; use Hyperf\GoTask\Exception\InvalidGoTaskConnectionException; +use function Hyperf\Coroutine\defer; + /** * Class SocketGoTask uses sockets to communicate. * This class can be used as a singleton. @@ -21,17 +23,12 @@ */ class SocketGoTask implements GoTask { - /** - * @var GoTaskConnectionPool - */ - private $pool; - - public function __construct(GoTaskConnectionPool $pool) - { - $this->pool = $pool; + public function __construct( + private GoTaskConnectionPool $pool + ) { } - public function call(string $method, $payload, int $flags = 0) + public function call(string $method, mixed $payload, int $flags = 0): mixed { $hasContextConnection = Context::has($this->getContextKey()); $connection = $this->getConnection($hasContextConnection); @@ -53,9 +50,8 @@ public function call(string $method, $payload, int $flags = 0) /** * Get a connection from coroutine context, or from redis connectio pool. - * @param mixed $hasContextConnection */ - private function getConnection($hasContextConnection): GoTaskConnection + private function getConnection(mixed $hasContextConnection): GoTaskConnection { $connection = null; if ($hasContextConnection) { diff --git a/src/SocketIPCFactory.php b/src/SocketIPCFactory.php index 2b3d3b5..098b3b4 100644 --- a/src/SocketIPCFactory.php +++ b/src/SocketIPCFactory.php @@ -14,19 +14,16 @@ use Hyperf\GoTask\Config\DomainConfig; use Hyperf\GoTask\IPC\SocketIPCSender; +use function Hyperf\Support\make; + class SocketIPCFactory { - /** - * @var DomainConfig - */ - private $config; - - public function __construct(DomainConfig $config) - { - $this->config = $config; + public function __construct( + private DomainConfig $config + ) { } - public function make() + public function make(): SocketIPCSender { $address = $this->config->getAddress(); return make(SocketIPCSender::class, ['address' => $address]); diff --git a/src/Wrapper/ByteWrapper.php b/src/Wrapper/ByteWrapper.php index 4a3aa3b..99cca2e 100644 --- a/src/Wrapper/ByteWrapper.php +++ b/src/Wrapper/ByteWrapper.php @@ -13,10 +13,8 @@ class ByteWrapper { - public $byte; - - public function __construct(string $s) - { - $this->byte = $s; + public function __construct( + public string $byte + ) { } } diff --git a/src/Wrapper/ConfigWrapper.php b/src/Wrapper/ConfigWrapper.php index f2d3e0b..0b83205 100644 --- a/src/Wrapper/ConfigWrapper.php +++ b/src/Wrapper/ConfigWrapper.php @@ -15,27 +15,22 @@ class ConfigWrapper { - /** - * @var ConfigInterface - */ - private $config; - - public function __construct(ConfigInterface $config) - { - $this->config = $config; + public function __construct( + private ConfigInterface $config + ) { } - public function get($payload) + public function get(string $payload) { return $this->config->get($payload, null); } - public function has($payload) + public function has(string $payload): bool { return $this->config->has($payload); } - public function set($payload) + public function set(string $payload): ?mixed { $this->config->set($payload['key'], $payload['value']); return null; diff --git a/src/Wrapper/LoggerWrapper.php b/src/Wrapper/LoggerWrapper.php index fdf5f8c..c7132f6 100644 --- a/src/Wrapper/LoggerWrapper.php +++ b/src/Wrapper/LoggerWrapper.php @@ -15,17 +15,12 @@ class LoggerWrapper { - /** - * @var LoggerInterface - */ - private $logger; - - public function __construct(LoggerInterface $logger) - { - $this->logger = $logger; + public function __construct( + private LoggerInterface $logger + ) { } - public function log($payload) + public function log(array $payload): ?mixed { $this->logger->log($payload['level'], $payload['message'], $payload['context']); return null; diff --git a/tests/Cases/AbstractTestCase.php b/tests/Cases/AbstractTestCase.php index 11c66de..6bee0c3 100644 --- a/tests/Cases/AbstractTestCase.php +++ b/tests/Cases/AbstractTestCase.php @@ -12,15 +12,14 @@ namespace HyperfTest\Cases; use Hyperf\Config\Config; +use Hyperf\Context\ApplicationContext; use Hyperf\Contract\ConfigInterface; use Hyperf\Contract\StdoutLoggerInterface; use Hyperf\Di\Container; use Hyperf\Di\Definition\DefinitionSource; -use Hyperf\Di\Definition\ScanConfig; use Hyperf\Framework\Logger\StdoutLogger; use Hyperf\GoTask\GoTask; use Hyperf\GoTask\SocketGoTask; -use Hyperf\Utils\ApplicationContext; use PHPUnit\Framework\TestCase; /** @@ -34,7 +33,7 @@ public function setUp(): void { ! defined('BASE_PATH') && define('BASE_PATH', dirname(__DIR__, 1)); @unlink(self::UNIX_SOCKET); - $container = new Container(new DefinitionSource([], new ScanConfig())); + $container = new Container(new DefinitionSource([])); $container->set(ConfigInterface::class, new Config([ 'gotask' => [ 'enable' => true, diff --git a/tests/Cases/CoroutineSocketTest.php b/tests/Cases/CoroutineSocketTest.php index 7954722..be56848 100644 --- a/tests/Cases/CoroutineSocketTest.php +++ b/tests/Cases/CoroutineSocketTest.php @@ -11,14 +11,17 @@ */ namespace HyperfTest\Cases; +use Hyperf\Coroutine\WaitGroup; use Hyperf\GoTask\IPC\SocketIPCSender; use Hyperf\GoTask\Relay\RelayInterface; use Hyperf\GoTask\SocketGoTask; -use Hyperf\Utils\WaitGroup; use Spiral\Goridge\Exceptions\ServiceException; use Swoole\Process; use Throwable; +use function Hyperf\Coroutine\go; +use function Hyperf\Support\make; + /** * @internal * @coversNothing @@ -27,10 +30,7 @@ class CoroutineSocketTest extends AbstractTestCase { public const UNIX_SOCKET = __DIR__ . '/test.sock'; - /** - * @var Process - */ - private $p; + private Process $p; public function setUp(): void { diff --git a/tests/Cases/MongoDBTest.php b/tests/Cases/MongoDBTest.php index d4958d4..65326ed 100644 --- a/tests/Cases/MongoDBTest.php +++ b/tests/Cases/MongoDBTest.php @@ -17,21 +17,17 @@ use Hyperf\GoTask\MongoClient\Type\InsertOneResult; use Swoole\Process; +use function Hyperf\Support\make; + /** * @internal * @coversNothing */ class MongoDBTest extends AbstractTestCase { - /** - * @var Process - */ - private $p; + private Process $p; - /** - * @var mixed|MongoClient - */ - private $client; + private MongoClient $client; public function setUp(): void { diff --git a/tests/Cases/ProcessPipeTest.php b/tests/Cases/ProcessPipeTest.php index 03c7cd9..4064644 100644 --- a/tests/Cases/ProcessPipeTest.php +++ b/tests/Cases/ProcessPipeTest.php @@ -11,24 +11,24 @@ */ namespace HyperfTest\Cases; +use Hyperf\Coroutine\WaitGroup; use Hyperf\GoTask\IPC\PipeIPCSender; use Hyperf\GoTask\PipeGoTask; use Hyperf\GoTask\Relay\RelayInterface; -use Hyperf\Utils\WaitGroup; use Spiral\Goridge\Exceptions\ServiceException; use Swoole\Process; use Throwable; +use function Hyperf\Coroutine\go; +use function Hyperf\Support\make; + /** * @internal * @coversNothing */ class ProcessPipeTest extends AbstractTestCase { - /** - * @var Process - */ - private $p; + private Process $p; public function setUp(): void { diff --git a/tests/Cases/SocketIPCReceiverTest.php b/tests/Cases/SocketIPCReceiverTest.php index 1aecb3b..806ba62 100644 --- a/tests/Cases/SocketIPCReceiverTest.php +++ b/tests/Cases/SocketIPCReceiverTest.php @@ -14,10 +14,12 @@ use Hyperf\GoTask\IPC\SocketIPCReceiver; use Hyperf\GoTask\IPC\SocketIPCSender; use Hyperf\GoTask\Relay\RelayInterface; -use Hyperf\Utils\WaitGroup; +use Hyperf\Coroutine\WaitGroup; use Spiral\Goridge\Exceptions\ServiceException; use Throwable; +use function Hyperf\Coroutine\go; + /** * @internal * @coversNothing @@ -33,7 +35,7 @@ public function testOnCoroutine() try { $receiver = new SocketIPCReceiver(self::UNIX_SOCKET); $receiver->start(); - } catch (Throwable $e) { + } catch (Throwable) { // Not Reachable $this->assertTrue(false); } diff --git a/tests/TestServer.php b/tests/TestServer.php index 213bcdb..6cca385 100644 --- a/tests/TestServer.php +++ b/tests/TestServer.php @@ -12,17 +12,17 @@ namespace HyperfTest; use Hyperf\Config\Config; +use Hyperf\Context\ApplicationContext; use Hyperf\Contract\ConfigInterface; use Hyperf\Contract\StdoutLoggerInterface; use Hyperf\Di\Container; use Hyperf\Di\Definition\DefinitionSource; -use Hyperf\Di\Definition\ScanConfig; use Hyperf\Framework\Logger\StdoutLogger; use Hyperf\GoTask\IPC\SocketIPCReceiver; -use Hyperf\Utils\ApplicationContext; use Psr\Log\LoggerInterface; use Swoole\Timer; +use function Hyperf\Support\env; use function Swoole\Coroutine\run; require __DIR__ . '/../vendor/autoload.php'; @@ -30,7 +30,7 @@ const ADDR = __DIR__ . '/test.sock'; @unlink(ADDR); -$container = new Container(new DefinitionSource([], new ScanConfig())); +$container = new Container(new DefinitionSource([])); $container->set(ConfigInterface::class, new Config([ 'gotask' => [ 'enable' => true,