Completed
Push — mater ( 02259a )
by Vasily
05:33
created

AutoScan::run()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
namespace PHPDaemon\Clients\Redis;
3
4
use PHPDaemon\Core\Daemon;
5
use PHPDaemon\Core\Debug;
6
use PHPDaemon\Core\CallbackWrapper;
7
8
/**
9
 * @package    NetworkClients
10
 * @subpackage RedisClient
11
 * @author     Efimenko Dmitriy <[email protected]>
12
 */
13
class AutoScan {
14
	use \PHPDaemon\Traits\ClassWatchdog;
15
	use \PHPDaemon\Traits\StaticObjectWatchdog;
16
17
	protected $conn;
18
19
	protected $cmd;
20
21
	protected $cursor = 0;
22
23
	protected $args;
24
25
	protected $limit = false;
26
27
	protected $num = 0;
28
29
	protected $isFreeze = false;
30
31
	protected $cb;
32
33
	protected $cbEnd;
34
35
	/**
36
	 * Constructor
37
	 * @param  Pool    $pool   Redis pool or connection
38
	 * @param  string  $cmd    Command
39
	 * @param  array   $args   Arguments
40
	 * @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...
41
	 * @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...
42
	 */
43
	public function __construct($pool, $cmd, $args = [], $cbEnd = null, $limit = false) {
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
			}
58
			elseif ($a !== null) {
59
				break;
60
			}
61
		}
62
		if ($cbEnd !== null) {
63
			$this->cbEnd = CallbackWrapper::wrap($cbEnd);
64
		}
65
		$this->doIteration();
66
	}
67
68
	public function freeze() {
69
		$this->isFreeze = true;
70
	}
71
72
	public function run() {
73
		$this->isFreeze = false;
74
		$this->doIteration();
75
	}
76
77
	public function reset() {
78
		$this->num = 0;
79
		$this->isFreeze = false;
80
	}
81
82
	protected function doIteration() {
83
		if ($this->isFreeze) {
84
			return;
85
		}
86
87
		$args = $this->args;
88
		array_unshift($args, $this->cursor);
89
		$args[] = function($redis) {
90
			$this->conn   = $redis;
91
			$this->cursor = $redis->result[0];
92
			call_user_func($this->cb, $redis);
93
			
94
			if (!is_numeric($redis->result[0]) || !$redis->result[0] || ($this->limit && ++$this->num > $this->limit)) {
95
				call_user_func($this->cbEnd, $redis, $this);
96
				return;
97
			}
98
			$this->doIteration();
99
		};
100
		call_user_func_array([$this->conn, $this->cmd], $args);
101
	}
102
}
103