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 — master ( c4fd22...d26df2 )
by Hong
02:46
created

EventManager::attach()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 8
rs 9.4285
cc 2
eloc 5
nc 2
nop 3
1
<?php
2
/**
3
 * Phossa Project
4
 *
5
 * PHP version 5.4
6
 *
7
 * @category  Library
8
 * @package   Phossa2\Event
9
 * @copyright Copyright (c) 2016 phossa.com
10
 * @license   http://mit-license.org/ MIT License
11
 * @link      http://www.phossa.com/
12
 */
13
/*# declare(strict_types=1); */
14
15
namespace Phossa2\Event;
16
17
use Phossa2\Shared\Base\ObjectAbstract;
18
use Phossa2\Event\Interfaces\EventInterface;
19
use Phossa2\Event\Interfaces\EventQueueInterface;
20
use Phossa2\Event\Interfaces\EventResultInterface;
21
use Phossa2\Event\Interfaces\EventManagerInterface;
22
23
/**
24
 * EventManager
25
 *
26
 * A basic implementation of EventManagerInterface
27
 *
28
 * @package Phossa2\Event
29
 * @author  Hong Zhang <[email protected]>
30
 * @see     ObjectAbstract
31
 * @see     EventManagerInterface
32
 * @version 2.1.0
33
 * @since   2.0.0 added
34
 * @since   2.1.0 updated to use the new EventManagerInterface
35
 */
36
class EventManager extends ObjectAbstract implements EventManagerInterface
37
{
38
    /**
39
     * Events managing
40
     *
41
     * @var    EventQueueInterface[]
42
     * @access protected
43
     */
44
    protected $events = [];
45
46
    /**
47
     * {@inheritDoc}
48
     */
49
    public function attach($event, $callback, $priority = 0)
50
    {
51
        if (!$this->hasEvent($event)) {
52
            $this->events[$event] = $this->newEventQueue();
53
        }
54
        $this->events[$event]->insert($callback, $priority);
55
        return true;
56
    }
57
58
    /**
59
     * {@inheritDoc}
60
     */
61
    public function detach($event, $callback)
62
    {
63
        if ($this->hasEvent($event)) {
64
            $this->removeEventCallable($event, $callback);
65
        } elseif ('' === $event) {
66
            $this->events = []; // remove all events
67
        }
68
        return true;
69
    }
70
71
    /**
72
     * {@inheritDoc}
73
     */
74
    public function clearListeners($event)
75
    {
76
        $this->detach($event, null);
0 ignored issues
show
Documentation introduced by
null is of type null, but the function expects a callable.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
77
    }
78
79
    /**
80
     * {@inheritDoc}
81
     */
82
    public function trigger($event, $target = null, $argv = array())
83
    {
84
        // result
85
        $res = true;
86
87
        // make sure is an event
88
        $evt = $this->newEvent($event, $target, $argv);
89
90
        // get handler queue
91
        $queue = $this->getMatchedQueue($evt->getName());
92
93
        // walk thru the queue
94
        foreach ($queue as $q) {
95
            // execute the handler
96
            $res = $q['data']($evt);
97
98
            // add result to event
99
            if ($evt instanceof EventResultInterface) {
100
                $evt->addResult($res);
101
            }
102
103
            // break out if event stopped
104
            if ($evt->isPropagationStopped()) {
105
                break;
106
            }
107
        }
108
109
        return $res;
110
    }
111
112
    /**
113
     * Has $eventName been bound ?
114
     *
115
     * @param  string $eventName
116
     * @return bool
117
     * @access protected
118
     */
119
    protected function hasEvent(/*# string */ $eventName)/*# : bool */
120
    {
121
        return isset($this->events[$eventName]);
122
    }
123
124
    /**
125
     * Create a new event
126
     *
127
     * @param  string|EventInterface $eventName
128
     * @param  object|string|null $target
129
     * @param  array $parameters
130
     * @return EventInterface
131
     * @access protected
132
     */
133
    protected function newEvent(
134
        $eventName,
135
        $target,
136
        array $parameters
137
    )/*# : EventInterface */ {
138
        if (is_object($eventName)) {
139
            return $eventName;
140
        } else {
141
            return new Event($eventName, $target, $parameters);
142
        }
143
    }
144
145
    /**
146
     * Get a new event queue
147
     *
148
     * @return EventQueueInterface
149
     * @access protected
150
     */
151
    protected function newEventQueue()/*# : EventQueueInterface */
152
    {
153
        return new EventQueue();
154
    }
155
156
    /**
157
     * Get related event handler queue for this $eventName
158
     *
159
     * @param  string $eventName
160
     * @return EventQueueInterface
161
     * @access protected
162
     */
163
    protected function getMatchedQueue(
164
        /*# : string */ $eventName
165
    )/*# : EventQueueInterface */ {
166
        if ($this->hasEvent($eventName)) {
167
            return $this->events[$eventName];
168
        } else {
169
            return $this->newEventQueue();
170
        }
171
    }
172
173
    /**
174
     * Remove event or its callable
175
     *
176
     * @param  string $eventName
177
     * @param  callable|null $callable
178
     * @access protected
179
     */
180
    protected function removeEventCallable(
181
        /*# string */ $eventName,
182
        $callable
183
    ) {
184
        if (null === $callable) {
185
            // remove all callables
186
            $this->events[$eventName]->flush();
187
        } else {
188
            // remove one callable
189
            $this->events[$eventName]->remove($callable);
190
        }
191
192
        // when count is zeror, remove queue
193
        if (count($this->events[$eventName]) === 0) {
194
            unset($this->events[$eventName]);
195
        }
196
    }
197
}
198