Skip to content

Commit

Permalink
Merge pull request #478 from BoShurik/new-type-fixes
Browse files Browse the repository at this point in the history
Psalm and type fixes
  • Loading branch information
BoShurik authored Jul 9, 2024
2 parents d11cf3a + c56a42e commit 3b44cb2
Show file tree
Hide file tree
Showing 15 changed files with 70 additions and 73 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down
9 changes: 9 additions & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,14 @@
<DeprecatedMethod errorLevel="suppress" />
<DeprecatedClass errorLevel="suppress" />
<DeprecatedProperty errorLevel="suppress" />

<MissingReturnType errorLevel="suppress" />
<MissingParamType errorLevel="suppress" />
<MissingPropertyType errorLevel="suppress" />

<RedundantConditionGivenDocblockType errorLevel="suppress" />
<DocblockTypeContradiction errorLevel="suppress" />

<RiskyTruthyFalsyComparison errorLevel="suppress" />
</issueHandlers>
</psalm>
7 changes: 4 additions & 3 deletions src/BotApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -278,7 +279,7 @@ public function downloadFile($fileId)
*
* Response validation
*
* @param resource $curl
* @param \CurlHandle $curl
* @param string|false|null $response
*
* @throws HttpException
Expand Down Expand Up @@ -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
]));
}
Expand Down
4 changes: 2 additions & 2 deletions src/Http/CurlHttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ class CurlHttpClient extends AbstractHttpClient
/**
* CURL object
*
* @var resource
* @var \CurlHandle
*/
private $curl;

Expand Down Expand Up @@ -178,7 +178,7 @@ private static function jsonValidate($jsonString)
}

/**
* @param resource $curl
* @param \CurlHandle $curl
* @param string|null $response
* @return void
* @throws HttpException
Expand Down
6 changes: 3 additions & 3 deletions src/Types/CallbackQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -129,15 +129,15 @@ public function setFrom(User $from)
}

/**
* @return MaybeInaccessibleMessage|null
* @return Message|InaccessibleMessage|null
*/
public function getMessage()
{
return $this->message;
}

/**
* @param MaybeInaccessibleMessage $message
* @param Message|InaccessibleMessage|null $message
* @return void
*/
public function setMessage($message)
Expand Down
3 changes: 3 additions & 0 deletions src/Types/ChatBoostSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions src/Types/ChatBoostSourceGiveaway.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 1 addition & 5 deletions src/Types/ChatMember.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
40 changes: 0 additions & 40 deletions src/Types/ExternalReplyInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
2 changes: 2 additions & 0 deletions src/Types/InputMedia/InputMedia.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions src/Types/MaybeInaccessibleMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
*/
class MaybeInaccessibleMessage extends BaseType implements TypeInterface
{
/**
* @psalm-suppress MoreSpecificReturnType,LessSpecificReturnStatement
*/
public static function fromResponse($data)
{
self::validate($data);
Expand Down
32 changes: 16 additions & 16 deletions src/Types/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ class Message extends BaseType implements TypeInterface
/**
* Unique message identifier inside this chat
*
* @var int
* @var int|float
*/
protected $messageId;

Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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;

Expand All @@ -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;

Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -719,7 +719,7 @@ class Message extends BaseType implements TypeInterface
protected $replyMarkup;

/**
* @return int
* @return int|float
*/
public function getMessageId()
{
Expand Down Expand Up @@ -1311,15 +1311,15 @@ public function setCaption($caption)
}

/**
* @return ArrayOfMessageEntity|null
* @return MessageEntity[]|null
*/
public function getCaptionEntities()
{
return $this->captionEntities;
}

/**
* @param ArrayOfMessageEntity|null $captionEntities
* @param MessageEntity[]|null $captionEntities
*/
public function setCaptionEntities($captionEntities)
{
Expand Down
20 changes: 20 additions & 0 deletions src/Types/MessageOrigin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/Types/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ class User extends BaseType implements TypeInterface
*/
protected $canConnectToBusiness;

public function getId(): int
public function getId(): int|float
{
return $this->id;
}
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tests/Types/UserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

/**
Expand Down

0 comments on commit 3b44cb2

Please sign in to comment.