StateMachineDispatcher::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2
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 90
    public function __construct(EventDispatcherInterface $eventDispatcher = null)
20
    {
21 90
        $this->eventDispatcher = $eventDispatcher ?: new EventDispatcher;
22 90
    }
23
24 80
    public function dispatch($eventName, StateMachineEvent $event)
25
    {
26 80
        if (self::isUnder43()) {
27 80
            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 5
    public function addListener($eventName, $listener, $priority = 0)
34
    {
35 5
        return $this->eventDispatcher->addListener($eventName, $listener, $priority);
36
    }
37
38 80
    private static function isUnder43()
39
    {
40 80
        static $result = null;
41
42 80
        if (null === $result) {
43 5
            $reflectionMethod     = new \ReflectionMethod(EventDispatcherInterface::class, 'dispatch');
44 5
            $reflectionParameters = $reflectionMethod->getParameters();
45 5
            $result               = count($reflectionParameters) >= 2 && 'event' === $reflectionParameters[1]->getName();
46 3
        }
47
48 80
        return $result;
49
    }
50
}
51