Passed
Push — master ( 69d06b...aefa73 )
by Gabriel
06:27 queued 12s
created

ProviderUtilitiesTrait::getListenerId()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 7
nc 4
nop 1
dl 0
loc 14
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace ListenerProviders\Traits;
4
5
use Fig\EventDispatcher\ParameterDeriverTrait;
6
7
/**
8
 * Trait ProviderUtilitiesTrait
9
 * @package ByTIC\EventDispatcher\ListenerProvider
10
 */
11
trait ProviderUtilitiesTrait
12
{
13
    use ParameterDeriverTrait;
14
15
    /**
16
     * Derives a predictable ID from the listener if possible.
17
     *
18
     * @param callable $listener
19
     *   The listener for which to derive an ID.
20
     * @return string|null
21
     *   The derived ID if possible or null if no reasonable ID could be derived.
22
     * @todo If we add support for annotations or similar for identifying listeners that logic would go here.
23
     *
24
     * It's OK for this method to return null, as OrderedCollection will generate a random
25
     * ID if necessary.  It will also handle duplicates for us.  This method is just a
26
     * suggestion.
27
     *
28
     */
29
    protected function getListenerId(callable $listener): ?string
30
    {
31
        if ($this->isFunctionCallable($listener)) {
0 ignored issues
show
Deprecated Code introduced by
The function ListenerProviders\Traits...t::isFunctionCallable() has been deprecated: No longer necessary so will be removed at some point in the future. ( Ignorable by Annotation )

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

31
        if (/** @scrutinizer ignore-deprecated */ $this->isFunctionCallable($listener)) {

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
32
            // Function callables are strings, so use that directly.
33
            return (string)$listener;
34
        }
35
        if ($this->isClassCallable($listener)) {
36
            return $listener[0] . '::' . $listener[1];
37
        }
38
        if ($this->isObjectCallable($listener)) {
0 ignored issues
show
Deprecated Code introduced by
The function ListenerProviders\Traits...ait::isObjectCallable() has been deprecated: No longer necessary so will be removed at some point in the future. ( Ignorable by Annotation )

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

38
        if (/** @scrutinizer ignore-deprecated */ $this->isObjectCallable($listener)) {

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
39
            return get_class($listener[0]) . '::' . $listener[1];
40
        }
41
        // Anything else we can't derive an ID for logically.
42
        return null;
43
    }
44
45
    /**
46
     * @param $event
47
     * @return string
48
     */
49
    public static function getEventType($event)
50
    {
51
        if (is_object($event)) {
52
            if (method_exists($event, 'getName')) {
53
                return $event->getName();
54
            }
55
            return get_class($event);
56
        }
57
        return $event;
58
    }
59
}
60