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

LazyListenerConfig   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 9
eloc 35
c 1
b 0
f 0
dl 0
loc 80
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getListener() 0 28 4
A __construct() 0 25 5
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Arp\EventDispatcher\Listener;
6
7
use Arp\EventDispatcher\Listener\Exception\EventListenerException;
8
use Arp\Factory\Exception\FactoryException;
9
use Arp\Factory\FactoryInterface;
10
11
/**
12
 * @author  Alex Patterson <[email protected]>
13
 * @package Arp\EventDispatcher\Listener
14
 */
15
final class LazyListenerConfig extends ListenerConfig
16
{
17
    /**
18
     * @var string|callable|FactoryInterface
19
     */
20
    private $factory;
21
22
    /**
23
     * @var array
24
     */
25
    private $options;
26
27
    /**
28
     * @param FactoryInterface|string $factory
29
     * @param string                  $eventName
30
     * @param int                     $priority
31
     * @param array                   $options
32
     *
33
     * @throws EventListenerException
34
     */
35
    public function __construct($factory, string $eventName, int $priority = 1, array $options = [])
36
    {
37
        if (
38
            is_callable($factory)
39
            && !$factory instanceof FactoryInterface
40
            && !is_a($factory, FactoryInterface::class, true)
41
        ) {
42
            throw new EventListenerException(
43
                sprintf(
44
                    'The \'listener\' argument must be a \'string\', \'callable\' or an object of type \'%s\'; '
45
                    . '\'%s\' provided in \'%s\'',
46
                    FactoryInterface::class,
47
                    is_object($factory) ? get_class($factory) : gettype($factory),
0 ignored issues
show
introduced by
The condition is_object($factory) is always false.
Loading history...
48
                    static::class
49
                )
50
            );
51
        }
52
53
        $this->factory = $factory;
54
        $this->options = $options;
55
56
        $listener = static function () {
57
        };
58
59
        parent::__construct($listener, $eventName, $priority);
0 ignored issues
show
Bug introduced by
$listener of type callable is incompatible with the type string expected by parameter $eventName of Arp\EventDispatcher\List...erConfig::__construct(). ( Ignorable by Annotation )

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

59
        parent::__construct(/** @scrutinizer ignore-type */ $listener, $eventName, $priority);
Loading history...
60
    }
61
62
    /**
63
     * @return callable
64
     *
65
     * @throws EventListenerException
66
     */
67
    public function getListener(): callable
68
    {
69
        $listener = new LazyListener();
0 ignored issues
show
Bug introduced by
The call to Arp\EventDispatcher\List...Listener::__construct() has too few arguments starting with factory. ( Ignorable by Annotation )

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

69
        $listener = /** @scrutinizer ignore-call */ new LazyListener();

This check compares calls to functions or methods with their respective definitions. If the call has less 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...
Unused Code introduced by
The assignment to $listener is dead and can be removed.
Loading history...
70
71
        $listener = $this->listener;
72
        $factory = $this->factory;
73
74
        try {
75
            if (is_string($factory)) {
76
                $this->factory = $factory = new $factory();
77
            }
78
79
            if ($factory instanceof FactoryInterface) {
80
                $this->listener = $listener = $factory->create($this->options);
81
            }
82
        } catch (FactoryException $e) {
83
            throw new EventListenerException(
84
                sprintf(
85
                    'Failed to lazy load the event listener from factory class \'%s\': %s',
86
                    get_class($factory),
0 ignored issues
show
Bug introduced by
It seems like $factory can also be of type callable; however, parameter $object of get_class() does only seem to accept object, 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

86
                    get_class(/** @scrutinizer ignore-type */ $factory),
Loading history...
87
                    $e->getMessage()
88
                ),
89
                $e->getCode(),
90
                $e
91
            );
92
        }
93
94
        return $listener;
95
    }
96
}
97