TickContinousQueue   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 72
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 72
loc 72
ccs 19
cts 19
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() 9 9 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 TickContinousQueue
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 the next 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 15
    public function add(callable $listener)
51
    {
52 15
        $this->queue->enqueue($listener);
53 15
    }
54
55
    /**
56
     * Flush the callback queue.
57
     *
58
     * Invokes callbacks which were on the queue when tick() was called and newly added ones.
59
     */
60 39
    public function tick()
61
    {
62 39
        while (!$this->queue->isEmpty() && $this->loop->isRunning())
63
        {
64 9
            $this->callback = $this->queue->dequeue();
65 9
            $callback = $this->callback; // without this proxy PHPStorm marks line as fatal error.
66 9
            $callback($this->loop);
67
        }
68 39
    }
69
70
    /**
71
     * Check if the next tick queue is empty.
72
     *
73
     * @return boolean
74
     */
75 6
    public function isEmpty()
76
    {
77 6
        return $this->queue->isEmpty();
78
    }
79
}
80