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

StackCallbacks::executeAll()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 5
eloc 11
nc 5
nop 1
dl 0
loc 20
rs 8.8571
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
            if ($cb) {
89
                $cb(...$args);
90
                ++$n;
91
                if ($cb instanceof CallbackWrapper) {
92
                    $cb->cancel();
93
                }
94
            }
95
        }
96
97
        return $n;
98
    }
99
100
    /**
101
     * Return array
102
     * @return array
103
     */
104
    public function toArray()
105
    {
106
        $arr = [];
107
        while (!$this->isEmpty()) {
108
            $arr[] = $this->shift();
109
        }
110
        return $arr;
111
    }
112
113
    /**
114
     * Shifts all callbacks sequentially
115
     * @return void
116
     */
117
    public function reset()
118
    {
119
        while (!$this->isEmpty()) {
120
            $this->shift();
121
        }
122
    }
123
}
124