EventListener   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 90
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 11
lcom 2
cbo 1
dl 0
loc 90
ccs 22
cts 22
cp 1
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 2
A __destruct() 0 7 1
A getEmitter() 0 4 1
A getEvent() 0 4 1
A getHandler() 0 4 2
A getListener() 0 4 2
A cancel() 0 7 2
1
<?php
2
3
namespace Dazzle\Event;
4
5
class EventListener
6
{
7
    /**
8
     * @var string
9
     */
10
    public $event;
11
12
    /**
13
     * @var callable
14
     */
15
    public $handler;
16
17
    /**
18
     * @var callable
19
     */
20
    public $listener;
21
22
    /**
23
     * @var EventEmitterInterface
24
     */
25
    private $emitter;
26
27
    /**
28
     * @param EventEmitterInterface $emitter
29
     * @param string $event
30
     * @param callable $handler
31
     * @param callable|null $listener
32
     */
33 107
    public function __construct(EventEmitterInterface $emitter, $event, callable $handler, callable $listener = null)
34
    {
35 107
        $this->emitter = $emitter;
36 107
        $this->event = $event;
37 107
        $this->handler = $handler;
38 107
        $this->listener = $listener !== null ? $listener : $handler;
39 107
    }
40
41
    /**
42
     *
43
     */
44 97
    public function __destruct()
45
    {
46 97
        unset($this->emitter);
47 97
        unset($this->event);
48 97
        unset($this->handler);
49 97
        unset($this->listener);
50 97
    }
51
52
    /**
53
     * @return EventEmitterInterface
54
     */
55 25
    public function getEmitter()
56
    {
57 25
        return $this->emitter;
58
    }
59
60
    /**
61
     * @return string
62
     */
63 26
    public function getEvent()
64
    {
65 26
        return $this->event;
66
    }
67
68
    /**
69
     * @return callable|null
70
     */
71 18
    public function getHandler()
72
    {
73 18
        return isset($this->handler) ? $this->handler : null;
74
    }
75
76
    /**
77
     * @return callable
78
     */
79
    public function getListener()
80
    {
81
        return isset($this->listener) ? $this->listener : function() {};
82
    }
83
84
    /**
85
     *
86
     */
87 1
    public function cancel()
88
    {
89 1
        if (isset($this->emitter))
90
        {
91 1
            $this->emitter->removeListener($this->getEvent(), $this->getHandler());
0 ignored issues
show
Bug introduced by
It seems like $this->getHandler() targeting Dazzle\Event\EventListener::getHandler() can also be of type null; however, Dazzle\Event\EventEmitte...rface::removeListener() does only seem to accept callable, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
92
        }
93 1
    }
94
}
95