Test Failed
Pull Request — master (#18)
by Alex
16:59 queued 14:19
created

LazyListener::loadListener()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Arp\EventDispatcher\Listener;
6
7
use Arp\EventDispatcher\Listener\Exception\EventListenerException;
8
9
/**
10
 * @author  Alex Patterson <[email protected]>
11
 * @package Arp\EventDispatcher\Listener
12
 */
13
class LazyListener
14
{
15
    /**
16
     * @var callable|null
17
     */
18
    private $factory;
19
20
    /**
21
     * @var array
22
     */
23
    private $arguments;
24
25
    /**
26
     * @param callable $factory
27
     * @param array    $arguments
28
     */
29
    public function __construct(callable $factory, array $arguments = [])
30
    {
31
        $this->factory = $factory;
32
        $this->arguments = $arguments;
33
    }
34
35
    /**
36
     * Create and then execute the event listener.
37
     *
38
     * @param object $event The event that has been dispatched.
39
     *
40
     * @throws EventListenerException  If the loaded event listener is not callable.
41
     */
42
    public function __invoke(object $event): void
43
    {
44
        $listener = $this->loadListener($event);
0 ignored issues
show
Unused Code introduced by
The call to Arp\EventDispatcher\List...istener::loadListener() has too many arguments starting with $event. ( Ignorable by Annotation )

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

44
        /** @scrutinizer ignore-call */ 
45
        $listener = $this->loadListener($event);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
45
        $listener($event);
46
    }
47
48
    /**
49
     * @return callable
50
     */
51
    private function loadListener(): callable
52
    {
53
        $factory = $this->factory ?? $this->getDefaultListenerFactory();
54
        return $factory($this->arguments);
55
    }
56
57
    /**
58
     * Return the default event listener factory.
59
     *
60
     * @return \Closure
61
     */
62
    protected function getDefaultListenerFactory(): callable
63
    {
64
        return static function (string $className, array $arguments = []) {
65
            return new $className($arguments);
66
        };
67
    }
68
}
69