EventEmitterTrait   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 99
Duplicated Lines 24.24 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%
Metric Value
wmc 11
lcom 1
cbo 0
dl 24
loc 99
ccs 26
cts 26
cp 1
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A on() 0 6 1
A removeListeners() 0 10 2
A getListeners() 0 4 2
A emit() 0 8 2
A once() 12 12 1
A removeListener() 12 12 3

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 Thruster\Component\EventEmitter;
4
5
/**
6
 * Trait EventEmitterTrait
7
 *
8
 * @package Thruster\Component\EventEmitter
9
 * @author  Aurimas Niekis <[email protected]>
10
 */
11
trait EventEmitterTrait
12
{
13
    /**
14
     * @var callable[]
15
     */
16
    protected $listeners = [];
17
18
    /**
19
     * @param string   $event
20
     * @param callable $listener
21
     *
22
     * @return $this
23
     */
24 14
    public function on(string $event, callable $listener) : self
25
    {
26 14
        $this->listeners[$event][] = $listener;
27
28 14
        return $this;
29
    }
30
31
    /**
32
     * @param string   $event
33
     * @param callable $listener
34
     *
35
     * @return $this
36
     */
37 View Code Duplication
    public function once(string $event, callable $listener) : self
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...
38
    {
39 2
        $onceListener = function () use (&$onceListener, $event, $listener) {
40 2
            $this->removeListener($event, $onceListener);
41
42 2
            call_user_func_array($listener, func_get_args());
43 2
        };
44
45 2
        $this->on($event, $onceListener);
46
47 2
        return $this;
48
    }
49
50
    /**
51
     * @param string   $event
52
     * @param callable $listener
53
     *
54
     * @return $this
55
     */
56 4 View Code Duplication
    public function removeListener(string $event, callable $listener) : self
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...
57
    {
58 4
        if (array_key_exists($event, $this->listeners)) {
59 3
            $index = array_search($listener, $this->listeners[$event], true);
60
61 3
            if (false !== $index) {
62 3
                unset($this->listeners[$event][$index]);
63
            }
64
        }
65
66 4
        return $this;
67
    }
68
69
    /**
70
     * @param string $event
71
     *
72
     * @return $this
73
     */
74 3
    public function removeListeners(string $event = null) : self
75
    {
76 3
        if (null !== $event) {
77 2
            unset($this->listeners[$event]);
78
        } else {
79 1
            $this->listeners = [];
80
        }
81
82 3
        return $this;
83
    }
84
85
    /**
86
     * @param string $event
87
     *
88
     * @return array|callable[]
89
     */
90 12
    public function getListeners(string $event) : array
91
    {
92 12
        return isset($this->listeners[$event]) ? $this->listeners[$event] : [];
93
    }
94
95
    /**
96
     * @param string $event
97
     * @param array  $arguments
98
     *
99
     * @return $this
100
     */
101 12
    public function emit(string $event, array $arguments = []) : self
102
    {
103 12
        foreach ($this->getListeners($event) as $listener) {
104 8
            call_user_func_array($listener, $arguments);
105
        }
106
107 12
        return $this;
108
    }
109
}
110