Passed
Push — main ( 23165f...4af278 )
by Miaad
01:25
created

settings::init()   B

Complexity

Conditions 11
Paths 16

Size

Total Lines 32
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 20
dl 0
loc 32
rs 7.3166
c 0
b 0
f 0
cc 11
nc 16
nop 1

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace BPT;
4
5
use BPT\constants\receiver;
6
use BPT\db\db;
7
use BPT\receiver\getUpdates;
8
use BPT\receiver\webhook;
9
use CURLFile;
10
use Error;
11
use mysqli;
12
use stdClass;
13
use TypeError;
14
15
class settings {
16
    public static string $token = '';
17
18
    //public static bool $auto_update = true;
19
20
    public static bool $logger = true;
21
22
    public static int $log_size = 10;
23
24
    public static string|CURLFile|null $certificate = null;
25
26
    public static bool $handler = true;
27
28
    public static bool $security = false;
29
30
    public static bool $secure_folder = false;
31
32
    //public static bool $array_update = false;
33
34
    public static bool $split_update = true;
35
36
    public static bool $multi = false;
37
38
    //public static bool $debug = false;
39
40
    public static bool $telegram_verify = true;
41
42
    public static int $max_connection = 40;
43
44
    public static string $base_url = 'https://api.telegram.org/bot';
45
46
    public static string $down_url = 'https://api.telegram.org/file/bot';
47
48
    public static int $forgot_time = 100;
49
50
    public static string $receiver = receiver::WEBHOOK;
51
52
    public static array $allowed_updates = ['message', 'edited_channel_post', 'callback_query', 'inline_query'];
53
54
    public static array|mysqli|null $db = ['type' => 'json', 'file_name' => 'BPT-DB.json'];
55
56
57
    public static function init (array|stdClass $settings) {
58
        $settings = (array) $settings;
59
60
        if (!(isset($settings['logger']) && $settings['logger'] == false)) {
61
            logger::init(isset($settings['log_size']) && is_numeric($settings['log_size']) ? $settings['log_size'] : self::$log_size);
62
        }
63
64
        foreach ($settings as $setting => $value) {
65
            try{
66
                self::$$setting = $value;
67
            }
68
            catch (TypeError){
1 ignored issue
show
Unused Code introduced by
catch (\TypeError) is not reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
69
                logger::write("$setting setting has wrong type , its set to default value",'warning');
70
            }
71
            catch (Error){
72
                logger::write("$setting setting is not one of library settings",'warning');
73
            }
74
        }
75
76
        if (self::$token !== '') {
77
            if (tools::isToken(self::$token)) {
78
                self::security();
79
                self::secureFolder();
80
                self::db();
81
                self::$receiver !== receiver::GETUPDATES ? self::webhook() : self::getUpdates();
82
            }
83
            else {
84
                logger::write('token format is not right, check it and try again','error');
85
            }
86
        }
87
        else {
88
            logger::write('You must specify token parameter in settings','error');
89
        }
90
    }
91
92
    private static function security() {
93
        if (self::$security) {
94
            ini_set('magic_quotes_gpc', 'off');
95
            ini_set('sql.safe_mode', 'on');
96
            ini_set('max_execution_time', 30);
97
            ini_set('max_input_time', 30);
98
            ini_set('memory_limit', '20M');
99
            ini_set('post_max_size', '8K');
100
            ini_set('expose_php', 'off');
101
            ini_set('file_uploads', 'off');
102
            ini_set('display_errors', 0);
103
            ini_set('error_reporting', 0);
104
        }
105
    }
106
107
    private static function secureFolder() {
108
        if (self::$secure_folder) {
109
            $address = explode('/', $_SERVER['REQUEST_URI']);
110
            unset($address[count($address) - 1]);
111
            $address = implode('/', $address) . '/BPT.php';
112
            $text = "ErrorDocument 404 $address\nErrorDocument 403 $address\n Options -Indexes\n  Order Deny,Allow\nDeny from all\nAllow from 127.0.0.1\n<Files *.php>\n    Order Allow,Deny\n    Allow from all\n</Files>";
113
            if (!file_exists('.htaccess') || filesize('.htaccess') != strlen($text)) {
114
                file_put_contents('.htaccess', $text);
115
            }
116
        }
117
    }
118
119
    private static function db() {
120
        if (!empty(self::$db)) {
121
            db::init(self::$db);
122
        }
123
    }
124
125
    private static function getUpdates() {
126
        if (self::$handler) {
127
            getUpdates::init();
128
        }
129
        else {
130
            logger::write('You can\'t use getUpdates receiver when handler is off , use webhook or use handler','error');
131
        }
132
    }
133
134
    private static function webhook() {
135
        webhook::init();
136
    }
137
}
138