Completed
Pull Request — master (#1583)
by Jeroen
41:02 queued 38:49
created

LegacyEventDispatcherProxy.php$0 ➔ addSubscriber()   A

Complexity

Conditions 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
crap 2
1
<?php
2
3
/*
4
 * This file is part of the FOSElasticaBundle package.
5
 *
6
 * (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace FOS\ElasticaBundle\EventDispatcher;
13
14
use FOS\ElasticaBundle\Event\ElasticaEvent;
15
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
16
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
17
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface as ContractsEventDispatcherInterface;
18
use TypeError;
19
20
final class LegacyEventDispatcherProxy
21
{
22 82
    public static function decorate(?EventDispatcherInterface $dispatcher): ?EventDispatcherInterface
23
    {
24 82
        if ($dispatcher === null) {
25 39
            return null;
26
        }
27
28 43
        if (!$dispatcher instanceof ContractsEventDispatcherInterface) {
29
            return $dispatcher;
30
        }
31
32
        return new class($dispatcher) implements EventDispatcherInterface {
33
            /**
34
             * @var EventDispatcherInterface
35
             */
36
            private $dispatcher;
37
38
            /**
39
             * @param EventDispatcherInterface $dispatcher
40
             */
41 43
            public function __construct(EventDispatcherInterface $dispatcher)
42
            {
43 43
                $this->dispatcher = $dispatcher;
44 43
            }
45
46
            /**
47
             * {@inheritdoc}
48
             */
49 24
            public function dispatch($eventName/*, object $event = null*/)
50
            {
51 24
                $event = 1 < func_num_args() ? func_get_arg(1) : new ElasticaEvent();
52
53 24
                if (!is_string($eventName)) {
54
                    throw new TypeError(sprintf('Argument 1 passed to "%s::dispatch()" must be a string, %s given.', EventDispatcherInterface::class, is_object($eventName) ? get_class($eventName) : gettype($eventName)));
0 ignored issues
show
Unused Code introduced by
The call to TypeError::__construct() has too many arguments starting with sprintf('Argument 1 pass... : gettype($eventName)).

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
55
                }
56
57 24
                $this->dispatcher->dispatch($event, $eventName);
58 24
            }
59
60
            /**
61
             * {@inheritdoc}
62
             */
63 5
            public function addListener($eventName, $listener, $priority = 0)
64
            {
65 5
                return $this->dispatcher->addListener($eventName, $listener, $priority);
66
            }
67
68
            /**
69
             * {@inheritdoc}
70
             */
71
            public function addSubscriber(EventSubscriberInterface $subscriber)
72
            {
73
                return $this->dispatcher->addSubscriber($subscriber);
74
            }
75
76
            /**
77
             * {@inheritdoc}
78
             */
79
            public function removeListener($eventName, $listener)
80
            {
81
                return $this->dispatcher->removeListener($eventName, $listener);
82
            }
83
84
            /**
85
             * {@inheritdoc}
86
             */
87
            public function removeSubscriber(EventSubscriberInterface $subscriber)
88
            {
89
                return $this->dispatcher->removeSubscriber($subscriber);
90
            }
91
92
            /**
93
             * {@inheritdoc}
94
             */
95
            public function getListeners($eventName = null): array
96
            {
97
                return $this->dispatcher->getListeners($eventName);
98
            }
99
100
            /**
101
             * {@inheritdoc}
102
             */
103
            public function getListenerPriority($eventName, $listener): ?int
104
            {
105
                return $this->dispatcher->getListenerPriority($eventName, $listener);
106
            }
107
108
            /**
109
             * {@inheritdoc}
110
             */
111
            public function hasListeners($eventName = null): bool
112
            {
113
                return $this->dispatcher->hasListeners($eventName);
114
            }
115
116
            /**
117
             * Proxies all method calls to the original event dispatcher.
118
             */
119
            public function __call($method, $arguments)
120
            {
121
                return $this->dispatcher->{$method}(...$arguments);
122
            }
123
        };
124
    }
125
}
126