Skip to content

Commit fe01d1a

Browse files
committed
Check scrutinizer findings and take more care of casting and typing.
1 parent a53eca3 commit fe01d1a

File tree

6 files changed

+32
-18
lines changed

6 files changed

+32
-18
lines changed

src/Entities/InputMedia/InputMedia.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,20 @@
44

55
interface InputMedia
66
{
7+
/**
8+
* @return string Type of the result.
9+
*/
10+
public function getType();
711

12+
/**
13+
* @return string File to send.
14+
*/
15+
public function getMedia();
16+
17+
/**
18+
* @param string $media File to send.
19+
*
20+
* @return string
21+
*/
22+
public function setMedia($media);
823
}

src/Entities/Keyboard.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public function isInlineKeyboard()
5454
/**
5555
* Get the proper keyboard button class for this keyboard.
5656
*
57-
* @return KeyboardButton|InlineKeyboardButton
57+
* @return string
5858
*/
5959
public function getKeyboardButtonClass()
6060
{
@@ -166,7 +166,7 @@ protected function parseButton($button)
166166
return $button;
167167
}
168168

169-
if (!$this->isInlineKeyboard() || $button_class::couldBe($button)) {
169+
if (!$this->isInlineKeyboard() || call_user_func([$button_class, 'couldBe'], $button)) {
170170
return new $button_class($button);
171171
}
172172

src/Entities/UserProfilePhotos.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,19 +34,17 @@ protected function subEntities()
3434
*
3535
* This method overrides the default getPhotos method and returns a nice array
3636
*
37-
* @return PhotoSize[]
37+
* @return PhotoSize[][]
3838
*/
3939
public function getPhotos()
4040
{
4141
$all_photos = [];
4242

4343
if ($these_photos = $this->getProperty('photos')) {
4444
foreach ($these_photos as $photos) {
45-
$new_photos = [];
46-
foreach ($photos as $photo) {
47-
$new_photos[] = new PhotoSize($photo);
48-
}
49-
$all_photos[] = $new_photos;
45+
$all_photos[] = array_map(function ($photo) {
46+
return new PhotoSize($photo);
47+
}, $photos);
5048
}
5149
}
5250

src/Request.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,7 @@ private static function setUpRequestParams(array $data)
377377
}
378378

379379
// Reformat data array in multipart way if it contains a resource
380-
$has_resource |= (is_resource($item) || $item instanceof Stream);
380+
$has_resource = $has_resource || is_resource($item) || $item instanceof Stream;
381381
$multipart[] = ['name' => $key, 'contents' => $item];
382382
}
383383

@@ -541,7 +541,7 @@ public static function downloadFile(File $file)
541541

542542
return filesize($file_path) > 0;
543543
} catch (RequestException $e) {
544-
return ($e->getResponse()) ? (string) $e->getResponse()->getBody() : '';
544+
return false;
545545
} finally {
546546
//Logging verbose debug output
547547
TelegramLog::endDebugLogTempStream('Verbose HTTP File Download Request output:' . PHP_EOL . '%s' . PHP_EOL);
@@ -829,26 +829,26 @@ private static function limitTelegramRequests($action, array $data = [])
829829
$chat_id = isset($data['chat_id']) ? $data['chat_id'] : null;
830830
$inline_message_id = isset($data['inline_message_id']) ? $data['inline_message_id'] : null;
831831

832-
if (($chat_id || $inline_message_id) && in_array($action, $limited_methods)) {
832+
if (($chat_id || $inline_message_id) && in_array($action, $limited_methods, true)) {
833833
$timeout = 60;
834834

835835
while (true) {
836836
if ($timeout <= 0) {
837837
throw new TelegramException('Timed out while waiting for a request spot!');
838838
}
839839

840-
$requests = DB::getTelegramRequestCount($chat_id, $inline_message_id);
840+
$requests = array_map('intval', DB::getTelegramRequestCount($chat_id, $inline_message_id));
841841

842-
$chat_per_second = ($requests['LIMIT_PER_SEC'] == 0); // No more than one message per second inside a particular chat
842+
$chat_per_second = ($requests['LIMIT_PER_SEC'] === 0); // No more than one message per second inside a particular chat
843843
$global_per_second = ($requests['LIMIT_PER_SEC_ALL'] < 30); // No more than 30 messages per second to different chats
844-
$groups_per_minute = (((is_numeric($chat_id) && $chat_id > 0) || !is_null($inline_message_id)) || ((!is_numeric($chat_id) || $chat_id < 0) && $requests['LIMIT_PER_MINUTE'] < 20)); // No more than 20 messages per minute in groups and channels
844+
$groups_per_minute = (((is_numeric($chat_id) && $chat_id > 0) || $inline_message_id !== null) || ((!is_numeric($chat_id) || $chat_id < 0) && $requests['LIMIT_PER_MINUTE'] < 20)); // No more than 20 messages per minute in groups and channels
845845

846846
if ($chat_per_second && $global_per_second && $groups_per_minute) {
847847
break;
848848
}
849849

850850
$timeout--;
851-
usleep(self::$limiter_interval * 1000000);
851+
usleep((int) (self::$limiter_interval * 1000000));
852852
}
853853

854854
DB::insertTelegramRequest($action, $data);

src/Telegram.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -344,9 +344,8 @@ public function handleGetUpdates($limit = null, $timeout = null)
344344
if ($custom_input = $this->getCustomInput()) {
345345
$response = new ServerResponse(json_decode($custom_input, true), $this->bot_username);
346346
} else {
347-
if (DB::isDbConnected()) {
347+
if (DB::isDbConnected() && $last_update = DB::selectTelegramUpdate(1)) {
348348
//Get last update id from the database
349-
$last_update = DB::selectTelegramUpdate(1);
350349
$last_update = reset($last_update);
351350

352351
$this->last_update_id = isset($last_update['id']) ? $last_update['id'] : null;

src/TelegramLog.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,9 @@ public static function getDebugLogTempStream()
147147
if (!self::isDebugLogActive()) {
148148
return false;
149149
}
150-
self::$debug_log_temp_stream_handle = fopen('php://temp', 'w+b');
150+
if ($temp_stream_handle = fopen('php://temp', 'wb+')) {
151+
self::$debug_log_temp_stream_handle = $temp_stream_handle;
152+
}
151153
}
152154

153155
return self::$debug_log_temp_stream_handle;

0 commit comments

Comments
 (0)