From c56a42e97697a6beebeb37f890cb23b83e5bb111 Mon Sep 17 00:00:00 2001 From: BoShurik Date: Tue, 9 Jul 2024 15:23:25 +0300 Subject: [PATCH] Psalm and type fixes --- CHANGELOG.md | 3 +- psalm.xml | 9 ++++++ src/BotApi.php | 7 +++-- src/Http/CurlHttpClient.php | 4 +-- src/Types/CallbackQuery.php | 6 ++-- src/Types/ChatBoostSource.php | 3 ++ src/Types/ChatBoostSourceGiveaway.php | 2 ++ src/Types/ChatMember.php | 6 +--- src/Types/ExternalReplyInfo.php | 40 -------------------------- src/Types/InputMedia/InputMedia.php | 2 ++ src/Types/MaybeInaccessibleMessage.php | 3 ++ src/Types/Message.php | 32 ++++++++++----------- src/Types/MessageOrigin.php | 20 +++++++++++++ src/Types/User.php | 4 +-- tests/Types/UserTest.php | 2 +- 15 files changed, 70 insertions(+), 73 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ca400033..3d24ae09 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,7 @@ All Notable changes to `PHP Telegram Bot Api` will be documented in this file -## 2.6.0 - YYYY-MM-DD +## 3.0.0 - YYYY-MM-DD - Add `\TelegramBot\Api\Types\Update::$myChatMember` field - Add `\TelegramBot\Api\Types\Update::$chatMember` field - Add `\TelegramBot\Api\Types\Update::$chatJoinRequest` field @@ -15,6 +15,7 @@ All Notable changes to `PHP Telegram Bot Api` will be documented in this file - Add support for local bot API server - Add method `\TelegramBot\Api\BotApi::validateWebAppData` to validate `window.Telegram.WebApp.initData` - Add `\TelegramBot\Api\Types\Message::$videoNote` field +- Drop php < 8.1 ## 2.5.0 - 2023-08-09 diff --git a/psalm.xml b/psalm.xml index 4cfcea27..238ab766 100644 --- a/psalm.xml +++ b/psalm.xml @@ -23,5 +23,14 @@ + + + + + + + + + diff --git a/src/BotApi.php b/src/BotApi.php index e1d472e1..6e5fd307 100644 --- a/src/BotApi.php +++ b/src/BotApi.php @@ -12,6 +12,7 @@ use TelegramBot\Api\Types\ArrayOfUpdates; use TelegramBot\Api\Types\BotCommand; use TelegramBot\Api\Types\Chat; +use TelegramBot\Api\Types\ChatFullInfo; use TelegramBot\Api\Types\ChatInviteLink; use TelegramBot\Api\Types\ChatMember; use TelegramBot\Api\Types\File; @@ -278,7 +279,7 @@ public function downloadFile($fileId) * * Response validation * - * @param resource $curl + * @param \CurlHandle $curl * @param string|false|null $response * * @throws HttpException @@ -2254,12 +2255,12 @@ public function unpinChatMessage($chatId, $messageId = null) * @param string|int $chatId Unique identifier for the target chat or username of the target channel * (in the format @channelusername) * - * @return Chat + * @return ChatFullInfo * @throws Exception */ public function getChat($chatId) { - return Chat::fromResponse($this->call('getChat', [ + return ChatFullInfo::fromResponse($this->call('getChat', [ 'chat_id' => $chatId ])); } diff --git a/src/Http/CurlHttpClient.php b/src/Http/CurlHttpClient.php index 6a8a6e8d..0df69e57 100644 --- a/src/Http/CurlHttpClient.php +++ b/src/Http/CurlHttpClient.php @@ -92,7 +92,7 @@ class CurlHttpClient extends AbstractHttpClient /** * CURL object * - * @var resource + * @var \CurlHandle */ private $curl; @@ -178,7 +178,7 @@ private static function jsonValidate($jsonString) } /** - * @param resource $curl + * @param \CurlHandle $curl * @param string|null $response * @return void * @throws HttpException diff --git a/src/Types/CallbackQuery.php b/src/Types/CallbackQuery.php index f2bbea3f..c3d946b0 100644 --- a/src/Types/CallbackQuery.php +++ b/src/Types/CallbackQuery.php @@ -58,7 +58,7 @@ class CallbackQuery extends BaseType * Note that message content and message date will not be available * if the message is too old * - * @var \TelegramBot\Api\Types\MaybeInaccessibleMessage|null + * @var Message|InaccessibleMessage|null */ protected $message; @@ -129,7 +129,7 @@ public function setFrom(User $from) } /** - * @return MaybeInaccessibleMessage|null + * @return Message|InaccessibleMessage|null */ public function getMessage() { @@ -137,7 +137,7 @@ public function getMessage() } /** - * @param MaybeInaccessibleMessage $message + * @param Message|InaccessibleMessage|null $message * @return void */ public function setMessage($message) diff --git a/src/Types/ChatBoostSource.php b/src/Types/ChatBoostSource.php index bb3a0917..d07c4221 100644 --- a/src/Types/ChatBoostSource.php +++ b/src/Types/ChatBoostSource.php @@ -31,6 +31,9 @@ class ChatBoostSource extends BaseType implements TypeInterface 'user' => User::class, ]; + /** + * @psalm-suppress LessSpecificReturnStatement,MoreSpecificReturnType + */ public static function fromResponse($data) { self::validate($data); diff --git a/src/Types/ChatBoostSourceGiveaway.php b/src/Types/ChatBoostSourceGiveaway.php index 941e45a8..39f00ad6 100644 --- a/src/Types/ChatBoostSourceGiveaway.php +++ b/src/Types/ChatBoostSourceGiveaway.php @@ -54,6 +54,8 @@ public static function fromResponse($data) protected $giveawayMessageId; /** + * @psalm-suppress NonInvariantDocblockPropertyType + * * Optional. User that won the prize in the giveaway if any * * @var User|null diff --git a/src/Types/ChatMember.php b/src/Types/ChatMember.php index f8df0915..5c1f69d2 100644 --- a/src/Types/ChatMember.php +++ b/src/Types/ChatMember.php @@ -16,11 +16,7 @@ abstract class ChatMember extends BaseType implements TypeInterface protected static $requiredParams = ['status', 'user']; /** - * Factory method to create a concrete ChatMember instance - * - * @param array $data - * @return ChatMember - * @throws InvalidArgumentException + * @psalm-suppress MoreSpecificReturnType,LessSpecificImplementedReturnType,LessSpecificReturnStatement */ public static function fromResponse($data) { diff --git a/src/Types/ExternalReplyInfo.php b/src/Types/ExternalReplyInfo.php index 0a760c82..3afe124b 100644 --- a/src/Types/ExternalReplyInfo.php +++ b/src/Types/ExternalReplyInfo.php @@ -604,43 +604,3 @@ public function setVenue($venue) $this->venue = $venue; } } - -/** - * Class ArrayOfPhotoSize - * Represents an array of PhotoSize objects. - * - * @package TelegramBot\Api\Types - */ -class ArrayOfPhotoSize extends BaseType implements TypeInterface -{ - /** - * {@inheritdoc} - * - * @var array - */ - protected static $map = [ - 'photos' => PhotoSize::class, - ]; - - /** - * @var array - */ - protected $photos; - - /** - * @return array - */ - public function getPhotos() - { - return $this->photos; - } - - /** - * @param array $photos - * @return void - */ - public function setPhotos($photos) - { - $this->photos = $photos; - } -} diff --git a/src/Types/InputMedia/InputMedia.php b/src/Types/InputMedia/InputMedia.php index e3f5bb75..e783a273 100644 --- a/src/Types/InputMedia/InputMedia.php +++ b/src/Types/InputMedia/InputMedia.php @@ -17,6 +17,8 @@ class InputMedia extends BaseType implements TypeInterface, CollectionItemInterface { /** + * @psalm-suppress LessSpecificImplementedReturnType + * * Factory method to create an instance of the appropriate InputMedia subclass based on the type. * * @param array $data diff --git a/src/Types/MaybeInaccessibleMessage.php b/src/Types/MaybeInaccessibleMessage.php index 505b07d7..e53b5b91 100644 --- a/src/Types/MaybeInaccessibleMessage.php +++ b/src/Types/MaybeInaccessibleMessage.php @@ -14,6 +14,9 @@ */ class MaybeInaccessibleMessage extends BaseType implements TypeInterface { + /** + * @psalm-suppress MoreSpecificReturnType,LessSpecificReturnStatement + */ public static function fromResponse($data) { self::validate($data); diff --git a/src/Types/Message.php b/src/Types/Message.php index 50377d70..0098262d 100644 --- a/src/Types/Message.php +++ b/src/Types/Message.php @@ -118,7 +118,7 @@ class Message extends BaseType implements TypeInterface /** * Unique message identifier inside this chat * - * @var int + * @var int|float */ protected $messageId; @@ -287,7 +287,7 @@ class Message extends BaseType implements TypeInterface /** * Optional. For text messages, special entities like usernames, URLs, bot commands, etc. that appear in the text * - * @var \TelegramBot\Api\Types\ArrayOfMessageEntity|null + * @var MessageEntity[]|null */ protected $entities; @@ -330,7 +330,7 @@ class Message extends BaseType implements TypeInterface /** * Optional. Message is a photo, available sizes of the photo * - * @var \TelegramBot\Api\Types\ArrayOfPhotoSize|null + * @var PhotoSize[]|null */ protected $photo; @@ -379,7 +379,7 @@ class Message extends BaseType implements TypeInterface /** * Optional. For messages with a caption, special entities like usernames, URLs, bot commands, etc. that appear in the caption * - * @var \TelegramBot\Api\Types\ArrayOfMessageEntity|null + * @var MessageEntity[]|null */ protected $captionEntities; @@ -444,7 +444,7 @@ class Message extends BaseType implements TypeInterface * Optional. New members that were added to the group or supergroup and information about them * (the bot itself may be one of these members) * - * @var \TelegramBot\Api\Types\ArrayOfUser|null + * @var User[]|null */ protected $newChatMembers; @@ -465,7 +465,7 @@ class Message extends BaseType implements TypeInterface /** * Optional. A chat photo was change to this value * - * @var \TelegramBot\Api\Types\ArrayOfPhotoSize|null + * @var PhotoSize[]|null */ protected $newChatPhoto; @@ -531,7 +531,7 @@ class Message extends BaseType implements TypeInterface /** * Optional. Specified message was pinned. Note that the Message object in this field will not contain further reply_to_message fields even if it itself is a reply. * - * @var \TelegramBot\Api\Types\MaybeInaccessibleMessage|null + * @var Message|InaccessibleMessage|null */ protected $pinnedMessage; @@ -578,12 +578,12 @@ class Message extends BaseType implements TypeInterface */ protected $writeAccessAllowed; - /** - * Optional. Telegram Passport data - * - * @var \TelegramBot\Api\Types\PassportData|null - */ - protected $passportData; + // /** + // * Optional. Telegram Passport data + // * + // * @var \TelegramBot\Api\Types\PassportData|null + // */ + // protected $passportData; /** * Optional. Service message. A user in the chat triggered another user's proximity alert while sharing Live Location @@ -719,7 +719,7 @@ class Message extends BaseType implements TypeInterface protected $replyMarkup; /** - * @return int + * @return int|float */ public function getMessageId() { @@ -1311,7 +1311,7 @@ public function setCaption($caption) } /** - * @return ArrayOfMessageEntity|null + * @return MessageEntity[]|null */ public function getCaptionEntities() { @@ -1319,7 +1319,7 @@ public function getCaptionEntities() } /** - * @param ArrayOfMessageEntity|null $captionEntities + * @param MessageEntity[]|null $captionEntities */ public function setCaptionEntities($captionEntities) { diff --git a/src/Types/MessageOrigin.php b/src/Types/MessageOrigin.php index 394bfce3..c7daa670 100644 --- a/src/Types/MessageOrigin.php +++ b/src/Types/MessageOrigin.php @@ -23,6 +23,26 @@ class MessageOrigin extends BaseType implements TypeInterface protected $type; protected $date; + /** + * @psalm-suppress MoreSpecificReturnType,LessSpecificReturnStatement + */ + public static function fromResponse($data) + { + self::validate($data); + $class = match ($data['type']) { + 'user' => MessageOriginUser::class, + 'hidden_user' => MessageOriginHiddenUser::class, + 'chat' => MessageOriginChat::class, + 'channel' => MessageOriginChannel::class, + default => MessageOrigin::class + }; + + $instance = new $class(); + $instance->map($data); + + return $instance; + } + public function getType() { return $this->type; diff --git a/src/Types/User.php b/src/Types/User.php index cd8faa72..0ea3ea55 100644 --- a/src/Types/User.php +++ b/src/Types/User.php @@ -125,7 +125,7 @@ class User extends BaseType implements TypeInterface */ protected $canConnectToBusiness; - public function getId(): int + public function getId(): int|float { return $this->id; } @@ -180,7 +180,7 @@ public function setLanguageCode($languageCode): void public function isBot(): bool { - return $this->isBot; + return (bool) $this->isBot; } public function setIsBot($isBot): void diff --git a/tests/Types/UserTest.php b/tests/Types/UserTest.php index 61a29f00..d024c374 100644 --- a/tests/Types/UserTest.php +++ b/tests/Types/UserTest.php @@ -55,7 +55,7 @@ protected function assertMinItem($item) $this->assertNull($item->getCanJoinGroups()); $this->assertNull($item->getCanReadAllGroupMessages()); $this->assertNull($item->getSupportsInlineQueries()); - $this->assertNull($item->isBot()); + $this->assertFalse($item->isBot()); } /**