From ec2719db4e98ddbac82535a0daa0148f86c1cd1d Mon Sep 17 00:00:00 2001 From: Daniil Gentili Date: Sat, 7 Mar 2020 21:45:50 +0100 Subject: [PATCH] Small cleanups --- composer.json | 4 +- src/danog/MadelineProto/API.php | 12 +++- src/danog/MadelineProto/DoHConnector.php | 2 + .../MTProtoSession/CallHandler.php | 2 +- .../MTProtoSession/MsgIdHandler.php | 65 ++++++++++++++++-- .../MsgIdHandler/MsgIdHandler32.php | 4 +- .../MsgIdHandler/MsgIdHandler64.php | 4 +- .../MTProtoSession/MsgIdHandlerAbstract.php | 66 ------------------- .../MadelineProto/MTProtoSession/Session.php | 2 +- src/danog/MadelineProto/Serialization.php | 8 ++- 10 files changed, 88 insertions(+), 81 deletions(-) delete mode 100644 src/danog/MadelineProto/MTProtoSession/MsgIdHandlerAbstract.php diff --git a/composer.json b/composer.json index 5ac25013..ccff9c0d 100644 --- a/composer.json +++ b/composer.json @@ -48,7 +48,8 @@ "amphp/websocket": "dev-master as 1", "ext-ctype": "*", "danog/7to70": "^1", - "danog/7to5": "^1" + "danog/7to5": "^1", + "vimeo/psalm": "dev-master" }, "suggest": { "ext-libtgvoip": "Install the php-libtgvoip extension to make phone calls (https://github.com/danog/php-libtgvoip)" @@ -64,7 +65,6 @@ "files": [ "src/BigIntegor.php", "src/YieldReturnValue.php", - "src/ReflectionGenerator.php", "src/polyfill.php" ] }, diff --git a/src/danog/MadelineProto/API.php b/src/danog/MadelineProto/API.php index 1e422e72..589e34f0 100644 --- a/src/danog/MadelineProto/API.php +++ b/src/danog/MadelineProto/API.php @@ -140,7 +140,6 @@ class API extends InternalDoc if (isset($this->API)) { $this->storage = $this->API->storage ?? $this->storage; - $unserialized->oldInstance = true; unset($unserialized); yield from $this->API->initAsynchronously(); @@ -165,6 +164,15 @@ class API extends InternalDoc $this->logger->logger(Lang::$current_lang['madelineproto_ready'], Logger::NOTICE); } + /** + * Wakeup function. + * + * @return void + */ + public function __wakeup(): void + { + $this->oldInstance = true; + } /** * Destruct function. * @@ -248,7 +256,7 @@ class API extends InternalDoc $promises = []; foreach ($instances as $k => $instance) { $instance->start(['async' => false]); - $promises []= Tools::call($instance->startAndLoopAsync($eventHandler[$k])); + $promises []= $instance->startAndLoopAsync($eventHandler[$k]); } Tools::wait(Tools::all($promises)); return; diff --git a/src/danog/MadelineProto/DoHConnector.php b/src/danog/MadelineProto/DoHConnector.php index 538e0f4f..41b60e2c 100644 --- a/src/danog/MadelineProto/DoHConnector.php +++ b/src/danog/MadelineProto/DoHConnector.php @@ -153,8 +153,10 @@ class DoHConnector implements Connector } return ResourceSocket::fromClientSocket($socket, $socketContext->getTlsContext()); } + // This is reached if either all URIs failed or the maximum number of attempts is reached. /** @noinspection PhpUndefinedVariableInspection */ + throw $e; })()); } diff --git a/src/danog/MadelineProto/MTProtoSession/CallHandler.php b/src/danog/MadelineProto/MTProtoSession/CallHandler.php index bf84f803..b6305174 100644 --- a/src/danog/MadelineProto/MTProtoSession/CallHandler.php +++ b/src/danog/MadelineProto/MTProtoSession/CallHandler.php @@ -101,7 +101,7 @@ trait CallHandler * @param array $args Arguments * @param array $aargs Additional arguments * - * @return Generator + * @return \Generator */ public function methodCallAsyncWrite(string $method, $args = [], array $aargs = ['msg_id' => null]): \Generator { diff --git a/src/danog/MadelineProto/MTProtoSession/MsgIdHandler.php b/src/danog/MadelineProto/MTProtoSession/MsgIdHandler.php index 7cbc48c7..28c54167 100644 --- a/src/danog/MadelineProto/MTProtoSession/MsgIdHandler.php +++ b/src/danog/MadelineProto/MTProtoSession/MsgIdHandler.php @@ -17,11 +17,68 @@ * @link https://docs.madelineproto.xyz MadelineProto documentation */ +namespace danog\MadelineProto\MTProtoSession; + use danog\MadelineProto\MTProtoSession\MsgIdHandler\MsgIdHandler32; use danog\MadelineProto\MTProtoSession\MsgIdHandler\MsgIdHandler64; -if (PHP_INT_SIZE === 8) { - \class_alias(MsgIdHandler64::class, \danog\MadelineProto\MTProtoSession\MsgIdHandler::class); -} else { - \class_alias(MsgIdHandler32::class, \danog\MadelineProto\MTProtoSession\MsgIdHandler::class); +/** + * Manages message ids. + */ +abstract class MsgIdHandler +{ + /** + * Session instance. + * + * @var Session + */ + protected $session; + /** + * Constructor. + * + * @param Session $session Session + */ + private function __construct(Session $session) + { + $this->session = $session; + } + + /** + * Create MsgIdHandler instance. + * + * @param Session $session Session + * + * @return self + */ + public static function createInstance(Session $session): self + { + if (PHP_INT_SIZE === 8) { + return new MsgIdHandler64($session); + } + return new MsgIdHandler32($session); + } + + /** + * Check validity of given message ID. + * + * @param string $newMessageId New message ID + * @param array $aargs Params + * + * @return void + */ + abstract public function checkMessageId($newMessageId, array $aargs): void; + /** + * Generate outgoing message ID. + * + * @return string + */ + abstract public function generateMessageId(): string; + /** + * Get maximum message ID. + * + * @param boolean $incoming Incoming or outgoing message ID + * + * @return mixed + */ + abstract public function getMaxId(bool $incoming); } diff --git a/src/danog/MadelineProto/MTProtoSession/MsgIdHandler/MsgIdHandler32.php b/src/danog/MadelineProto/MTProtoSession/MsgIdHandler/MsgIdHandler32.php index 2792eeae..b29db062 100644 --- a/src/danog/MadelineProto/MTProtoSession/MsgIdHandler/MsgIdHandler32.php +++ b/src/danog/MadelineProto/MTProtoSession/MsgIdHandler/MsgIdHandler32.php @@ -19,12 +19,12 @@ namespace danog\MadelineProto\MTProtoSession\MsgIdHandler; -use danog\MadelineProto\MTProtoSession\MsgIdHandlerAbstract; +use danog\MadelineProto\MTProtoSession\MsgIdHandler; /** * Manages message ids. */ -class MsgIdHandler32 extends MsgIdHandlerAbstract +class MsgIdHandler32 extends MsgIdHandler { /** * Maximum incoming ID. diff --git a/src/danog/MadelineProto/MTProtoSession/MsgIdHandler/MsgIdHandler64.php b/src/danog/MadelineProto/MTProtoSession/MsgIdHandler/MsgIdHandler64.php index 2134216d..b57c0509 100644 --- a/src/danog/MadelineProto/MTProtoSession/MsgIdHandler/MsgIdHandler64.php +++ b/src/danog/MadelineProto/MTProtoSession/MsgIdHandler/MsgIdHandler64.php @@ -19,13 +19,13 @@ namespace danog\MadelineProto\MTProtoSession\MsgIdHandler; -use danog\MadelineProto\MTProtoSession\MsgIdHandlerAbstract; +use danog\MadelineProto\MTProtoSession\MsgIdHandler; use danog\MadelineProto\Tools; /** * Manages message ids. */ -class MsgIdHandler64 extends MsgIdHandlerAbstract +class MsgIdHandler64 extends MsgIdHandler { /** * Maximum incoming ID. diff --git a/src/danog/MadelineProto/MTProtoSession/MsgIdHandlerAbstract.php b/src/danog/MadelineProto/MTProtoSession/MsgIdHandlerAbstract.php deleted file mode 100644 index f96a77b7..00000000 --- a/src/danog/MadelineProto/MTProtoSession/MsgIdHandlerAbstract.php +++ /dev/null @@ -1,66 +0,0 @@ -. - * - * @author Daniil Gentili - * @copyright 2016-2020 Daniil Gentili - * @license https://opensource.org/licenses/AGPL-3.0 AGPLv3 - * - * @link https://docs.madelineproto.xyz MadelineProto documentation - */ - -namespace danog\MadelineProto\MTProtoSession; - -/** - * Manages message ids. - */ -abstract class MsgIdHandlerAbstract -{ - /** - * Session instance. - * - * @var Session - */ - protected $session; - /** - * Constructor. - * - * @param Session $session Session - */ - public function __construct(Session $session) - { - $this->session = $session; - } - - /** - * Check validity of given message ID. - * - * @param string $newMessageId New message ID - * @param array $aargs Params - * - * @return void - */ - abstract public function checkMessageId($newMessageId, array $aargs): void; - /** - * Generate outgoing message ID. - * - * @return string - */ - abstract public function generateMessageId(): string; - /** - * Get maximum message ID. - * - * @param boolean $incoming Incoming or outgoing message ID - * - * @return mixed - */ - abstract public function getMaxId(bool $incoming); -} diff --git a/src/danog/MadelineProto/MTProtoSession/Session.php b/src/danog/MadelineProto/MTProtoSession/Session.php index 3cbff00b..bfa0d2e8 100644 --- a/src/danog/MadelineProto/MTProtoSession/Session.php +++ b/src/danog/MadelineProto/MTProtoSession/Session.php @@ -53,7 +53,7 @@ abstract class Session $this->session_id = \danog\MadelineProto\Tools::random(8); $this->session_in_seq_no = 0; $this->session_out_seq_no = 0; - $this->msgIdHandler = new MsgIdHandler($this); + $this->msgIdHandler = MsgIdHandler::createInstance($this); } /** * Create MTProto session if needed. diff --git a/src/danog/MadelineProto/Serialization.php b/src/danog/MadelineProto/Serialization.php index a8b25b7b..7a8c1cdb 100644 --- a/src/danog/MadelineProto/Serialization.php +++ b/src/danog/MadelineProto/Serialization.php @@ -27,6 +27,12 @@ use function Amp\File\get; */ class Serialization { + /** + * List of session paths. + * + * @var array + */ + private $paths = []; /** * Extract path components for serialization. * @@ -60,7 +66,7 @@ class Serialization } finally { $unlock(); } - \danog\MadelineProto\Magic::classExists(); + Magic::classExists(); try { $unserialized = \unserialize($tounserialize); } catch (\danog\MadelineProto\Bug74586Exception $e) {