Skip to content

Commit c56a42e

Browse files
committed
Psalm and type fixes
1 parent d11cf3a commit c56a42e

15 files changed

+70
-73
lines changed

CHANGELOG.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
All Notable changes to `PHP Telegram Bot Api` will be documented in this file
44

5-
## 2.6.0 - YYYY-MM-DD
5+
## 3.0.0 - YYYY-MM-DD
66
- Add `\TelegramBot\Api\Types\Update::$myChatMember` field
77
- Add `\TelegramBot\Api\Types\Update::$chatMember` field
88
- 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
1515
- Add support for local bot API server
1616
- Add method `\TelegramBot\Api\BotApi::validateWebAppData` to validate `window.Telegram.WebApp.initData`
1717
- Add `\TelegramBot\Api\Types\Message::$videoNote` field
18+
- Drop php < 8.1
1819

1920
## 2.5.0 - 2023-08-09
2021

psalm.xml

+9
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,14 @@
2323
<DeprecatedMethod errorLevel="suppress" />
2424
<DeprecatedClass errorLevel="suppress" />
2525
<DeprecatedProperty errorLevel="suppress" />
26+
27+
<MissingReturnType errorLevel="suppress" />
28+
<MissingParamType errorLevel="suppress" />
29+
<MissingPropertyType errorLevel="suppress" />
30+
31+
<RedundantConditionGivenDocblockType errorLevel="suppress" />
32+
<DocblockTypeContradiction errorLevel="suppress" />
33+
34+
<RiskyTruthyFalsyComparison errorLevel="suppress" />
2635
</issueHandlers>
2736
</psalm>

src/BotApi.php

+4-3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use TelegramBot\Api\Types\ArrayOfUpdates;
1313
use TelegramBot\Api\Types\BotCommand;
1414
use TelegramBot\Api\Types\Chat;
15+
use TelegramBot\Api\Types\ChatFullInfo;
1516
use TelegramBot\Api\Types\ChatInviteLink;
1617
use TelegramBot\Api\Types\ChatMember;
1718
use TelegramBot\Api\Types\File;
@@ -278,7 +279,7 @@ public function downloadFile($fileId)
278279
*
279280
* Response validation
280281
*
281-
* @param resource $curl
282+
* @param \CurlHandle $curl
282283
* @param string|false|null $response
283284
*
284285
* @throws HttpException
@@ -2254,12 +2255,12 @@ public function unpinChatMessage($chatId, $messageId = null)
22542255
* @param string|int $chatId Unique identifier for the target chat or username of the target channel
22552256
* (in the format @channelusername)
22562257
*
2257-
* @return Chat
2258+
* @return ChatFullInfo
22582259
* @throws Exception
22592260
*/
22602261
public function getChat($chatId)
22612262
{
2262-
return Chat::fromResponse($this->call('getChat', [
2263+
return ChatFullInfo::fromResponse($this->call('getChat', [
22632264
'chat_id' => $chatId
22642265
]));
22652266
}

src/Http/CurlHttpClient.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ class CurlHttpClient extends AbstractHttpClient
9292
/**
9393
* CURL object
9494
*
95-
* @var resource
95+
* @var \CurlHandle
9696
*/
9797
private $curl;
9898

@@ -178,7 +178,7 @@ private static function jsonValidate($jsonString)
178178
}
179179

180180
/**
181-
* @param resource $curl
181+
* @param \CurlHandle $curl
182182
* @param string|null $response
183183
* @return void
184184
* @throws HttpException

src/Types/CallbackQuery.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ class CallbackQuery extends BaseType
5858
* Note that message content and message date will not be available
5959
* if the message is too old
6060
*
61-
* @var \TelegramBot\Api\Types\MaybeInaccessibleMessage|null
61+
* @var Message|InaccessibleMessage|null
6262
*/
6363
protected $message;
6464

@@ -129,15 +129,15 @@ public function setFrom(User $from)
129129
}
130130

131131
/**
132-
* @return MaybeInaccessibleMessage|null
132+
* @return Message|InaccessibleMessage|null
133133
*/
134134
public function getMessage()
135135
{
136136
return $this->message;
137137
}
138138

139139
/**
140-
* @param MaybeInaccessibleMessage $message
140+
* @param Message|InaccessibleMessage|null $message
141141
* @return void
142142
*/
143143
public function setMessage($message)

src/Types/ChatBoostSource.php

+3
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ class ChatBoostSource extends BaseType implements TypeInterface
3131
'user' => User::class,
3232
];
3333

34+
/**
35+
* @psalm-suppress LessSpecificReturnStatement,MoreSpecificReturnType
36+
*/
3437
public static function fromResponse($data)
3538
{
3639
self::validate($data);

src/Types/ChatBoostSourceGiveaway.php

+2
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ public static function fromResponse($data)
5454
protected $giveawayMessageId;
5555

5656
/**
57+
* @psalm-suppress NonInvariantDocblockPropertyType
58+
*
5759
* Optional. User that won the prize in the giveaway if any
5860
*
5961
* @var User|null

src/Types/ChatMember.php

+1-5
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,7 @@ abstract class ChatMember extends BaseType implements TypeInterface
1616
protected static $requiredParams = ['status', 'user'];
1717

1818
/**
19-
* Factory method to create a concrete ChatMember instance
20-
*
21-
* @param array $data
22-
* @return ChatMember
23-
* @throws InvalidArgumentException
19+
* @psalm-suppress MoreSpecificReturnType,LessSpecificImplementedReturnType,LessSpecificReturnStatement
2420
*/
2521
public static function fromResponse($data)
2622
{

src/Types/ExternalReplyInfo.php

-40
Original file line numberDiff line numberDiff line change
@@ -604,43 +604,3 @@ public function setVenue($venue)
604604
$this->venue = $venue;
605605
}
606606
}
607-
608-
/**
609-
* Class ArrayOfPhotoSize
610-
* Represents an array of PhotoSize objects.
611-
*
612-
* @package TelegramBot\Api\Types
613-
*/
614-
class ArrayOfPhotoSize extends BaseType implements TypeInterface
615-
{
616-
/**
617-
* {@inheritdoc}
618-
*
619-
* @var array
620-
*/
621-
protected static $map = [
622-
'photos' => PhotoSize::class,
623-
];
624-
625-
/**
626-
* @var array
627-
*/
628-
protected $photos;
629-
630-
/**
631-
* @return array
632-
*/
633-
public function getPhotos()
634-
{
635-
return $this->photos;
636-
}
637-
638-
/**
639-
* @param array $photos
640-
* @return void
641-
*/
642-
public function setPhotos($photos)
643-
{
644-
$this->photos = $photos;
645-
}
646-
}

src/Types/InputMedia/InputMedia.php

+2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
class InputMedia extends BaseType implements TypeInterface, CollectionItemInterface
1818
{
1919
/**
20+
* @psalm-suppress LessSpecificImplementedReturnType
21+
*
2022
* Factory method to create an instance of the appropriate InputMedia subclass based on the type.
2123
*
2224
* @param array $data

src/Types/MaybeInaccessibleMessage.php

+3
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
*/
1515
class MaybeInaccessibleMessage extends BaseType implements TypeInterface
1616
{
17+
/**
18+
* @psalm-suppress MoreSpecificReturnType,LessSpecificReturnStatement
19+
*/
1720
public static function fromResponse($data)
1821
{
1922
self::validate($data);

src/Types/Message.php

+16-16
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ class Message extends BaseType implements TypeInterface
118118
/**
119119
* Unique message identifier inside this chat
120120
*
121-
* @var int
121+
* @var int|float
122122
*/
123123
protected $messageId;
124124

@@ -287,7 +287,7 @@ class Message extends BaseType implements TypeInterface
287287
/**
288288
* Optional. For text messages, special entities like usernames, URLs, bot commands, etc. that appear in the text
289289
*
290-
* @var \TelegramBot\Api\Types\ArrayOfMessageEntity|null
290+
* @var MessageEntity[]|null
291291
*/
292292
protected $entities;
293293

@@ -330,7 +330,7 @@ class Message extends BaseType implements TypeInterface
330330
/**
331331
* Optional. Message is a photo, available sizes of the photo
332332
*
333-
* @var \TelegramBot\Api\Types\ArrayOfPhotoSize|null
333+
* @var PhotoSize[]|null
334334
*/
335335
protected $photo;
336336

@@ -379,7 +379,7 @@ class Message extends BaseType implements TypeInterface
379379
/**
380380
* Optional. For messages with a caption, special entities like usernames, URLs, bot commands, etc. that appear in the caption
381381
*
382-
* @var \TelegramBot\Api\Types\ArrayOfMessageEntity|null
382+
* @var MessageEntity[]|null
383383
*/
384384
protected $captionEntities;
385385

@@ -444,7 +444,7 @@ class Message extends BaseType implements TypeInterface
444444
* Optional. New members that were added to the group or supergroup and information about them
445445
* (the bot itself may be one of these members)
446446
*
447-
* @var \TelegramBot\Api\Types\ArrayOfUser|null
447+
* @var User[]|null
448448
*/
449449
protected $newChatMembers;
450450

@@ -465,7 +465,7 @@ class Message extends BaseType implements TypeInterface
465465
/**
466466
* Optional. A chat photo was change to this value
467467
*
468-
* @var \TelegramBot\Api\Types\ArrayOfPhotoSize|null
468+
* @var PhotoSize[]|null
469469
*/
470470
protected $newChatPhoto;
471471

@@ -531,7 +531,7 @@ class Message extends BaseType implements TypeInterface
531531
/**
532532
* 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.
533533
*
534-
* @var \TelegramBot\Api\Types\MaybeInaccessibleMessage|null
534+
* @var Message|InaccessibleMessage|null
535535
*/
536536
protected $pinnedMessage;
537537

@@ -578,12 +578,12 @@ class Message extends BaseType implements TypeInterface
578578
*/
579579
protected $writeAccessAllowed;
580580

581-
/**
582-
* Optional. Telegram Passport data
583-
*
584-
* @var \TelegramBot\Api\Types\PassportData|null
585-
*/
586-
protected $passportData;
581+
// /**
582+
// * Optional. Telegram Passport data
583+
// *
584+
// * @var \TelegramBot\Api\Types\PassportData|null
585+
// */
586+
// protected $passportData;
587587

588588
/**
589589
* 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
719719
protected $replyMarkup;
720720

721721
/**
722-
* @return int
722+
* @return int|float
723723
*/
724724
public function getMessageId()
725725
{
@@ -1311,15 +1311,15 @@ public function setCaption($caption)
13111311
}
13121312

13131313
/**
1314-
* @return ArrayOfMessageEntity|null
1314+
* @return MessageEntity[]|null
13151315
*/
13161316
public function getCaptionEntities()
13171317
{
13181318
return $this->captionEntities;
13191319
}
13201320

13211321
/**
1322-
* @param ArrayOfMessageEntity|null $captionEntities
1322+
* @param MessageEntity[]|null $captionEntities
13231323
*/
13241324
public function setCaptionEntities($captionEntities)
13251325
{

src/Types/MessageOrigin.php

+20
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,26 @@ class MessageOrigin extends BaseType implements TypeInterface
2323
protected $type;
2424
protected $date;
2525

26+
/**
27+
* @psalm-suppress MoreSpecificReturnType,LessSpecificReturnStatement
28+
*/
29+
public static function fromResponse($data)
30+
{
31+
self::validate($data);
32+
$class = match ($data['type']) {
33+
'user' => MessageOriginUser::class,
34+
'hidden_user' => MessageOriginHiddenUser::class,
35+
'chat' => MessageOriginChat::class,
36+
'channel' => MessageOriginChannel::class,
37+
default => MessageOrigin::class
38+
};
39+
40+
$instance = new $class();
41+
$instance->map($data);
42+
43+
return $instance;
44+
}
45+
2646
public function getType()
2747
{
2848
return $this->type;

src/Types/User.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ class User extends BaseType implements TypeInterface
125125
*/
126126
protected $canConnectToBusiness;
127127

128-
public function getId(): int
128+
public function getId(): int|float
129129
{
130130
return $this->id;
131131
}
@@ -180,7 +180,7 @@ public function setLanguageCode($languageCode): void
180180

181181
public function isBot(): bool
182182
{
183-
return $this->isBot;
183+
return (bool) $this->isBot;
184184
}
185185

186186
public function setIsBot($isBot): void

tests/Types/UserTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ protected function assertMinItem($item)
5555
$this->assertNull($item->getCanJoinGroups());
5656
$this->assertNull($item->getCanReadAllGroupMessages());
5757
$this->assertNull($item->getSupportsInlineQueries());
58-
$this->assertNull($item->isBot());
58+
$this->assertFalse($item->isBot());
5959
}
6060

6161
/**

0 commit comments

Comments
 (0)