Total Complexity | 62 |
Total Lines | 225 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like EventEmitterGlobal often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use EventEmitterGlobal, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
10 | final class EventEmitterGlobal implements Interfaces\EventEmitterGlobal |
||
11 | { |
||
12 | /** |
||
13 | * @var \xobotyi\emittr\EventEmitterGlobal; |
||
14 | */ |
||
15 | private static $instance; |
||
16 | |||
17 | private $listeners; |
||
18 | |||
19 | private $maxListenersCount = 10; |
||
20 | |||
21 | public static function getInstance() :self { |
||
22 | if (!self::$instance) { |
||
23 | self::$instance = new self(); |
||
24 | } |
||
25 | |||
26 | return self::$instance; |
||
27 | } |
||
28 | |||
29 | public static function propagateEvent(Event $event, array &$eventsListeners) :bool { |
||
30 | $eventName = $event->getEventName(); |
||
31 | |||
32 | if (empty($eventsListeners[$eventName])) { |
||
33 | return true; |
||
34 | } |
||
35 | |||
36 | $listeners = &$eventsListeners[$eventName]; |
||
37 | $result = true; |
||
38 | |||
39 | foreach ($listeners as $key => &$listener) { |
||
40 | if (in_array($eventName, [EventEmitter::EVENT_LISTENER_ADDED, EventEmitter::EVENT_LISTENER_REMOVED,]) && |
||
41 | $event->getPayload()['callback'] && $event->getPayload()['callback'] === $listener[1]) { |
||
42 | continue; |
||
43 | } |
||
44 | |||
45 | call_user_func($listener[1], $event); |
||
46 | |||
47 | if ($listener[0]) { |
||
48 | unset($listeners[$key]); |
||
49 | } |
||
50 | |||
51 | if (!$event->isPropagatable()) { |
||
52 | $result = false; |
||
53 | break; |
||
54 | } |
||
55 | } |
||
56 | |||
57 | if (empty($listeners)) { |
||
58 | unset($listeners); |
||
59 | } |
||
60 | |||
61 | return $result; |
||
62 | } |
||
63 | |||
64 | public static function isValidCallback($callback) :bool { |
||
65 | return is_string($callback) || is_callable($callback) || (is_array($callback) && count($callback) === 2 && is_string($callback[0]) && is_string($callback[1])); |
||
66 | } |
||
67 | |||
68 | public static function storeCallback(array &$arrayToStore, string $eventName, $callback, int $maxListeners = 10, bool $once = false, bool $prepend = false) :void { |
||
84 | } |
||
85 | |||
86 | public function loadListeners(array $listeners) :self { |
||
87 | foreach ($listeners as $className => &$classListeners) { |
||
113 | } |
||
114 | |||
115 | public function on(string $className, string $eventName, $callback) :self { |
||
116 | if (!isset($this->listeners[$className][$eventName])) { |
||
117 | $this->listeners[$className][$eventName] = []; |
||
118 | } |
||
119 | |||
120 | self::storeCallback($this->listeners[$className], $eventName, $callback, $this->maxListenersCount, false, false); |
||
121 | |||
122 | return $this; |
||
123 | } |
||
124 | |||
125 | public function once(string $className, string $eventName, $callback) :self { |
||
126 | if (!isset($this->listeners[$className][$eventName])) { |
||
127 | $this->listeners[$className][$eventName] = []; |
||
128 | } |
||
129 | |||
130 | self::storeCallback($this->listeners[$className], $eventName, $callback, $this->maxListenersCount, true, false); |
||
131 | |||
132 | return $this; |
||
133 | } |
||
134 | |||
135 | public function prependListener(string $className, string $eventName, $callback) :self { |
||
143 | } |
||
144 | |||
145 | public function prependOnceListener(string $className, string $eventName, $callback) :self { |
||
146 | if (!isset($this->listeners[$className][$eventName])) { |
||
147 | $this->listeners[$className][$eventName] = []; |
||
148 | } |
||
149 | |||
150 | self::storeCallback($this->listeners[$className], $eventName, $callback, $this->maxListenersCount, true, true); |
||
151 | |||
152 | return $this; |
||
153 | } |
||
154 | |||
155 | public function off(string $className, string $eventName, $callback) :self { |
||
156 | if (empty($this->listeners[$className][$eventName])) { |
||
157 | return $this; |
||
158 | } |
||
159 | |||
160 | $this->listeners[$className][$eventName] = \array_values(\array_filter($this->listeners[$className][$eventName], function ($item) use (&$callback) { return $item[1] !== $callback; })); |
||
161 | |||
162 | if (empty($this->listeners[$className][$eventName])) { |
||
163 | unset($this->listeners[$className][$eventName]); |
||
164 | } |
||
165 | |||
166 | if (empty($this->listeners[$className])) { |
||
167 | unset($this->listeners[$className]); |
||
168 | } |
||
169 | |||
170 | return $this; |
||
171 | } |
||
172 | |||
173 | public function removeAllListeners(?string $className = null, ?string $eventName = null) :self { |
||
174 | if ($className === null) { |
||
175 | $this->listeners = []; |
||
176 | |||
177 | return $this; |
||
178 | } |
||
179 | |||
180 | if (empty($this->listeners[$className])) { |
||
181 | return $this; |
||
182 | } |
||
183 | |||
184 | if ($eventName === null) { |
||
185 | unset($this->listeners[$className]); |
||
186 | |||
187 | return $this; |
||
188 | } |
||
189 | |||
190 | if (empty($this->listeners[$className][$eventName])) { |
||
191 | return $this; |
||
192 | } |
||
193 | |||
194 | unset($this->listeners[$className][$eventName]); |
||
195 | |||
196 | if (empty($this->listeners[$className])) { |
||
197 | unset($this->listeners[$className]); |
||
198 | } |
||
199 | |||
200 | return $this; |
||
201 | } |
||
202 | |||
203 | public function getEventNames(string $className) :array { |
||
204 | return empty($this->listeners[$className]) ? [] : \array_keys($this->listeners[$className]); |
||
205 | } |
||
206 | |||
207 | public function getListeners(?string $className = null, ?string $eventName = null) :array { |
||
209 | } |
||
210 | |||
211 | public function getMaxListenersCount() :int { |
||
212 | return $this->maxListenersCount; |
||
213 | } |
||
214 | |||
215 | public function setMaxListenersCount(int $maxListenersCount) :self { |
||
223 | } |
||
224 | |||
225 | public function propagateEventGlobal(Event $event) :bool { |
||
235 | } |
||
236 | } |