PriorityQueueCallbacks::insert()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
nc 1
nop 2
1
<?php
2
namespace PHPDaemon\Structures;
3
4
use PHPDaemon\Core\CallbackWrapper;
5
6
/**
7
 * PriorityQueueCallbacks
8
 * @package PHPDaemon\Structures
9
 * @author  Vasily Zorin <[email protected]>
10
 */
11
class PriorityQueueCallbacks extends \SplPriorityQueue
12
{
13
    use \PHPDaemon\Traits\ClassWatchdog;
14
    use \PHPDaemon\Traits\StaticObjectWatchdog;
15
16
    /**
17
     * Insert callback
18
     * @param  callable $cb Callback
19
     * @param  integer $pri Priority
20
     * @return void
21
     */
22
    public function insert($cb, $pri = 0)
23
    {
24
        parent::insert(CallbackWrapper::wrap($cb), $pri);
25
    }
26
27
    /**
28
     * Enqueue callback
29
     * @param  callable $cb Callback
30
     * @param  integer $pri Priority
31
     * @return void
32
     */
33
    public function enqueue($cb, $pri = 0)
34
    {
35
        parent::insert(CallbackWrapper::wrap($cb), $pri);
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (insert() instead of enqueue()). Are you sure this is correct? If so, you might want to change this to $this->insert().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
36
    }
37
38
    /**
39
     * Dequeue
40
     * @return callable
41
     */
42
    public function dequeue()
43
    {
44
        return $this->extract();
45
    }
46
47
    /**
48
     * Compare two priorities
49
     * @param  integer $pri1
50
     * @param  integer $pri2
51
     * @return integer
52
     */
53
    public function compare($pri1, $pri2)
54
    {
55
        if ($pri1 === $pri2) {
56
            return 0;
57
        }
58
        return $pri1 < $pri2 ? -1 : 1;
59
    }
60
61
    /**
62
     * Executes one callback from the top of queue with arbitrary arguments
63
     * @param  mixed ...$args Arguments
64
     * @return boolean
65
     */
66
    public function executeOne(...$args)
67
    {
68
        if ($this->isEmpty()) {
69
            return false;
70
        }
71
        $cb = $this->extract();
72
        if ($cb) {
73
            $cb(...$args);
74
        }
75
        return true;
76
    }
77
78
    /**
79
     * Executes all callbacks from the top of queue to bottom with arbitrary arguments
80
     * @param  mixed ...$args Arguments
81
     * @return integer
82
     */
83 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...
84
    {
85
        if ($this->isEmpty()) {
86
            return 0;
87
        }
88
        $n = 0;
89
        do {
90
            $cb = $this->extract();
91
            if ($cb) {
92
                $cb(...$args);
93
                ++$n;
94
            }
95
        } while (!$this->isEmpty());
96
        return $n;
97
    }
98
}
99