Completed
Push — master ( a4e23e...cc91f5 )
by Yohan
07:52
created

StateMachineDispatcher::isUnder43()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.9
c 0
b 0
f 0
cc 2
nc 2
nop 0
1
<?php
2
3
namespace Finite\Event;
4
5
use Symfony\Component\EventDispatcher\EventDispatcher;
6
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
7
8
/**
9
 * This class is here to provide a compatibility layer between symfony
10
 * versions from 2.8 to ^5.0
11
 */
12
class StateMachineDispatcher
13
{
14
    /**
15
     * @var EventDispatcherInterface
16
     */
17
    private $eventDispatcher;
18
19
    public function __construct(EventDispatcherInterface $eventDispatcher = null)
20
    {
21
        $this->eventDispatcher = $eventDispatcher ?: new EventDispatcher;
22
    }
23
24
    public function dispatch($eventName, StateMachineEvent $event)
25
    {
26
        if (self::isUnder43()) {
27
            return $this->eventDispatcher->dispatch($eventName, $event);
0 ignored issues
show
Documentation introduced by
$event is of type object<Finite\Event\StateMachineEvent>, but the function expects a null|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...
28
        }
29
30
        return $this->eventDispatcher->dispatch($event, $eventName);
0 ignored issues
show
Documentation introduced by
$event is of type object<Finite\Event\StateMachineEvent>, but the function expects a object<Symfony\Contracts\EventDispatcher\object>.

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...
31
    }
32
33
    public function addListener($eventName, $listener, $priority = 0)
34
    {
35
        return $this->eventDispatcher->addListener($eventName, $listener, $priority);
36
    }
37
38
    private static function isUnder43()
39
    {
40
        static $result = null;
41
42
        if (null === $result) {
43
            $reflectionMethod = new \ReflectionMethod(EventDispatcherInterface::class, 'dispatch');
44
            $result           = 'event' === ($reflectionMethod)->getParameters()[1]->getName();
45
        }
46
47
        return $result;
48
    }
49
}
50