Passed
Push — main ( e1a2dc...e011f6 )
by Miaad
02:06
created

getUpdates   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 11
eloc 24
dl 0
loc 41
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A handleUpdates() 0 3 2
A deleteOldLocks() 0 9 4
A loadData() 0 11 2
A init() 0 8 3
1
<?php
2
3
namespace BPT\receiver;
4
5
use BPT\api\telegram;
6
use BPT\BPT;
7
use BPT\lock;
8
use BPT\settings;
9
use stdClass;
10
11
class getUpdates extends receiver {
12
    public static function init () {
13
        $last_update_id = self::loadData();
14
        while(true) {
15
            if (!lock::exist('getUpdate')) {
16
                $updates = telegram::getUpdates($last_update_id,allowed_updates: settings::$allowed_updates)->result;
0 ignored issues
show
Bug introduced by
The method getUpdates() does not exist on BPT\api\telegram. Since you implemented __callStatic, consider adding a @method annotation. ( Ignorable by Annotation )

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

16
                $updates = telegram::/** @scrutinizer ignore-call */ getUpdates($last_update_id,allowed_updates: settings::$allowed_updates)->result;
Loading history...
Bug introduced by
Are you sure the usage of BPT\api\telegram::getUpd...tings::allowed_updates) targeting BPT\api\telegram::__callStatic() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
17
                self::handleUpdates($updates);
18
                lock::save('getUpdate',BPT::$update->update_id+1);
19
                $last_update_id = BPT::$update->update_id+1;
20
            }
21
        }
22
    }
23
24
    private static function loadData(): bool|int|string {
25
        if (lock::exist('getUpdate')) {
26
            $last_update_id = lock::read('getUpdate');
27
        }
28
        else {
29
            self::deleteOldLocks();
30
            telegram::deleteWebhook();
0 ignored issues
show
Bug introduced by
The method deleteWebhook() does not exist on BPT\api\telegram. Since you implemented __callStatic, consider adding a @method annotation. ( Ignorable by Annotation )

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

30
            telegram::/** @scrutinizer ignore-call */ deleteWebhook();
Loading history...
31
            $last_update_id = 0;
32
            lock::save('getUpdate',0);
33
        }
34
        return $last_update_id;
35
    }
36
37
    private static function deleteOldLocks() {
38
        if (lock::exist('BPT-HOOK')) {
39
            lock::delete('BPT-HOOK');
40
        }
41
        if (lock::exist('BPT-MULTI-EXEC')) {
42
            lock::delete('BPT-MULTI-EXEC');
43
        }
44
        if (lock::exist('BPT-MULTI-CURL')) {
45
            lock::delete('BPT-MULTI-CURL');
46
        }
47
    }
48
49
    private static function handleUpdates(stdClass $updates) {
50
        foreach ($updates as $update) {
51
            receiver::processUpdate($update);
52
        }
53
    }
54
}