Completed
Push — master ( 039325...4e8005 )
by Alexander
11:24
created

DispatcherTest::testPropagationStops()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 26

Duplication

Lines 16
Ratio 61.54 %

Importance

Changes 0
Metric Value
cc 1
dl 16
loc 26
rs 9.504
c 0
b 0
f 0
nc 1
nop 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A DispatcherTest.php$1 ➔ getListenersForEvent() 13 13 1
1
<?php
2
3
namespace Yii\EventDispatcher\Tests;
4
5
use PHPUnit\Framework\TestCase;
6
use Psr\EventDispatcher\ListenerProviderInterface;
7
use Yii\EventDispatcher\Dispatcher;
8
use Yii\EventDispatcher\Tests\Event\Event;
9
use Yii\EventDispatcher\Tests\Event\StoppableEvent;
10
11
class DispatcherTest extends TestCase
12
{
13
    public function testCallsAllListeners()
14
    {
15
        $event = new Event();
16
17 View Code Duplication
        $provider = new class implements ListenerProviderInterface
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
18
        {
19
            public function getListenersForEvent(object $event): iterable
20
            {
21
                yield function (Event $event) {
22
                    $event->register(1);
23
                };
24
                yield function (Event $event) {
25
                    $event->register(2);
26
                };
27
                yield function (Event $event) {
28
                    $event->register(3);
29
                };
30
            }
31
        };
32
33
        $dispatcher = new Dispatcher($provider);
34
        $dispatcher->dispatch($event);
0 ignored issues
show
Documentation introduced by
$event is of type object<Yii\EventDispatcher\Tests\Event\Event>, 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...
35
36
        $this->assertEquals([1, 2, 3], $event->registered());
37
    }
38
39
    public function testPropagationStops()
40
    {
41
        $event = new StoppableEvent();
42
43 View Code Duplication
        $provider = new class implements ListenerProviderInterface
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
44
        {
45
            public function getListenersForEvent(object $event): iterable
46
            {
47
                yield function (StoppableEvent $event) {
48
                    $event->register(1);
49
                    $event->stopPropogation();
50
                };
51
                yield function (StoppableEvent $event) {
52
                    $event->register(2);
53
                };
54
                yield function (StoppableEvent $event) {
55
                    $event->register(3);
56
                };
57
            }
58
        };
59
60
        $dispatcher = new Dispatcher($provider);
61
        $dispatcher->dispatch($event);
0 ignored issues
show
Documentation introduced by
$event is of type object<Yii\EventDispatch...s\Event\StoppableEvent>, 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...
62
63
        $this->assertEquals([1], $event->registered());
64
    }
65
}
66