Completed
Pull Request — master (#239)
by Дмитрий
03:46
created

StackCallbacks::executeAll()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 4
eloc 10
nc 4
nop 1
dl 0
loc 18
rs 9.2
1
<?php
2
namespace PHPDaemon\Structures;
3
4
use PHPDaemon\Core\CallbackWrapper;
5
6
/**
7
 * StackCallbacks
8
 * @package PHPDaemon\Structures
9
 * @author  Vasily Zorin <[email protected]>
10
 */
11
class StackCallbacks extends \SplStack
12
{
13
    use \PHPDaemon\Traits\ClassWatchdog;
14
    use \PHPDaemon\Traits\StaticObjectWatchdog;
15
16
    /**
17
     * Push callback to the bottom of stack
18
     * @param  callable $cb Callback
19
     * @return void
20
     */
21
    public function push($cb)
22
    {
23
        parent::push(CallbackWrapper::wrap($cb));
24
    }
25
26
    /**
27
     * Push callback to the top of stack
28
     * @param  callable $cb Callback
29
     * @return void
30
     */
31
    public function unshift($cb)
32
    {
33
        parent::unshift(CallbackWrapper::wrap($cb));
34
    }
35
36
    /**
37
     * Executes one callback from the top with given arguments
38
     * @param  mixed   ...$args Arguments
39
     * @return boolean
40
     */
41 View Code Duplication
    public function executeOne()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
42
    {
43
        if ($this->isEmpty()) {
44
            return false;
45
        }
46
        $cb = $this->shift();
47
        if ($cb) {
48
            $cb(...func_get_args());
49
            if ($cb instanceof CallbackWrapper) {
50
                $cb->cancel();
51
            }
52
        }
53
        return true;
54
    }
55
56
    /**
57
     * Executes one callback from the top with given arguments without taking it out
58
     * @param  mixed   ...$args Arguments
59
     * @return boolean
60
     */
61 View Code Duplication
    public function executeAndKeepOne()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
62
    {
63
        if ($this->isEmpty()) {
64
            return false;
65
        }
66
        $cb = $this->shift();
67
        $this->unshift($cb);
0 ignored issues
show
Documentation introduced by
$cb is of type *, but the function expects a callable.

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...
68
        if ($cb) {
69
            $cb(...func_get_args());
70
        }
71
        return true;
72
    }
73
74
    /**
75
     * Executes all callbacks with given arguments
76
     * @param array $args
77
     * @return int
78
     */
79
    public function executeAll(...$args)
80
    {
81
        if ($this->isEmpty()) {
82
            return 0;
83
        }
84
85
        $n = 0;
86
        
87
        foreach ($this as $cb) {
88
            $cb(...$args);
89
            ++$n;
90
            if ($cb instanceof CallbackWrapper) {
91
                $cb->cancel();
92
            }
93
        }
94
95
        return $n;
96
    }
97
98
    /**
99
     * Return array
100
     * @return array
101
     */
102
    public function toArray()
103
    {
104
        $arr = [];
105
        while (!$this->isEmpty()) {
106
            $arr[] = $this->shift();
107
        }
108
        return $arr;
109
    }
110
111
    /**
112
     * Shifts all callbacks sequentially
113
     * @return void
114
     */
115
    public function reset()
116
    {
117
        while (!$this->isEmpty()) {
118
            $this->shift();
119
        }
120
    }
121
}
122