1 | <?php |
||
12 | class EventDispatcher |
||
13 | { |
||
14 | /** |
||
15 | * All registered event listener objects and their assigned events. |
||
16 | * |
||
17 | * @var array |
||
18 | */ |
||
19 | private $listeners = []; |
||
20 | |||
21 | /** |
||
22 | * Adds an event listener. |
||
23 | * |
||
24 | * @param string|array $eventNames The event name(s) to listen for. |
||
25 | * @param object $listener The event listener object. |
||
26 | * @return self |
||
27 | * @throws \InvalidArgumentException If the listener does not contain the event method. |
||
28 | */ |
||
29 | public function addListener($eventNames, $listener) |
||
40 | |||
41 | /** |
||
42 | * Adds an event subscriber. |
||
43 | * |
||
44 | * @param EventSubscriberInterface $subscriber |
||
45 | * @return self |
||
46 | */ |
||
47 | public function addSubscriber(EventSubscriberInterface $subscriber) |
||
51 | |||
52 | /** |
||
53 | * Dispatches an event to all registered listeners. |
||
54 | * |
||
55 | * @param EventArguments|null $arguments |
||
56 | * @return self |
||
57 | */ |
||
58 | public function dispatch($eventName, EventArguments $arguments = null) |
||
69 | |||
70 | /** |
||
71 | * Gets the key for a listener object. |
||
72 | * |
||
73 | * @param object $listener |
||
74 | * @return string |
||
75 | * @throws \InvalidArgumentException If the listener is not an object. |
||
76 | */ |
||
77 | protected function getListenerKey($listener) |
||
84 | |||
85 | /** |
||
86 | * Gets all registered listeners for an event name. |
||
87 | * |
||
88 | * @param string $eventName |
||
89 | * @return array|null |
||
90 | */ |
||
91 | protected function getListeners($eventName) |
||
98 | |||
99 | /** |
||
100 | * Determines if registered listeners exist for an event name. |
||
101 | * |
||
102 | * @param string $eventName |
||
103 | * @return bool |
||
104 | */ |
||
105 | public function hasListeners($eventName) |
||
109 | |||
110 | /** |
||
111 | * Removes an event listener, if registered. |
||
112 | * |
||
113 | * @param string|array $eventNames The event name(s) to listen for. |
||
114 | * @param object $listener The event listener object. |
||
115 | * @return self |
||
116 | */ |
||
117 | public function removeListener($eventNames, $listener) |
||
127 | |||
128 | /** |
||
129 | * Removes an event subscriber. |
||
130 | * |
||
131 | * @param EventSubscriberInterface $subscriber |
||
132 | * @return self |
||
133 | */ |
||
134 | public function removeSubscriber(EventSubscriberInterface $subscriber) |
||
138 | } |
||
139 |
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.