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

LegacyEventDispatcherProxy::decorate()   B

Complexity

Conditions 1
Paths 3

Size

Total Lines 103

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 1.1516

Importance

Changes 0
Metric Value
dl 0
loc 103
ccs 14
cts 30
cp 0.4667
rs 8
c 0
b 0
f 0
cc 1
crap 1.1516
nc 3
nop 1

10 Methods

Rating   Name   Duplication   Size   Complexity  
A LegacyEventDispatcherProxy.php$0 ➔ __construct() 0 4 1
A LegacyEventDispatcherProxy.php$0 ➔ dispatch() 0 10 4
A LegacyEventDispatcherProxy.php$0 ➔ addListener() 0 4 1
A LegacyEventDispatcherProxy.php$0 ➔ addSubscriber() 0 4 1
A LegacyEventDispatcherProxy.php$0 ➔ removeListener() 0 4 1
A LegacyEventDispatcherProxy.php$0 ➔ removeSubscriber() 0 4 1
A LegacyEventDispatcherProxy.php$0 ➔ getListeners() 0 4 1
A LegacyEventDispatcherProxy.php$0 ➔ getListenerPriority() 0 4 1
A LegacyEventDispatcherProxy.php$0 ➔ hasListeners() 0 4 1
A LegacyEventDispatcherProxy.php$0 ➔ __call() 0 4 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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