Passed
Push — main ( 723093...1f3d08 )
by Miaad
01:40
created

telegram::sendFile()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 12
c 1
b 0
f 0
nc 1
nop 14
dl 0
loc 13
rs 9.8666

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
namespace BPT\telegram;
4
use BPT\constants\fileTypes;
5
use BPT\tools;
6
use BPT\types\forceReply;
7
use BPT\types\inlineKeyboardMarkup;
8
use BPT\types\message;
9
use BPT\types\replyKeyboardMarkup;
10
use BPT\types\replyKeyboardRemove;
11
use BPT\types\responseError;
12
use stdClass;
13
14
/**
15
 * telegram class , Adding normal method call to request class and a simple name for being easy to call
16
 */
17
class telegram extends request {
18
    public function __call (string $name, array $arguments) {
19
        return request::$name(...$arguments);
20
    }
21
22
    /**
23
     * download telegram file with file_id to destination location
24
     *
25
     * It has 20MB download limit(same as telegram)
26
     *
27
     * e.g. => tools::downloadFile('test.mp4');
28
     *
29
     * e.g. => tools::downloadFile('test.mp4','file_id_asdadadadadadad);
30
     *
31
     * @param string|null $destination destination for save the file
32
     * @param string|null $file_id     file_id for download, if not set, will generate by request::catchFields method
33
     *
34
     * @return bool
35
     */
36
    public static function downloadFile (string|null $destination = null, string|null $file_id = null): bool {
37
        return tools::downloadFile(self::fileLink($file_id), $destination);
0 ignored issues
show
Bug introduced by
It seems like $destination can also be of type null; however, parameter $path of BPT\tools::downloadFile() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

37
        return tools::downloadFile(self::fileLink($file_id), /** @scrutinizer ignore-type */ $destination);
Loading history...
Bug introduced by
It seems like self::fileLink($file_id) can also be of type boolean; however, parameter $url of BPT\tools::downloadFile() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

37
        return tools::downloadFile(/** @scrutinizer ignore-type */ self::fileLink($file_id), $destination);
Loading history...
38
    }
39
40
    public static function fileLink(string|null $file_id = null): bool|string {
41
        $file = self::getFile($file_id);
42
        if (!isset($file->file_path)) {
43
            return false;
44
        }
45
        return $file->link();
46
    }
47
48
    /**
49
     * send file with only file_id
50
     *
51
     * e.g. => tools::sendFile('file_id_asdadsadadadadadada');
52
     *
53
     * e.g. => tools::sendFile('file_id_asdadsadadadadadada','hello');
54
     *
55
     * @param string          $file_id
56
     * @param int|string|null $chat_id
57
     * @param int|null        $message_thread_id default : null
58
     * @param string|null     $caption
59
     *
60
     * @return message|bool|responseError
61
     */
62
    public static function sendFile (string $file_id, int|string $chat_id = null, int $message_thread_id = null, string $caption = null, string $parse_mode = null, array $caption_entities = null, bool $disable_notification = null, bool $protect_content = null, int $reply_to_message_id = null, bool $allow_sending_without_reply = null, inlineKeyboardMarkup|replyKeyboardMarkup|replyKeyboardRemove|forceReply|stdClass|array $reply_markup = null, string $token = null, bool $forgot = null, bool $answer = null): message|bool|responseError {
63
        $type = tools::fileType($file_id);
64
        return match ($type) {
65
            fileTypes::VIDEO => self::sendVideo($file_id, $chat_id, null, null, null, null, $caption, $parse_mode, $caption_entities, null, $disable_notification, $protect_content, $reply_to_message_id, $allow_sending_without_reply, $reply_markup, $token, $forgot, $answer, $message_thread_id),
66
            fileTypes::VIDEO_NOTE => self::sendVideoNote($file_id, $chat_id, null, null, null, $disable_notification, $protect_content, $reply_to_message_id, $allow_sending_without_reply, $reply_markup, $token, $forgot, $answer, $message_thread_id),
67
            fileTypes::ANIMATION => self::sendAnimation($file_id, $chat_id, null, null, null, null, $caption, $parse_mode, $caption_entities, $disable_notification, $protect_content, $reply_to_message_id, $allow_sending_without_reply, $reply_markup, $token, $forgot, $answer, $message_thread_id),
68
            fileTypes::AUDIO => self::sendAudio($file_id, $chat_id, $caption, $parse_mode, $caption_entities, null, null, null, null, $disable_notification, $protect_content, $reply_to_message_id, $allow_sending_without_reply, $reply_markup, $token, $forgot, $answer, $message_thread_id),
69
            fileTypes::PHOTO,
70
            fileTypes::PROFILE_PHOTO => self::sendPhoto($file_id, $chat_id, $caption, $parse_mode, $caption_entities, $disable_notification, $protect_content, $reply_to_message_id, $allow_sending_without_reply, $reply_markup, $token, $forgot, $answer, $message_thread_id),
71
            fileTypes::VOICE => self::sendVoice($file_id, $chat_id, $caption, $parse_mode, $caption_entities, null, $disable_notification, $protect_content, $reply_to_message_id, $allow_sending_without_reply, $reply_markup, $token, $forgot, $answer, $message_thread_id),
72
            fileTypes::STICKER => self::sendSticker($file_id, $chat_id, $disable_notification, $protect_content, $reply_to_message_id, $allow_sending_without_reply, $reply_markup, $token, $forgot, $answer, $message_thread_id),
73
            fileTypes::DOCUMENT => self::sendDocument($file_id, $chat_id, null, $caption, $parse_mode, $caption_entities, null, $disable_notification, $protect_content, $reply_to_message_id, $allow_sending_without_reply, $reply_markup, $token, $forgot, $answer, $message_thread_id),
74
            default => false,
75
        };
76
    }
77
}