TickFiniteQueue   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 74
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 1
dl 74
loc 74
ccs 20
cts 20
cp 1
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 5 5 1
A __destruct() 5 5 1
A add() 4 4 1
A tick() 11 11 3
A isEmpty() 4 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Dazzle\Loop\Tick;
4
5
use Dazzle\Loop\LoopModelInterface;
6
use SplQueue;
7
8 View Code Duplication
class TickFiniteQueue
0 ignored issues
show
Duplication introduced by
This class 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...
9
{
10
    /**
11
     * @var LoopModelInterface
12
     */
13
    protected $loop;
14
15
    /**
16
     * @var SplQueue
17
     */
18
    protected $queue;
19
20
    /**
21
     * @var callable
22
     */
23
    private $callback;
24
25
    /**
26
     * @param LoopModelInterface $loop
27
     */
28 61
    public function __construct(LoopModelInterface $loop)
29
    {
30 61
        $this->loop = $loop;
31 61
        $this->queue = new SplQueue();
32 61
    }
33
34
    /**
35
     *
36
     */
37 10
    public function __destruct()
38
    {
39 10
        unset($this->loop);
40 10
        unset($this->queue);
41 10
    }
42
43
    /**
44
     * Add a callback to be invoked on a future tick of the event loop.
45
     *
46
     * Callbacks are guaranteed to be executed in the order they are enqueued, before any timer or stream events.
47
     *
48
     * @param callable $listener
49
     */
50 17
    public function add(callable $listener)
51
    {
52 17
        $this->queue->enqueue($listener);
53 17
    }
54
55
    /**
56
     * Flush the callback queue.
57
     *
58
     * Invokes as many callbacks as were on the queue when tick() was called.
59
     */
60 39
    public function tick()
61
    {
62 39
        $count = $this->queue->count();
63
64 39
        while ($count-- && $this->loop->isRunning())
65
        {
66 11
            $this->callback = $this->queue->dequeue();
67 11
            $callback = $this->callback; // without this proxy PHPStorm marks line as fatal error.
68 11
            $callback($this->loop);
69
        }
70 39
    }
71
72
    /**
73
     * Check if the next tick queue is empty.
74
     *
75
     * @return boolean
76
     */
77 6
    public function isEmpty()
78
    {
79 6
        return $this->queue->isEmpty();
80
    }
81
}
82