AutoScan::doIteration()   B
last analyzed

Complexity

Conditions 6
Paths 2

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
dl 0
loc 24
rs 8.9137
c 0
b 0
f 0
nc 2
nop 0
1
<?php
2
namespace PHPDaemon\Clients\Redis;
3
4
use PHPDaemon\Core\CallbackWrapper;
5
6
/**
7
 * @package    NetworkClients
8
 * @subpackage RedisClient
9
 * @author     Efimenko Dmitriy <[email protected]>
10
 */
11
class AutoScan
12
{
13
    use \PHPDaemon\Traits\ClassWatchdog;
14
    use \PHPDaemon\Traits\StaticObjectWatchdog;
15
16
    protected $conn;
17
18
    protected $cmd;
19
20
    protected $cursor = 0;
21
22
    protected $args;
23
24
    protected $limit = false;
25
26
    protected $num = 0;
27
28
    protected $isFreeze = false;
29
30
    protected $cb;
31
32
    protected $cbEnd;
33
34
    /**
35
     * Constructor
36
     * @param  Pool $pool Redis pool or connection
37
     * @param  string $cmd Command
38
     * @param  array $args Arguments
39
     * @param  cllable $cbEnd Callback
0 ignored issues
show
Documentation introduced by
Should the type for parameter $cbEnd not be cllable|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
40
     * @param  integer $limit Limit
0 ignored issues
show
Documentation introduced by
Should the type for parameter $limit not be false|integer?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
41
     */
42
    public function __construct($pool, $cmd, $args = [], $cbEnd = null, $limit = false)
43
    {
44
        $this->conn = $pool;
45
        $this->cmd = $cmd;
46
        $this->args = empty($args) ? [] : $args;
47
        $this->limit = $limit;
0 ignored issues
show
Documentation Bug introduced by
It seems like $limit can also be of type integer. However, the property $limit is declared as type boolean. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
48
        if (is_numeric($this->args[0])) {
49
            array_shift($this->args);
50
        }
51
        for ($i = sizeof($this->args) - 1; $i >= 0; --$i) {
52
            $a = $this->args[$i];
53
            if ((is_array($a) || is_object($a)) && is_callable($a)) {
54
                $this->cb = CallbackWrapper::wrap($a);
55
                $this->args = array_slice($this->args, 0, $i);
56
                break;
57
            } elseif ($a !== null) {
58
                break;
59
            }
60
        }
61
        if ($cbEnd !== null) {
62
            $this->cbEnd = CallbackWrapper::wrap($cbEnd);
63
        }
64
        $this->doIteration();
65
    }
66
67
    protected function doIteration()
68
    {
69
        if ($this->isFreeze) {
70
            return;
71
        }
72
73
        $args = $this->args;
74
        array_unshift($args, $this->cursor);
75
        $args[] = function ($redis) {
76
            $this->conn = $redis;
77
            $this->cursor = $redis->result[0];
78
            $func = $this->cb;
79
            $func($redis);
80
81
            if (!is_numeric($redis->result[0]) || !$redis->result[0] || ($this->limit && ++$this->num > $this->limit)) {
82
                $func = $this->cbEnd;
83
                $func($redis, $this);
84
                return;
85
            }
86
            $this->doIteration();
87
        };
88
        $func = [$this->conn, $this->cmd];
89
        $func(... $args);
90
    }
91
92
    public function freeze()
93
    {
94
        $this->isFreeze = true;
95
    }
96
97
    public function run()
98
    {
99
        $this->isFreeze = false;
100
        $this->doIteration();
101
    }
102
103
    public function reset()
104
    {
105
        $this->num = 0;
106
        $this->isFreeze = false;
107
    }
108
}
109