Completed
Pull Request — master (#2)
by Andrii
12:37
created

DispatcherTest::testPropogationStops()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 35

Duplication

Lines 11
Ratio 31.43 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 11
loc 35
rs 9.36
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A DispatcherTest.php$2 ➔ isPropagationStopped() 0 4 1
A DispatcherTest.php$2 ➔ stopPropagation() 0 4 1
A DispatcherTest.php$3 ➔ getListenersForEvent() 0 9 1
1
<?php
2
namespace Yii\EventDispatcher\Tests;
3
4
use PHPUnit\Framework\TestCase;
5
use Psr\EventDispatcher\ListenerProviderInterface;
6
use Psr\EventDispatcher\StoppableEventInterface;
7
use Yii\EventDispatcher\Dispatcher;
8
9
class DispatcherTest extends TestCase
10
{
11
    public function testCallsAllListeners()
12
    {
13
        $event = new class {
14
            public $listeners = [];
15
        };
16
17
        $provider = new class implements ListenerProviderInterface {
18
            public function getListenersForEvent(object $event): iterable
19
            {
20
                yield function ($event) { $event->listeners[] = 1; };
21
                yield function ($event) { $event->listeners[] = 2; };
22
                yield function ($event) { $event->listeners[] = 3; };
23
            }
24
        };
25
26
        $dispatcher = new Dispatcher($provider);
27
        $dispatcher->dispatch($event);
0 ignored issues
show
Documentation introduced by
$event is of type object<Yii\EventDispatch...s/DispatcherTest.php$0>, but the function expects a object<Yii\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...
28
29
        $this->assertEquals([1, 2, 3], $event->listeners);
30
    }
31
32
    public function testPropagationStops()
33
    {
34
        $event = new class implements StoppableEventInterface {
35
            public $listeners = [];
36
37
            private $isPropagationStopped = false;
38
39
            public function isPropagationStopped(): bool
40
            {
41
                return $this->isPropagationStopped;
42
            }
43
44
            public function stopPropagation(): void
45
            {
46
                $this->isPropagationStopped = true;
47
            }
48
        };
49
50
        $provider = new class implements ListenerProviderInterface {
51
            public function getListenersForEvent(object $event): iterable
52
            {
53
                yield function ($event) {
54
                    $event->listeners[] = 1;
55
                    $event->stopPropagation();
56
                };
57
                yield function ($event) { $event->listeners[] = 2; };
58
                yield function ($event) { $event->listeners[] = 3; };
59
            }
60
        };
61
62
        $dispatcher = new Dispatcher($provider);
63
        $dispatcher->dispatch($event);
0 ignored issues
show
Documentation introduced by
$event is of type object<Yii\EventDispatch...s/DispatcherTest.php$2>, but the function expects a object<Yii\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...
64
65
        $this->assertEquals([1], $event->listeners);
66
    }
67
}
68