StackCallbacks::toArray()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
dl 0
loc 8
rs 10
c 0
b 0
f 0
nc 2
nop 0
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
    /**
14
     * Push callback to the bottom of stack
15
     * @param  callable $cb Callback
16
     * @return void
17
     */
18
    public function push($cb)
19
    {
20
        parent::push(CallbackWrapper::wrap($cb));
21
    }
22
23
    /**
24
     * Executes one callback from the top with given arguments
25
     * @param  mixed ...$args Arguments
26
     * @return boolean
27
     */
28 View Code Duplication
    public function executeOne(...$args)
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...
29
    {
30
        if ($this->isEmpty()) {
31
            return false;
32
        }
33
        $cb = $this->shift();
34
        if ($cb) {
35
            $cb(...$args);
36
            if ($cb instanceof CallbackWrapper) {
37
                $cb->cancel();
38
            }
39
        }
40
        return true;
41
    }
42
43
    /**
44
     * Executes one callback from the top with given arguments without taking it out
45
     * @param  mixed ...$args Arguments
46
     * @return boolean
47
     */
48 View Code Duplication
    public function executeAndKeepOne(...$args)
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...
49
    {
50
        if ($this->isEmpty()) {
51
            return false;
52
        }
53
        $cb = $this->shift();
54
        $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...
55
        if ($cb) {
56
            $cb(...$args);
57
        }
58
        return true;
59
    }
60
61
    /**
62
     * Push callback to the top of stack
63
     * @param  callable $cb Callback
64
     * @return void
65
     */
66
    public function unshift($cb)
67
    {
68
        parent::unshift(CallbackWrapper::wrap($cb));
69
    }
70
71
    /**
72
     * Executes all callbacks with given arguments
73
     * @param array $args
74
     * @return int
75
     */
76 View Code Duplication
    public function executeAll(...$args)
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...
77
    {
78
        if ($this->isEmpty()) {
79
            return 0;
80
        }
81
82
        $n = 0;
83
        do {
84
            if ($cb = $this->shift()) {
85
                $cb(...$args);
86
                ++$n;
87
                if ($cb instanceof CallbackWrapper) {
88
                    $cb->cancel();
89
                }
90
            }
91
        } while (!$this->isEmpty());
92
        return $n;
93
    }
94
95
    /**
96
     * Return array
97
     * @return array
98
     */
99
    public function toArray()
100
    {
101
        $arr = [];
102
        while (!$this->isEmpty()) {
103
            $arr[] = $this->shift();
104
        }
105
        return $arr;
106
    }
107
108
    /**
109
     * Shifts all callbacks sequentially
110
     * @return void
111
     */
112
    public function reset()
113
    {
114
        while (!$this->isEmpty()) {
115
            $this->shift();
116
        }
117
    }
118
}
119