ListenForInterfacesTrait   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 10
dl 0
loc 28
rs 10
c 1
b 0
f 0
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getListeners() 0 7 2
A addInterfaceListeners() 0 10 3
1
<?php
2
3
namespace ByTIC\EventDispatcher\Dispatcher\Traits;
4
5
/**
6
 * Trait ListenForInterfacesTrait
7
 * @package ByTIC\EventDispatcher\Dispatcher\Traits
8
 */
9
trait ListenForInterfacesTrait
10
{
11
    public function getListeners(string $eventName = null): array
12
    {
13
        $listeners = parent::getListeners($eventName);
14
15
        return class_exists($eventName, false)
0 ignored issues
show
Bug introduced by
It seems like $eventName can also be of type null; however, parameter $class of class_exists() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

15
        return class_exists(/** @scrutinizer ignore-type */ $eventName, false)
Loading history...
16
            ? $this->addInterfaceListeners($eventName, $listeners)
17
            : $listeners;
18
    }
19
20
    /**
21
     * Add the listeners for the event's interfaces to the given array.
22
     *
23
     * @param string $eventName
24
     * @param array $listeners
25
     * @return array
26
     */
27
    protected function addInterfaceListeners($eventName, array $listeners = []): array
28
    {
29
        foreach (class_implements($eventName) as $interface) {
30
            $interfaceListeners = parent::getListeners($interface);
31
            if (count($interfaceListeners)) {
32
                $listeners = array_merge($listeners, $interfaceListeners);
33
            }
34
        }
35
36
        return $listeners;
37
    }
38
}
39