From 1da9cad6b954e6cb564e372064690effd09b007a Mon Sep 17 00:00:00 2001 From: Daniil Gentili Date: Thu, 1 Oct 2020 21:36:07 +0200 Subject: [PATCH] Improved typehints --- src/danog/MadelineProto/MTProto.php | 14 +++++--- .../MTProtoTools/AuthKeyHandler.php | 12 +++++-- .../MadelineProto/MTProtoTools/Files.php | 8 +++-- .../MadelineProto/MTProtoTools/FilesLogic.php | 6 ++-- .../MTProtoTools/PeerHandler.php | 36 +++++++++++++------ .../MTProtoTools/UpdateHandler.php | 6 ++-- src/danog/MadelineProto/RSA.php | 2 +- .../SecretChats/MessageHandler.php | 6 ++++ .../MadelineProto/TL/Conversion/BotAPI.php | 28 +++++++++++---- .../TL/Conversion/BotAPIFiles.php | 1 + src/danog/MadelineProto/TL/TL.php | 12 +++++-- .../MadelineProto/VoIP/AuthKeyHandler.php | 4 +-- .../MadelineProto/Wrappers/DialogHandler.php | 4 ++- src/danog/MadelineProto/Wrappers/Login.php | 4 ++- src/danog/MadelineProto/Wrappers/Loop.php | 2 +- 15 files changed, 105 insertions(+), 40 deletions(-) diff --git a/src/danog/MadelineProto/MTProto.php b/src/danog/MadelineProto/MTProto.php index 9f82177f..39a75239 100644 --- a/src/danog/MadelineProto/MTProto.php +++ b/src/danog/MadelineProto/MTProto.php @@ -798,7 +798,9 @@ class MTProto extends AsyncConstruct implements TLCallback * * @param string $url URL * - * @return \Generator + * @return \Generator + * + * @psalm-return \Generator, mixed, string> */ public function fileGetContents(string $url): \Generator { @@ -816,9 +818,9 @@ class MTProto extends AsyncConstruct implements TLCallback /** * Get main DC ID. * - * @return int + * @return int|string */ - public function getDataCenterId(): int + public function getDataCenterId() { return $this->datacenter->curdc; } @@ -937,9 +939,11 @@ class MTProto extends AsyncConstruct implements TLCallback * * @internal * - * @return void + * @return \Generator + * + * @psalm-return \Generator */ - private function cleanupProperties() + private function cleanupProperties(): \Generator { if (!$this->channels_state instanceof CombinedUpdatesState) { $this->channels_state = new CombinedUpdatesState($this->channels_state); diff --git a/src/danog/MadelineProto/MTProtoTools/AuthKeyHandler.php b/src/danog/MadelineProto/MTProtoTools/AuthKeyHandler.php index 37a7bca1..b6826e91 100644 --- a/src/danog/MadelineProto/MTProtoTools/AuthKeyHandler.php +++ b/src/danog/MadelineProto/MTProtoTools/AuthKeyHandler.php @@ -60,7 +60,9 @@ trait AuthKeyHandler * * @internal * - * @return \Generator + * @return \Generator + * + * @psalm-return \Generator */ public function createAuthKey(int $expires_in, string $datacenter): \Generator { @@ -508,7 +510,9 @@ trait AuthKeyHandler * * @internal * - * @return \Generator + * @return \Generator + * + * @psalm-return \Generator */ public function bindTempAuthKey(int $expires_in, string $datacenter): \Generator { @@ -552,7 +556,9 @@ trait AuthKeyHandler * * @param string|integer $what Number to factorize * - * @return \Generator + * @return \Generator + * + * @psalm-return \Generator, mixed, false|int|string> */ private function wolframSingle($what): \Generator { diff --git a/src/danog/MadelineProto/MTProtoTools/Files.php b/src/danog/MadelineProto/MTProtoTools/Files.php index 0a69818d..cc6b302c 100644 --- a/src/danog/MadelineProto/MTProtoTools/Files.php +++ b/src/danog/MadelineProto/MTProtoTools/Files.php @@ -48,6 +48,7 @@ use function Amp\Promise\all; trait Files { use FilesLogic; + /** * Upload file from URL. * @@ -1049,7 +1050,7 @@ trait Files } while (true); } private $cdn_hashes = []; - private function addCdnHashes($file, $hashes) + private function addCdnHashes($file, $hashes): void { if (!isset($this->cdn_hashes[$file])) { $this->cdn_hashes = []; @@ -1075,7 +1076,10 @@ trait Files } return true; } - private function clearCdnHashes($file) + /** + * @return true + */ + private function clearCdnHashes($file): bool { unset($this->cdn_hashes[$file]); return true; diff --git a/src/danog/MadelineProto/MTProtoTools/FilesLogic.php b/src/danog/MadelineProto/MTProtoTools/FilesLogic.php index 78c7f6e4..783551e9 100644 --- a/src/danog/MadelineProto/MTProtoTools/FilesLogic.php +++ b/src/danog/MadelineProto/MTProtoTools/FilesLogic.php @@ -137,7 +137,9 @@ trait FilesLogic * @param ServerRequest $request Request * @param callable $cb Status callback (can also use FileCallback) * - * @return \Generator Returned response + * @return \Generator Returned response + * + * @psalm-return \Generator */ public function downloadToResponse($messageMedia, ServerRequest $request, callable $cb = null): \Generator { @@ -188,7 +190,7 @@ trait FilesLogic * * @return \Generator * - * @psalm-return \Generator|array, mixed, mixed> + * @psalm-return \Generator|\Amp\Promise<\Amp\Ipc\Sync\ChannelledSocket>|\Amp\Promise|\Amp\Promise|\Amp\Promise|\danog\MadelineProto\Stream\StreamInterface|array|int|mixed, mixed, mixed> */ public function uploadEncrypted($file, string $fileName = '', $cb = null): \Generator { diff --git a/src/danog/MadelineProto/MTProtoTools/PeerHandler.php b/src/danog/MadelineProto/MTProtoTools/PeerHandler.php index 1ef3a563..51856160 100644 --- a/src/danog/MadelineProto/MTProtoTools/PeerHandler.php +++ b/src/danog/MadelineProto/MTProtoTools/PeerHandler.php @@ -45,7 +45,7 @@ trait PeerHandler * * @param int $id MTProto channel ID * - * @return int + * @return float|int */ public static function toSupergroup($id) { @@ -56,9 +56,9 @@ trait PeerHandler * * @param int $id Bot API channel ID * - * @return int + * @return float|int */ - public static function fromSupergroup($id): int + public static function fromSupergroup($id) { return -$id - \pow(10, (int) \floor(\log(-$id, 10))); } @@ -149,7 +149,9 @@ trait PeerHandler * * @internal * - * @return \Generator + * @return \Generator + * + * @psalm-return \Generator */ public function addChat($chat): \Generator { @@ -209,14 +211,14 @@ trait PeerHandler } } - private function cacheChatUsername(int $id, array $chat) + private function cacheChatUsername(int $id, array $chat): void { if ($id && !empty($chat['username'])) { $this->usernames[\strtolower($chat['username'])] = $id; } } - private function cachePwrChat($id, $full_fetch, $send) + private function cachePwrChat($id, $full_fetch, $send): void { \danog\MadelineProto\Tools::callFork((function () use ($id, $full_fetch, $send): \Generator { try { @@ -233,7 +235,9 @@ trait PeerHandler * * @param mixed $id Peer * - * @return \Generator + * @return \Generator + * + * @psalm-return \Generator */ public function peerIsset($id): \Generator { @@ -469,7 +473,9 @@ trait PeerHandler * * @see https://docs.madelineproto.xyz/Info.html * - * @return \Generator Info object + * @return \Generator Info object + * + * @psalm-return \Generator|array, mixed, array|mixed> */ public function getInfo($id, $recursive = true): \Generator { @@ -627,7 +633,12 @@ trait PeerHandler } throw new \danog\MadelineProto\Exception('This peer is not present in the internal peer database'); } - private function genAll($constructor, $folder_id = null) + /** + * @return (((mixed|string)[]|mixed|string)[]|int|mixed|string)[] + * + * @psalm-return array{InputPeer: array{_: string, user_id?: mixed, access_hash?: mixed, min?: mixed, chat_id?: mixed, channel_id?: mixed}, Peer: array{_: string, user_id?: mixed, chat_id?: mixed, channel_id?: mixed}, DialogPeer: array{_: string, peer: array{_: string, user_id?: mixed, chat_id?: mixed, channel_id?: mixed}}, NotifyPeer: array{_: string, peer: array{_: string, user_id?: mixed, chat_id?: mixed, channel_id?: mixed}}, InputDialogPeer: array{_: string, peer: array{_: string, user_id?: mixed, access_hash?: mixed, min?: mixed, chat_id?: mixed, channel_id?: mixed}}, InputNotifyPeer: array{_: string, peer: array{_: string, user_id?: mixed, access_hash?: mixed, min?: mixed, chat_id?: mixed, channel_id?: mixed}}, bot_api_id: int|mixed, type: string} + */ + private function genAll($constructor, $folder_id = null): array { $res = [$this->TL->getConstructors()->findByPredicate($constructor['_'])['type'] => $constructor]; switch ($constructor['_']) { @@ -706,7 +717,9 @@ trait PeerHandler * * @see https://docs.madelineproto.xyz/FullInfo.html * - * @return \Generator FullInfo object + * @return \Generator FullInfo object + * + * @psalm-return \Generator */ public function getFullInfo($id): \Generator { @@ -714,6 +727,7 @@ trait PeerHandler if (\time() - (yield from $this->fullChatLastUpdated($partial['bot_api_id'])) < $this->getSettings()->getPeer()->getFullInfoCacheTime()) { return \array_merge($partial, yield $this->full_chats[$partial['bot_api_id']]); } + $full = null; switch ($partial['type']) { case 'user': case 'bot': @@ -992,7 +1006,7 @@ trait PeerHandler } return $has_more; } - private function fetchParticipantsCache($channel, $filter, $q, $offset, $limit) + private function fetchParticipantsCache($channel, $filter, $q, $offset, $limit): \Generator { return (yield $this->channel_participants[$channel['channel_id']])[$filter][$q][$offset][$limit]; } diff --git a/src/danog/MadelineProto/MTProtoTools/UpdateHandler.php b/src/danog/MadelineProto/MTProtoTools/UpdateHandler.php index e2e1e29e..54de5119 100644 --- a/src/danog/MadelineProto/MTProtoTools/UpdateHandler.php +++ b/src/danog/MadelineProto/MTProtoTools/UpdateHandler.php @@ -55,7 +55,9 @@ trait UpdateHandler * * @internal * - * @return \Generator + * @return \Generator + * + * @psalm-return \Generator, mixed, list|mixed> */ public function getUpdates($params = []): \Generator { @@ -152,7 +154,7 @@ trait UpdateHandler * * @internal * - * @return UpdatesState|UpdatesState[] + * @return \Generator */ public function loadUpdateState(): \Generator { diff --git a/src/danog/MadelineProto/RSA.php b/src/danog/MadelineProto/RSA.php index 63b68a82..56466980 100644 --- a/src/danog/MadelineProto/RSA.php +++ b/src/danog/MadelineProto/RSA.php @@ -53,7 +53,7 @@ class RSA * * @return \Generator * - * @psalm-return \Generator + * @psalm-return \Generator */ public function load(TL $TL, string $rsa_key): \Generator { diff --git a/src/danog/MadelineProto/SecretChats/MessageHandler.php b/src/danog/MadelineProto/SecretChats/MessageHandler.php index d8c7d12b..712a1047 100644 --- a/src/danog/MadelineProto/SecretChats/MessageHandler.php +++ b/src/danog/MadelineProto/SecretChats/MessageHandler.php @@ -125,6 +125,9 @@ trait MessageHandler $this->secret_chats[$message['message']['chat_id']]['incoming'][$this->secret_chats[$message['message']['chat_id']]['in_seq_no']] = $message['message']; yield from $this->handleDecryptedUpdate($message); } + /** + * @return false|string + */ private function tryMTProtoV1Decrypt($message_key, $chat_id, $old, $encrypted_data) { list($aes_key, $aes_iv) = Crypt::oldAesCalculate($message_key, $this->secret_chats[$chat_id][$old ? 'old_key' : 'key']['auth_key'], true); @@ -145,6 +148,9 @@ trait MessageHandler } return $message_data; } + /** + * @return false|string + */ private function tryMTProtoV2Decrypt($message_key, $chat_id, $old, $encrypted_data) { list($aes_key, $aes_iv) = Crypt::aesCalculate($message_key, $this->secret_chats[$chat_id][$old ? 'old_key' : 'key']['auth_key'], !$this->secret_chats[$chat_id]['admin']); diff --git a/src/danog/MadelineProto/TL/Conversion/BotAPI.php b/src/danog/MadelineProto/TL/Conversion/BotAPI.php index 0aec9a77..955c58c8 100644 --- a/src/danog/MadelineProto/TL/Conversion/BotAPI.php +++ b/src/danog/MadelineProto/TL/Conversion/BotAPI.php @@ -36,9 +36,9 @@ trait BotAPI * * @param string $text Text * - * @return int + * @return float|int */ - public static function mbStrlen(string $text): int + public static function mbStrlen(string $text) { $length = 0; $textlength = \strlen($text); @@ -107,7 +107,12 @@ trait BotAPI } return $result; } - private function parseButtons($rows) + /** + * @return ((bool|mixed|string)[][]|string)[][] + * + * @psalm-return array}> + */ + private function parseButtons($rows): array { $newrows = []; $key = 0; @@ -190,6 +195,7 @@ trait BotAPI } return $newd; } + $res = null; switch ($data['_']) { case 'updateShortSentMessage': $newd['message_id'] = $data['id']; @@ -679,7 +685,12 @@ trait BotAPI } return $multiple_args; } - private function multipleExplodeKeepDelimiters($delimiters, $string) + /** + * @return string[] + * + * @psalm-return list + */ + private function multipleExplodeKeepDelimiters($delimiters, $string): array { $initialArray = \explode(\chr(1), \str_replace($delimiters, \chr(1), $string)); $finalArray = []; @@ -693,7 +704,7 @@ trait BotAPI } return $finalArray; } - private function htmlFixtags($text) + private function htmlFixtags($text): string { $diff = 0; \preg_match_all('#(.*?)(<(\\bu\\b|\\bs\\b|\\ba\\b|\\bb\\b|\\bstrong\\b|\\bblockquote\\b|\\bstrike\\b|\\bdel\\b|\\bem\\b|i|\\bcode\\b|\\bpre\\b)[^>]*>)(.*?)([<]\\s*/\\s*\\3[>])#is', $text, $matches, PREG_SET_ORDER | PREG_OFFSET_CAPTURE); @@ -728,7 +739,12 @@ trait BotAPI } return \htmlentities($text); } - private function buildRows($button_list) + /** + * @return ((array|string)[][]|string)[] + * + * @psalm-return array{_: string, rows: list}>} + */ + private function buildRows($button_list): array { $end = false; $rows = []; diff --git a/src/danog/MadelineProto/TL/Conversion/BotAPIFiles.php b/src/danog/MadelineProto/TL/Conversion/BotAPIFiles.php index 0922c631..d5b57de1 100644 --- a/src/danog/MadelineProto/TL/Conversion/BotAPIFiles.php +++ b/src/danog/MadelineProto/TL/Conversion/BotAPIFiles.php @@ -90,6 +90,7 @@ trait BotAPIFiles $photoSize = $fileId->hasPhotoSizeSource() ? $fileId->getPhotoSizeSource() : null; + $res = null; switch ($fileId->getType()) { case PROFILE_PHOTO: /** diff --git a/src/danog/MadelineProto/TL/TL.php b/src/danog/MadelineProto/TL/TL.php index 9a236a15..4bfc2321 100644 --- a/src/danog/MadelineProto/TL/TL.php +++ b/src/danog/MadelineProto/TL/TL.php @@ -401,7 +401,9 @@ class TL * @param string $ctx Context * @param integer $layer Layer version * - * @return \Generator + * @return \Generator + * + * @psalm-return \Generator */ public function serializeObject(array $type, $object, $ctx, int $layer = -1): \Generator { @@ -581,7 +583,9 @@ class TL * @param string $method Method name * @param mixed $arguments Arguments * - * @return \Generator + * @return \Generator + * + * @psalm-return \Generator|Promise<\Amp\Ipc\Sync\ChannelledSocket>|Promise|Promise|Promise|Promise|\danog\MadelineProto\Stream\StreamInterface|array|int|mixed, mixed, string> */ public function serializeMethod(string $method, $arguments): \Generator { @@ -662,7 +666,9 @@ class TL * @param string $ctx Context * @param integer $layer Layer * - * @return \Generator + * @return \Generator + * + * @psalm-return \Generator|Promise<\Amp\Ipc\Sync\ChannelledSocket>|Promise|Promise|Promise|\danog\MadelineProto\Stream\StreamInterface|array|int|mixed, mixed, string> */ private function serializeParams(array $tl, $arguments, $ctx, int $layer = -1): \Generator { diff --git a/src/danog/MadelineProto/VoIP/AuthKeyHandler.php b/src/danog/MadelineProto/VoIP/AuthKeyHandler.php index 2ee00b7c..2fea7647 100644 --- a/src/danog/MadelineProto/VoIP/AuthKeyHandler.php +++ b/src/danog/MadelineProto/VoIP/AuthKeyHandler.php @@ -86,7 +86,7 @@ trait AuthKeyHandler * * @param mixed $user User * - * @return void + * @return \Generator */ public function requestCall($user): \Generator { @@ -276,7 +276,7 @@ trait AuthKeyHandler * Discard call. * * @param array $call Call - * @param string $reason Discard reason + * @param array $reason * @param array $rating Rating * @param boolean $need_debug Need debug? * diff --git a/src/danog/MadelineProto/Wrappers/DialogHandler.php b/src/danog/MadelineProto/Wrappers/DialogHandler.php index b6b1bb5f..4ce6872a 100644 --- a/src/danog/MadelineProto/Wrappers/DialogHandler.php +++ b/src/danog/MadelineProto/Wrappers/DialogHandler.php @@ -33,7 +33,9 @@ trait DialogHandler * * @param boolean $force Whether to refetch all dialogs ignoring cache * - * @return \Generator> + * @return \Generator + * + * @psalm-return \Generator, mixed, list> */ public function getDialogs(bool $force = true): \Generator { diff --git a/src/danog/MadelineProto/Wrappers/Login.php b/src/danog/MadelineProto/Wrappers/Login.php index 8ccd8b4f..5907eea6 100644 --- a/src/danog/MadelineProto/Wrappers/Login.php +++ b/src/danog/MadelineProto/Wrappers/Login.php @@ -202,7 +202,9 @@ trait Login /** * Export authorization. * - * @return \Generator + * @return \Generator + * + * @psalm-return \Generator */ public function exportAuthorization(): \Generator { diff --git a/src/danog/MadelineProto/Wrappers/Loop.php b/src/danog/MadelineProto/Wrappers/Loop.php index f2d0cc23..dd11912f 100644 --- a/src/danog/MadelineProto/Wrappers/Loop.php +++ b/src/danog/MadelineProto/Wrappers/Loop.php @@ -94,7 +94,7 @@ trait Loop * * @param callable|null $callback Async callable to run * - * @return mixed + * @return \Generator */ public function loop($callback = null): \Generator {