Improved typehints

This commit is contained in:
Daniil Gentili 2020-10-01 21:36:07 +02:00
parent 76c4fe480c
commit 1da9cad6b9
Signed by: danog
GPG Key ID: 8C1BE3B34B230CA7
15 changed files with 105 additions and 40 deletions

View File

@ -798,7 +798,9 @@ class MTProto extends AsyncConstruct implements TLCallback
* *
* @param string $url URL * @param string $url URL
* *
* @return \Generator<string> * @return \Generator
*
* @psalm-return \Generator<int, Promise<string>, mixed, string>
*/ */
public function fileGetContents(string $url): \Generator public function fileGetContents(string $url): \Generator
{ {
@ -816,9 +818,9 @@ class MTProto extends AsyncConstruct implements TLCallback
/** /**
* Get main DC ID. * Get main DC ID.
* *
* @return int * @return int|string
*/ */
public function getDataCenterId(): int public function getDataCenterId()
{ {
return $this->datacenter->curdc; return $this->datacenter->curdc;
} }
@ -937,9 +939,11 @@ class MTProto extends AsyncConstruct implements TLCallback
* *
* @internal * @internal
* *
* @return void * @return \Generator
*
* @psalm-return \Generator<mixed, mixed, mixed, void>
*/ */
private function cleanupProperties() private function cleanupProperties(): \Generator
{ {
if (!$this->channels_state instanceof CombinedUpdatesState) { if (!$this->channels_state instanceof CombinedUpdatesState) {
$this->channels_state = new CombinedUpdatesState($this->channels_state); $this->channels_state = new CombinedUpdatesState($this->channels_state);

View File

@ -60,7 +60,9 @@ trait AuthKeyHandler
* *
* @internal * @internal
* *
* @return \Generator<AuthKey> * @return \Generator
*
* @psalm-return \Generator<mixed, mixed|string, mixed, \danog\MadelineProto\MTProto\PermAuthKey|\danog\MadelineProto\MTProto\TempAuthKey|null>
*/ */
public function createAuthKey(int $expires_in, string $datacenter): \Generator public function createAuthKey(int $expires_in, string $datacenter): \Generator
{ {
@ -508,7 +510,9 @@ trait AuthKeyHandler
* *
* @internal * @internal
* *
* @return \Generator<bool> * @return \Generator
*
* @psalm-return \Generator<int|mixed, array|mixed, mixed, true>
*/ */
public function bindTempAuthKey(int $expires_in, string $datacenter): \Generator public function bindTempAuthKey(int $expires_in, string $datacenter): \Generator
{ {
@ -552,7 +556,9 @@ trait AuthKeyHandler
* *
* @param string|integer $what Number to factorize * @param string|integer $what Number to factorize
* *
* @return \Generator<string|bool> * @return \Generator
*
* @psalm-return \Generator<int, \Amp\Promise<string>, mixed, false|int|string>
*/ */
private function wolframSingle($what): \Generator private function wolframSingle($what): \Generator
{ {

View File

@ -48,6 +48,7 @@ use function Amp\Promise\all;
trait Files trait Files
{ {
use FilesLogic; use FilesLogic;
/** /**
* Upload file from URL. * Upload file from URL.
* *
@ -1049,7 +1050,7 @@ trait Files
} while (true); } while (true);
} }
private $cdn_hashes = []; private $cdn_hashes = [];
private function addCdnHashes($file, $hashes) private function addCdnHashes($file, $hashes): void
{ {
if (!isset($this->cdn_hashes[$file])) { if (!isset($this->cdn_hashes[$file])) {
$this->cdn_hashes = []; $this->cdn_hashes = [];
@ -1075,7 +1076,10 @@ trait Files
} }
return true; return true;
} }
private function clearCdnHashes($file) /**
* @return true
*/
private function clearCdnHashes($file): bool
{ {
unset($this->cdn_hashes[$file]); unset($this->cdn_hashes[$file]);
return true; return true;

View File

@ -137,7 +137,9 @@ trait FilesLogic
* @param ServerRequest $request Request * @param ServerRequest $request Request
* @param callable $cb Status callback (can also use FileCallback) * @param callable $cb Status callback (can also use FileCallback)
* *
* @return \Generator<Response> Returned response * @return \Generator Returned response
*
* @psalm-return \Generator<mixed, array, mixed, \Amp\Http\Server\Response>
*/ */
public function downloadToResponse($messageMedia, ServerRequest $request, callable $cb = null): \Generator public function downloadToResponse($messageMedia, ServerRequest $request, callable $cb = null): \Generator
{ {
@ -188,7 +190,7 @@ trait FilesLogic
* *
* @return \Generator * @return \Generator
* *
* @psalm-return \Generator<int|mixed, \Amp\Promise|\Amp\Promise<\Amp\File\File>|array, mixed, mixed> * @psalm-return \Generator<int|mixed, \Amp\Promise|\Amp\Promise<\Amp\File\File>|\Amp\Promise<\Amp\Ipc\Sync\ChannelledSocket>|\Amp\Promise<int>|\Amp\Promise<mixed>|\Amp\Promise<null|string>|\danog\MadelineProto\Stream\StreamInterface|array|int|mixed, mixed, mixed>
*/ */
public function uploadEncrypted($file, string $fileName = '', $cb = null): \Generator public function uploadEncrypted($file, string $fileName = '', $cb = null): \Generator
{ {

View File

@ -45,7 +45,7 @@ trait PeerHandler
* *
* @param int $id MTProto channel ID * @param int $id MTProto channel ID
* *
* @return int * @return float|int
*/ */
public static function toSupergroup($id) public static function toSupergroup($id)
{ {
@ -56,9 +56,9 @@ trait PeerHandler
* *
* @param int $id Bot API channel ID * @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))); return -$id - \pow(10, (int) \floor(\log(-$id, 10)));
} }
@ -149,7 +149,9 @@ trait PeerHandler
* *
* @internal * @internal
* *
* @return \Generator<void> * @return \Generator
*
* @psalm-return \Generator<int, \Amp\Promise|null, mixed, void>
*/ */
public function addChat($chat): \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'])) { if ($id && !empty($chat['username'])) {
$this->usernames[\strtolower($chat['username'])] = $id; $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 { \danog\MadelineProto\Tools::callFork((function () use ($id, $full_fetch, $send): \Generator {
try { try {
@ -233,7 +235,9 @@ trait PeerHandler
* *
* @param mixed $id Peer * @param mixed $id Peer
* *
* @return \Generator<boolean> * @return \Generator
*
* @psalm-return \Generator<int|mixed, \Amp\Promise|array, mixed, bool>
*/ */
public function peerIsset($id): \Generator public function peerIsset($id): \Generator
{ {
@ -469,7 +473,9 @@ trait PeerHandler
* *
* @see https://docs.madelineproto.xyz/Info.html * @see https://docs.madelineproto.xyz/Info.html
* *
* @return \Generator<array> Info object * @return \Generator Info object
*
* @psalm-return \Generator<int|mixed, \Amp\Promise|\Amp\Promise<string>|array, mixed, array|mixed>
*/ */
public function getInfo($id, $recursive = true): \Generator 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'); 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]; $res = [$this->TL->getConstructors()->findByPredicate($constructor['_'])['type'] => $constructor];
switch ($constructor['_']) { switch ($constructor['_']) {
@ -706,7 +717,9 @@ trait PeerHandler
* *
* @see https://docs.madelineproto.xyz/FullInfo.html * @see https://docs.madelineproto.xyz/FullInfo.html
* *
* @return \Generator<array> FullInfo object * @return \Generator FullInfo object
*
* @psalm-return \Generator<int|mixed, \Amp\Promise|array, mixed, array>
*/ */
public function getFullInfo($id): \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()) { 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']]); return \array_merge($partial, yield $this->full_chats[$partial['bot_api_id']]);
} }
$full = null;
switch ($partial['type']) { switch ($partial['type']) {
case 'user': case 'user':
case 'bot': case 'bot':
@ -992,7 +1006,7 @@ trait PeerHandler
} }
return $has_more; 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]; return (yield $this->channel_participants[$channel['channel_id']])[$filter][$q][$offset][$limit];
} }

View File

@ -55,7 +55,9 @@ trait UpdateHandler
* *
* @internal * @internal
* *
* @return \Generator<array> * @return \Generator
*
* @psalm-return \Generator<int, \Amp\Promise<mixed|null>, mixed, list<array{update_id: mixed, update: mixed}>|mixed>
*/ */
public function getUpdates($params = []): \Generator public function getUpdates($params = []): \Generator
{ {
@ -152,7 +154,7 @@ trait UpdateHandler
* *
* @internal * @internal
* *
* @return UpdatesState|UpdatesState[] * @return \Generator
*/ */
public function loadUpdateState(): \Generator public function loadUpdateState(): \Generator
{ {

View File

@ -53,7 +53,7 @@ class RSA
* *
* @return \Generator * @return \Generator
* *
* @psalm-return \Generator<mixed, string, mixed, self> * @psalm-return \Generator<int|mixed, array|mixed, mixed, self>
*/ */
public function load(TL $TL, string $rsa_key): \Generator public function load(TL $TL, string $rsa_key): \Generator
{ {

View File

@ -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']; $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); yield from $this->handleDecryptedUpdate($message);
} }
/**
* @return false|string
*/
private function tryMTProtoV1Decrypt($message_key, $chat_id, $old, $encrypted_data) 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); 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 $message_data;
} }
/**
* @return false|string
*/
private function tryMTProtoV2Decrypt($message_key, $chat_id, $old, $encrypted_data) 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']); 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']);

View File

@ -36,9 +36,9 @@ trait BotAPI
* *
* @param string $text Text * @param string $text Text
* *
* @return int * @return float|int
*/ */
public static function mbStrlen(string $text): int public static function mbStrlen(string $text)
{ {
$length = 0; $length = 0;
$textlength = \strlen($text); $textlength = \strlen($text);
@ -107,7 +107,12 @@ trait BotAPI
} }
return $result; return $result;
} }
private function parseButtons($rows) /**
* @return ((bool|mixed|string)[][]|string)[][]
*
* @psalm-return array<int|int, array{_: string, buttons: array<int|int, array{_: string, text: mixed, same_peer?: bool, query?: mixed, data?: mixed, url?: mixed}>}>
*/
private function parseButtons($rows): array
{ {
$newrows = []; $newrows = [];
$key = 0; $key = 0;
@ -190,6 +195,7 @@ trait BotAPI
} }
return $newd; return $newd;
} }
$res = null;
switch ($data['_']) { switch ($data['_']) {
case 'updateShortSentMessage': case 'updateShortSentMessage':
$newd['message_id'] = $data['id']; $newd['message_id'] = $data['id'];
@ -679,7 +685,12 @@ trait BotAPI
} }
return $multiple_args; return $multiple_args;
} }
private function multipleExplodeKeepDelimiters($delimiters, $string) /**
* @return string[]
*
* @psalm-return list<string>
*/
private function multipleExplodeKeepDelimiters($delimiters, $string): array
{ {
$initialArray = \explode(\chr(1), \str_replace($delimiters, \chr(1), $string)); $initialArray = \explode(\chr(1), \str_replace($delimiters, \chr(1), $string));
$finalArray = []; $finalArray = [];
@ -693,7 +704,7 @@ trait BotAPI
} }
return $finalArray; return $finalArray;
} }
private function htmlFixtags($text) private function htmlFixtags($text): string
{ {
$diff = 0; $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); \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); return \htmlentities($text);
} }
private function buildRows($button_list) /**
* @return ((array|string)[][]|string)[]
*
* @psalm-return array{_: string, rows: list<array{_: string, buttons: list<mixed>}>}
*/
private function buildRows($button_list): array
{ {
$end = false; $end = false;
$rows = []; $rows = [];

View File

@ -90,6 +90,7 @@ trait BotAPIFiles
$photoSize = $fileId->hasPhotoSizeSource() ? $fileId->getPhotoSizeSource() : null; $photoSize = $fileId->hasPhotoSizeSource() ? $fileId->getPhotoSizeSource() : null;
$res = null;
switch ($fileId->getType()) { switch ($fileId->getType()) {
case PROFILE_PHOTO: case PROFILE_PHOTO:
/** /**

View File

@ -401,7 +401,9 @@ class TL
* @param string $ctx Context * @param string $ctx Context
* @param integer $layer Layer version * @param integer $layer Layer version
* *
* @return \Generator<string> * @return \Generator
*
* @psalm-return \Generator<int|mixed, array|mixed, mixed, false|mixed|null|string>
*/ */
public function serializeObject(array $type, $object, $ctx, int $layer = -1): \Generator public function serializeObject(array $type, $object, $ctx, int $layer = -1): \Generator
{ {
@ -581,7 +583,9 @@ class TL
* @param string $method Method name * @param string $method Method name
* @param mixed $arguments Arguments * @param mixed $arguments Arguments
* *
* @return \Generator<string> * @return \Generator
*
* @psalm-return \Generator<int|mixed, Promise|Promise<\Amp\File\File>|Promise<\Amp\Ipc\Sync\ChannelledSocket>|Promise<int>|Promise<mixed>|Promise<null|string>|Promise<string>|\danog\MadelineProto\Stream\StreamInterface|array|int|mixed, mixed, string>
*/ */
public function serializeMethod(string $method, $arguments): \Generator public function serializeMethod(string $method, $arguments): \Generator
{ {
@ -662,7 +666,9 @@ class TL
* @param string $ctx Context * @param string $ctx Context
* @param integer $layer Layer * @param integer $layer Layer
* *
* @return \Generator<string> * @return \Generator
*
* @psalm-return \Generator<int|mixed, Promise|Promise<\Amp\File\File>|Promise<\Amp\Ipc\Sync\ChannelledSocket>|Promise<int>|Promise<mixed>|Promise<null|string>|\danog\MadelineProto\Stream\StreamInterface|array|int|mixed, mixed, string>
*/ */
private function serializeParams(array $tl, $arguments, $ctx, int $layer = -1): \Generator private function serializeParams(array $tl, $arguments, $ctx, int $layer = -1): \Generator
{ {

View File

@ -86,7 +86,7 @@ trait AuthKeyHandler
* *
* @param mixed $user User * @param mixed $user User
* *
* @return void * @return \Generator
*/ */
public function requestCall($user): \Generator public function requestCall($user): \Generator
{ {
@ -276,7 +276,7 @@ trait AuthKeyHandler
* Discard call. * Discard call.
* *
* @param array $call Call * @param array $call Call
* @param string $reason Discard reason * @param array $reason
* @param array $rating Rating * @param array $rating Rating
* @param boolean $need_debug Need debug? * @param boolean $need_debug Need debug?
* *

View File

@ -33,7 +33,9 @@ trait DialogHandler
* *
* @param boolean $force Whether to refetch all dialogs ignoring cache * @param boolean $force Whether to refetch all dialogs ignoring cache
* *
* @return \Generator<array<array>> * @return \Generator
*
* @psalm-return \Generator<int, \Amp\Promise<bool>, mixed, list<mixed>>
*/ */
public function getDialogs(bool $force = true): \Generator public function getDialogs(bool $force = true): \Generator
{ {

View File

@ -202,7 +202,9 @@ trait Login
/** /**
* Export authorization. * Export authorization.
* *
* @return \Generator<array> * @return \Generator
*
* @psalm-return \Generator<mixed, array|bool, mixed, array{0: int|string, 1: string}>
*/ */
public function exportAuthorization(): \Generator public function exportAuthorization(): \Generator
{ {

View File

@ -94,7 +94,7 @@ trait Loop
* *
* @param callable|null $callback Async callable to run * @param callable|null $callback Async callable to run
* *
* @return mixed * @return \Generator
*/ */
public function loop($callback = null): \Generator public function loop($callback = null): \Generator
{ {