GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — 1.1 ( d6c0e8...886204 )
by Simon
68:37 queued 25:15
created

ProjectEnabledDispatcher::addSubscriber()   B

Complexity

Conditions 7
Paths 4

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 7

Importance

Changes 2
Bugs 0 Features 2
Metric Value
c 2
b 0
f 2
dl 0
loc 14
ccs 12
cts 12
cp 1
rs 8.2222
cc 7
eloc 9
nc 4
nop 1
crap 7
1
<?php
2
namespace SmoothPhp\EventDispatcher;
3
4
use SmoothPhp\Contracts\EventDispatcher\EventDispatcher;
5
use SmoothPhp\Contracts\EventDispatcher\Projection;
6
use SmoothPhp\Contracts\EventDispatcher\Subscriber;
7
8
/**
9
 * Class ProjectEnabledDispatcher
10
 * @package SmoothPhp\EventDispatcher
11
 * @author Simon Bennett <[email protected]>
12
 */
13
final class ProjectEnabledDispatcher implements EventDispatcher
14
{
15
    /**
16
     * @var array
17
     */
18
    private $listeners = [];
19
20
    /**
21
     * @param string $eventName
22
     * @param array $arguments
23
     * @param bool $runProjectionsOnly
24
     */
25 24
    public function dispatch($eventName, array $arguments, $runProjectionsOnly = false)
26
    {
27 24
        if (!isset($this->listeners[$eventName])) {
28 3
            return;
29
        }
30 21
        foreach ($this->getListenersInOrder($eventName) as $listener) {
31 21
            if ($this->listenerCanRun($runProjectionsOnly, $listener)) {
32 21
                call_user_func_array($listener, $arguments);
33 21
            }
34 21
        }
35 21
    }
36
37
    /**
38
     * @param string $eventName
39
     * @param callable $callable
40
     * @param int $priority
41
     */
42 24
    public function addListener($eventName, callable $callable, $priority = 0)
43
    {
44 24
        $dotEventName = str_replace('\\', '.', $eventName);
45 24
        $this->listeners[$dotEventName][$priority][] = $callable;
46 24
        unset($this->sorted[$dotEventName]);
47
48 24
    }
49
50
51
    /**
52
     * @param Subscriber $subscriber
53
     * @return void
54
     */
55 6
    public function addSubscriber(Subscriber $subscriber)
56
    {
57 6
        foreach ($subscriber->getSubscribedEvents() as $eventName => $params) {
58 6
            if (is_string($params)) {
59 3
                $this->addListener($eventName, array($subscriber, $params));
60 6
            } elseif (is_string($params[0])) {
61 6
                $this->addListener($eventName, array($subscriber, $params[0]), isset($params[1]) ? $params[1] : 0);
62 6
            } else {
63 3
                foreach ($params as $listener) {
64 3
                    $this->addListener($eventName, array($subscriber, $listener[0]), isset($listener[1]) ? $listener[1] : 0);
65 3
                }
66
            }
67 6
        }
68 6
    }
69
70
    /**
71
     * @param $runProjectionsOnly
72
     * @param $listener
73
     * @return bool
74
     */
75 21
    protected function listenerCanRun($runProjectionsOnly, $listener)
76
    {
77 21
        return !$runProjectionsOnly || (is_array($listener) && $listener[0] instanceof Projection);
78
    }
79
80
    /**
81
     * @param $eventName
82
     * @return array
83
     */
84 21
    protected function getListenersInOrder($eventName)
85
    {
86 21
        if (!isset($this->listeners[$eventName])) {
87
            return [];
88
        }
89 21
        if (!isset($this->sorted[$eventName])) {
0 ignored issues
show
Bug introduced by
The property sorted does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
90 21
            $this->sortListeners($eventName);
91 21
        }
92
93 21
        return $this->sorted[$eventName];
94
    }
95
96
    /**
97
     * Sorts the internal list of listeners for the given event by priority.
98
     *
99
     * @param string $eventName The name of the event.
100
     */
101 21
    private function sortListeners($eventName)
102
    {
103 21
        $this->sorted[$eventName] = array();
104
105 21
        krsort($this->listeners[$eventName]);
106 21
        $this->sorted[$eventName] = call_user_func_array('array_merge', $this->listeners[$eventName]);
107
    }
108
}