GroupProcessManager   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 94
c 0
b 0
f 0
wmc 9
lcom 1
cbo 1
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A addTask() 0 6 1
A addTaskGroup() 0 10 2
A tick() 0 18 4
A __call() 0 4 1
1
<?php
2
3
namespace AsyncPHP\Doorman\Manager;
4
5
use AsyncPHP\Doorman\Manager;
6
use AsyncPHP\Doorman\Task;
7
8
class GroupProcessManager implements Manager
9
{
10
    /**
11
     * @var Manager
12
     */
13
    protected $manager;
14
15
    /**
16
     * @var Task[][]
17
     */
18
    protected $waiting = array();
19
20
    /**
21
     * @var Task[][]
22
     */
23
    protected $queuing = array();
24
25
    /**
26
     * @param Manager $manager
27
     */
28
    public function __construct(Manager $manager)
29
    {
30
        $this->manager = $manager;
31
    }
32
33
    /**
34
     * @inheritdoc
35
     *
36
     * @param Task $task
37
     *
38
     * @return $this
39
     */
40
    public function addTask(Task $task)
41
    {
42
        array_push($this->waiting, array($task));
43
44
        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
    public function addTaskGroup(array $tasks)
55
    {
56
        foreach ($tasks as $task) {
57
            assert($task instanceof Task);
58
        }
59
60
        array_push($this->waiting, $tasks);
61
62
        return $this;
63
    }
64
65
    /**
66
     * @inheritdoc
67
     *
68
     * @return bool
69
     */
70
    public function tick()
71
    {
72
        if (!empty($this->queuing)) {
73
            $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
        if ($this->manager->tick()) {
77
            return true;
78
        }
79
80
        if (empty($this->waiting)) {
81
            return false;
82
        }
83
84
        $this->queuing = array_shift($this->waiting);
0 ignored issues
show
Documentation Bug introduced by
It seems like array_shift($this->waiting) can be null. However, the property $queuing is declared as array. Maybe change the type of the property to array|null or add a type check?

Our type inference engine has found an assignment of a scalar value (like a string, an integer or null) to a property which is an array.

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

To type hint that a parameter can be either an array or null, you can set a type hint of array and a default value of null. The PHP interpreter will then accept both an array or null for that parameter.

function aContainsB(array $needle = null, array  $haystack) {
    if (!$needle) {
        return false;
    }

    return array_intersect($haystack, $needle) == $haystack;
}

The function can be called with either null or an array for the parameter $needle but will only accept an array as $haystack.

Loading history...
85
86
        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(array($this->manager, $method), $parameters);
100
    }
101
}
102