Check scrutinizer findings and take more care of casting and typing.

parent a53eca3b
......@@ -1186,13 +1186,13 @@ class DB
* @param integer $chat_id
* @param string $inline_message_id
*
* @return array|bool Array containing TOTAL and CURRENT fields or false on invalid arguments
* @return array Array containing TOTAL and CURRENT fields or false on invalid arguments
* @throws TelegramException
*/
public static function getTelegramRequestCount($chat_id = null, $inline_message_id = null)
{
if (!self::isDbConnected()) {
return false;
return [];
}
try {
......
......@@ -4,5 +4,20 @@ namespace Longman\TelegramBot\Entities\InputMedia;
interface InputMedia
{
/**
* @return string Type of the result.
*/
public function getType();
/**
* @return string File to send.
*/
public function getMedia();
/**
* @param string $media File to send.
*
* @return string
*/
public function setMedia($media);
}
......@@ -54,7 +54,7 @@ class Keyboard extends Entity
/**
* Get the proper keyboard button class for this keyboard.
*
* @return KeyboardButton|InlineKeyboardButton
* @return string
*/
public function getKeyboardButtonClass()
{
......@@ -166,7 +166,7 @@ class Keyboard extends Entity
return $button;
}
if (!$this->isInlineKeyboard() || $button_class::couldBe($button)) {
if (!$this->isInlineKeyboard() || call_user_func([$button_class, 'couldBe'], $button)) {
return new $button_class($button);
}
......
......@@ -34,7 +34,7 @@ class UserProfilePhotos extends Entity
*
* This method overrides the default getPhotos method and returns a nice array
*
* @return PhotoSize[]
* @return PhotoSize[][]
*/
public function getPhotos()
{
......@@ -42,11 +42,9 @@ class UserProfilePhotos extends Entity
if ($these_photos = $this->getProperty('photos')) {
foreach ($these_photos as $photos) {
$new_photos = [];
foreach ($photos as $photo) {
$new_photos[] = new PhotoSize($photo);
}
$all_photos[] = $new_photos;
$all_photos[] = array_map(function ($photo) {
return new PhotoSize($photo);
}, $photos);
}
}
......
......@@ -377,7 +377,7 @@ class Request
}
// Reformat data array in multipart way if it contains a resource
$has_resource |= (is_resource($item) || $item instanceof Stream);
$has_resource = $has_resource || is_resource($item) || $item instanceof Stream;
$multipart[] = ['name' => $key, 'contents' => $item];
}
......@@ -541,7 +541,7 @@ class Request
return filesize($file_path) > 0;
} catch (RequestException $e) {
return ($e->getResponse()) ? (string) $e->getResponse()->getBody() : '';
return false;
} finally {
//Logging verbose debug output
TelegramLog::endDebugLogTempStream('Verbose HTTP File Download Request output:' . PHP_EOL . '%s' . PHP_EOL);
......@@ -829,7 +829,7 @@ class Request
$chat_id = isset($data['chat_id']) ? $data['chat_id'] : null;
$inline_message_id = isset($data['inline_message_id']) ? $data['inline_message_id'] : null;
if (($chat_id || $inline_message_id) && in_array($action, $limited_methods)) {
if (($chat_id || $inline_message_id) && in_array($action, $limited_methods, true)) {
$timeout = 60;
while (true) {
......@@ -837,18 +837,23 @@ class Request
throw new TelegramException('Timed out while waiting for a request spot!');
}
$requests = DB::getTelegramRequestCount($chat_id, $inline_message_id);
if (!($requests = DB::getTelegramRequestCount($chat_id, $inline_message_id))) {
break;
}
// Make sure we're handling integers here.
$requests = array_map('intval', $requests);
$chat_per_second = ($requests['LIMIT_PER_SEC'] == 0); // No more than one message per second inside a particular chat
$global_per_second = ($requests['LIMIT_PER_SEC_ALL'] < 30); // No more than 30 messages per second to different chats
$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
$chat_per_second = ($requests['LIMIT_PER_SEC'] === 0); // No more than one message per second inside a particular chat
$global_per_second = ($requests['LIMIT_PER_SEC_ALL'] < 30); // No more than 30 messages per second to different chats
$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
if ($chat_per_second && $global_per_second && $groups_per_minute) {
break;
}
$timeout--;
usleep(self::$limiter_interval * 1000000);
usleep((int) (self::$limiter_interval * 1000000));
}
DB::insertTelegramRequest($action, $data);
......
......@@ -344,9 +344,8 @@ class Telegram
if ($custom_input = $this->getCustomInput()) {
$response = new ServerResponse(json_decode($custom_input, true), $this->bot_username);
} else {
if (DB::isDbConnected()) {
if (DB::isDbConnected() && $last_update = DB::selectTelegramUpdate(1)) {
//Get last update id from the database
$last_update = DB::selectTelegramUpdate(1);
$last_update = reset($last_update);
$this->last_update_id = isset($last_update['id']) ? $last_update['id'] : null;
......
......@@ -147,7 +147,9 @@ class TelegramLog
if (!self::isDebugLogActive()) {
return false;
}
self::$debug_log_temp_stream_handle = fopen('php://temp', 'w+b');
if ($temp_stream_handle = fopen('php://temp', 'wb+')) {
self::$debug_log_temp_stream_handle = $temp_stream_handle;
}
}
return self::$debug_log_temp_stream_handle;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment