Test Failed
Push — master ( 34c58c...47fa7b )
by Alexey
02:01
created

EventEmitterTrait::runListeners()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 3
eloc 5
nc 3
nop 2
1
<?php
2
/**
3
 * @author Alexey Samoylov <[email protected]>
4
 */
5
6
namespace YarCode\Events;
7
8
trait EventEmitterTrait
9
{
10
    protected $listeners = [];
11
12
    /**
13
     * @param string $eventName
14
     * @param callable $listener
15
     */
16
    public function addListener($eventName, callable $listener)
17
    {
18
        if (!isset($this->listeners[$eventName])) {
19
            $this->listeners[$eventName] = [];
20
        }
21
22
        $this->listeners[$eventName][] = $listener;
23
    }
24
25
    /**
26
     * @param string $eventName
27
     * @param callable $listener
28
     */
29
    public function removeListener($eventName, callable $listener)
30
    {
31
        if (isset($this->listeners[$eventName])) {
32
            if (false !== $index = array_search($listener, $this->listeners[$eventName], true)) {
33
                unset($this->listeners[$eventName][$index]);
34
            }
35
        }
36
    }
37
38
    /**
39
     * @param string|null $eventName
40
     */
41
    public function removeAllListeners($eventName = null)
42
    {
43
        if ($eventName !== null) {
44
            unset($this->listeners[$eventName]);
45
        } else {
46
            $this->listeners = [];
47
        }
48
    }
49
50
    /**
51
     * @param string $eventName
52
     * @param Event $event
53
     */
54
    public function emit($eventName, Event $event = null)
55
    {
56
        if (empty($this->listeners($eventName))) {
57
            return;
58
        }
59
60
        $event = $event ?: new Event();
61
        $event->name = $eventName;
62
        $event->handled = false;
63
64
        $this->runListeners($event, $event);
0 ignored issues
show
Documentation introduced by
$event is of type object<YarCode\Events\Event>, but the function expects a string.

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...
65
    }
66
67
    /**
68
     * @param string $eventName
69
     * @return array
70
     */
71
    public function listeners($eventName)
72
    {
73
        return isset($this->listeners[$eventName]) ? $this->listeners[$eventName] : [];
74
    }
75
76
    /**
77
     * @param string $eventName
78
     * @param Event $event
79
     */
80
    private function runListeners($eventName, Event $event)
81
    {
82
        foreach ($this->listeners($eventName) as $listener) {
83
            call_user_func($listener, $event);
84
            if ($event->handled) {
85
                break;
86
            }
87
        }
88
    }
89
}
90