Completed
Pull Request — master (#33)
by Viktor
01:18
created

GroupProcessManager::tick()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4.1755

Importance

Changes 0
Metric Value
dl 0
loc 18
ccs 7
cts 9
cp 0.7778
rs 9.6666
c 0
b 0
f 0
cc 4
nc 6
nop 0
crap 4.1755
1
<?php
2
3
namespace AsyncPHP\Doorman\Manager;
4
5
use AsyncPHP\Doorman\Manager;
6
use AsyncPHP\Doorman\Task;
7
8
final class GroupProcessManager implements Manager
9
{
10
    /**
11
     * @var Manager
12
     */
13
    private $manager;
14
15
    /**
16
     * @var Task[][]
17
     */
18
    private $waiting = [];
19
20
    /**
21
     * @var Task[][]
22
     */
23
    private $queuing = [];
24
25
    /**
26
     * @param Manager $manager
27
     */
28 1
    public function __construct(Manager $manager)
29
    {
30 1
        $this->manager = $manager;
31 1
    }
32
33
    /**
34
     * @inheritdoc
35
     *
36
     * @param Task $task
37
     *
38
     * @return $this
39
     */
40 1
    public function addTask(Task $task)
41
    {
42 1
        array_push($this->waiting, [$task]);
43
44 1
        return $this;
45
    }
46
47
    /**
48
     * Adds a group of tasks to be handled.
49
     *
50
     * @param Task[] $tasks
51
     *
52
     * @return $this
53
     */
54 1
    public function addTaskGroup(array $tasks)
55
    {
56 1
        foreach ($tasks as $task) {
57 1
            assert($task instanceof Task);
58
        }
59
60 1
        array_push($this->waiting, $tasks);
61
62 1
        return $this;
63
    }
64
65
    /**
66
     * @inheritdoc
67
     *
68
     * @return bool
69
     */
70 1
    public function tick()
71
    {
72 1
        if (!empty($this->queuing)) {
73 1
            $this->manager->addTask(array_shift($this->queuing));
0 ignored issues
show
Documentation introduced by
array_shift($this->queuing) is of type array<integer,object<AsyncPHP\Doorman\Task>>|null, but the function expects a object<AsyncPHP\Doorman\Task>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
74
        }
75
76 1
        if ($this->manager->tick()) {
77
            return true;
78
        }
79
80 1
        if (empty($this->waiting)) {
81
            return false;
82
        }
83
84 1
        $this->queuing = [array_shift($this->waiting)];
0 ignored issues
show
Documentation Bug introduced by
It seems like array(array_shift($this->waiting)) of type array<integer,array<inte...Doorman\\Task>>|null"}> is incompatible with the declared type array<integer,array<inte...syncPHP\Doorman\Task>>> of property $queuing.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
85
86 1
        return true;
87
    }
88
89
    /**
90
     * Passes missing method calls to the decorated manager.
91
     *
92
     * @param string $method
93
     * @param array $parameters
94
     *
95
     * @return mixed
96
     */
97
    public function __call($method, array $parameters)
98
    {
99
        return call_user_func_array([$this->manager, $method], $parameters);
100
    }
101
}
102