RecordingDispatcher::dispatch()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
rs 9.4286
cc 1
eloc 3
nc 1
nop 2
1
<?php
2
3
namespace Werkspot\Bundle\StatsdBundle\Event;
4
5
use Symfony\Component\EventDispatcher\ContainerAwareEventDispatcher;
6
use Symfony\Component\EventDispatcher\Event;
7
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
8
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
9
use Werkspot\Bundle\StatsdBundle\Client\StatsdClientInterface;
10
11
class RecordingDispatcher implements EventDispatcherInterface
12
{
13
    /**
14
     * @var ContainerAwareEventDispatcher|EventDispatcherInterface
15
     */
16
    private $originalDispatcher;
17
18
    /**
19
     * @var StatsdClientInterface
20
     */
21
    private $statsdClient;
22
23
    /**
24
     * @param EventDispatcherInterface $originalDispatcher
25
     * @param StatsdClientInterface $statsdClient
26
     */
27
    public function __construct(
28
        EventDispatcherInterface $originalDispatcher,
29
        StatsdClientInterface $statsdClient
30
    ) {
31
        $this->originalDispatcher = $originalDispatcher;
32
        $this->statsdClient = $statsdClient;
33
    }
34
35
    /**
36
     * @see ContainerAwareEventDispatcher::addListenerService
37
     */
38
    public function addListenerService($eventName, $callback, $priority = 0)
39
    {
40
        $this->originalDispatcher->addListenerService($eventName, $callback, $priority);
0 ignored issues
show
Bug introduced by
The method addListenerService does only exist in Symfony\Component\EventD...nerAwareEventDispatcher, but not in Symfony\Component\EventD...ventDispatcherInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
41
    }
42
43
    /**
44
     * @see ContainerAwareEventDispatcher::addSubscriberService
45
     */
46
    public function addSubscriberService($serviceId, $class)
47
    {
48
        $this->originalDispatcher->addSubscriberService($serviceId, $class);
0 ignored issues
show
Bug introduced by
The method addSubscriberService does only exist in Symfony\Component\EventD...nerAwareEventDispatcher, but not in Symfony\Component\EventD...ventDispatcherInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54
    public function dispatch($eventName, Event $event = null)
55
    {
56
        $this->statsdClient->increment(str_replace('.', '-', $eventName));
57
        return $this->originalDispatcher->dispatch($eventName, $event);
58
    }
59
60
    /**
61
     * {@inheritdoc}
62
     */
63
    public function addListener($eventName, $listener, $priority = 0)
64
    {
65
        $this->originalDispatcher->addListener($eventName, $listener, $priority);
66
    }
67
68
    /**
69
     * {@inheritdoc}
70
     */
71
    public function addSubscriber(EventSubscriberInterface $subscriber)
72
    {
73
        $this->originalDispatcher->addSubscriber($subscriber);
74
    }
75
76
    /**
77
     * {@inheritdoc}
78
     */
79
    public function removeListener($eventName, $listener)
80
    {
81
        $this->originalDispatcher->removeListener($eventName, $listener);
82
    }
83
84
    /**
85
     * {@inheritdoc}
86
     */
87
    public function removeSubscriber(EventSubscriberInterface $subscriber)
88
    {
89
        $this->originalDispatcher->removeSubscriber($subscriber);
90
    }
91
92
    /**
93
     * {@inheritdoc}
94
     */
95
    public function getListeners($eventName = null)
96
    {
97
        return $this->originalDispatcher->getListeners($eventName);
98
    }
99
100
    /**
101
     * {@inheritdoc}
102
     */
103
    public function hasListeners($eventName = null)
104
    {
105
        return $this->originalDispatcher->hasListeners($eventName);
106
    }
107
108
    /**
109
     * {@inheritdoc}
110
     */
111
    public function getListenerPriority($eventName, $listener)
112
    {
113
        return $this->getListenerPriority($eventName, $listener);
114
    }
115
}
116