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

anonymous//tests/Provider/AggregateTest.php$2   A

Complexity

Total Complexity 1

Size/Duplication

Total Lines 6
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
wmc 1
lcom 0
cbo 0
1
<?php
2
3
namespace Yii\EventDispatcher\Tests\Provider;
4
5
use PHPUnit\Framework\TestCase;
6
use Psr\EventDispatcher\ListenerProviderInterface;
7
use Yii\EventDispatcher\Provider\Aggregate;
8
use Yii\EventDispatcher\Tests\Event\Event;
9
10
class AggregateTest extends TestCase
11
{
12
    public function testProvidesAllListeners(): void
13
    {
14
        $event = new Event();
15
16
        $provider1 = new class implements ListenerProviderInterface
17
        {
18
            public function getListenersForEvent(object $event): iterable
19
            {
20
                yield function (Event $event) {
21
                    $event->register(1);
22
                };
23
            }
24
        };
25
26
        $provider2 = new class implements ListenerProviderInterface
27
        {
28
            public function getListenersForEvent(object $event): iterable
29
            {
30
                yield function (Event $event) {
31
                    $event->register(2);
32
                };
33
            }
34
        };
35
36
        $aggregate = new Aggregate();
37
38
        $aggregate->attach($provider1);
39
        $aggregate->attach($provider2);
40
41
        foreach ($aggregate->getListenersForEvent($event) as $listener) {
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\Provider\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...
42
            $listener($event);
43
        }
44
        $this->assertEquals([1, 2], $event->registered());
45
    }
46
}
47