| Total Complexity | 51 |
| Total Lines | 191 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like EventEmitterStatic 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 EventEmitterStatic, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 29 | class EventEmitterStatic |
||
| 30 | { |
||
| 31 | public const EVENT_LISTENER_ADDED = 'listenerAdded'; |
||
| 32 | public const EVENT_LISTENER_REMOVED = 'listenerRemoved'; |
||
| 33 | |||
| 34 | protected static $staticListeners = []; |
||
| 35 | protected static $staticMaxListeners = []; |
||
| 36 | |||
| 37 | public static function __callStatic($name, $arguments) { |
||
| 38 | $calledClass = get_called_class(); |
||
| 39 | if (method_exists($calledClass, '_' . $name . 'Static')) { |
||
| 40 | return forward_static_call_array([$calledClass, '_' . $name . 'Static'], $arguments); |
||
| 41 | } |
||
| 42 | |||
| 43 | throw new \Error('Call to undefined method ' . $calledClass . '::' . $name . '()'); |
||
| 44 | } |
||
| 45 | |||
| 46 | private static function _emitStatic(string $evtName, $payload = null) :void { |
||
| 47 | $calledClass = get_called_class(); |
||
| 48 | |||
| 49 | $event = new Event($evtName, $payload, $calledClass); |
||
| 50 | if (!(self::$staticListeners[$calledClass] ?? false) || self::propagateEvent($event, self::$staticListeners[$calledClass])) { |
||
| 51 | EventEmitterGlobal::propagateClassEvent($event); |
||
| 52 | } |
||
| 53 | } |
||
| 54 | |||
| 55 | protected static function propagateEvent(Event $evt, array &$listeners) :bool { |
||
| 56 | if (!($listeners = &$listeners[$evt->getEventName()] ?? false)) { |
||
| 57 | return true; |
||
| 58 | } |
||
| 59 | |||
| 60 | $res = true; |
||
| 61 | |||
| 62 | foreach ($listeners as $key => &$listener) { |
||
| 63 | if (($evt->getEventName() === self::EVENT_LISTENER_ADDED || $evt->getEventName() === self::EVENT_LISTENER_REMOVED) && |
||
| 64 | $listener[1] === $evt->getPayload()['callback']) { |
||
| 65 | continue; |
||
| 66 | } |
||
| 67 | |||
| 68 | call_user_func($listener[1], $evt); |
||
| 69 | |||
| 70 | if ($listener[0]) { |
||
| 71 | unset($listeners[$key]); |
||
| 72 | } |
||
| 73 | |||
| 74 | if (!$evt->isPropagatable()) { |
||
| 75 | $res = false; |
||
| 76 | break; |
||
| 77 | } |
||
| 78 | } |
||
| 79 | |||
| 80 | if (!count($listeners)) { |
||
| 81 | unset($listeners); |
||
| 82 | } |
||
| 83 | |||
| 84 | return $res; |
||
| 85 | } |
||
| 86 | |||
| 87 | private static function _getEventNamesStatic() :array { |
||
| 88 | return \array_keys(self::$staticListeners[get_called_class()] ?? []); |
||
| 89 | } |
||
| 90 | |||
| 91 | private static function _getListenersStatic(?string $eventName = null) :array { |
||
| 93 | } |
||
| 94 | |||
| 95 | private static function _onStatic(string $evtName, callable $callback) :void { |
||
| 96 | $calledClass = get_called_class(); |
||
| 97 | |||
| 98 | if (!isset(self::$staticListeners[$calledClass])) { |
||
| 99 | self::$staticListeners[$calledClass] = []; |
||
| 100 | } |
||
| 101 | |||
| 102 | self::storeCallback(self::$staticListeners[$calledClass], $evtName, $callback, false, false, self::_getMaxListenersStatic()); |
||
| 103 | |||
| 104 | self::emit(self::EVENT_LISTENER_ADDED, ['eventName' => $evtName, 'callback' => $callback, 'once' => false]); |
||
| 105 | } |
||
| 106 | |||
| 107 | private static function _onceStatic(string $evtName, $callback) :void { |
||
| 108 | $calledClass = get_called_class(); |
||
| 109 | |||
| 110 | if (!isset(self::$staticListeners[$calledClass])) { |
||
| 111 | self::$staticListeners[$calledClass] = []; |
||
| 112 | } |
||
| 113 | |||
| 114 | self::storeCallback(self::$staticListeners[$calledClass], $evtName, $callback, true, false, self::_getMaxListenersStatic()); |
||
| 115 | |||
| 116 | self::emit(self::EVENT_LISTENER_ADDED, ['eventName' => $evtName, 'callback' => $callback, 'once' => true]); |
||
| 117 | } |
||
| 118 | |||
| 119 | private static function _prependListenerStatic(string $evtName, $callback) :void { |
||
| 120 | $calledClass = get_called_class(); |
||
| 121 | |||
| 122 | if (!isset(self::$staticListeners[$calledClass])) { |
||
| 123 | self::$staticListeners[$calledClass] = []; |
||
| 124 | } |
||
| 125 | |||
| 126 | self::storeCallback(self::$staticListeners[$calledClass], $evtName, $callback, false, true, self::_getMaxListenersStatic()); |
||
| 127 | |||
| 128 | self::emit(self::EVENT_LISTENER_ADDED, ['eventName' => $evtName, 'callback' => $callback, 'once' => false]); |
||
| 129 | } |
||
| 130 | |||
| 131 | private static function _prependOnceListenerStatic(string $evtName, $callback) :void { |
||
| 141 | } |
||
| 142 | |||
| 143 | private static function _removeAllListenersStatic(?string $evtName = null) :void { |
||
| 144 | $calledClass = get_called_class(); |
||
| 145 | |||
| 146 | if (!isset(self::$staticListeners[$calledClass])) { |
||
| 147 | return; |
||
| 148 | } |
||
| 149 | |||
| 150 | if (!$evtName) { |
||
| 151 | foreach (self::$staticListeners as $eventname => &$listeners) { |
||
| 152 | foreach ($listeners as &$callback) { |
||
| 153 | self::emit(self::EVENT_LISTENER_REMOVED, ['eventName' => $evtName, 'callback' => &$callback[1]]); |
||
| 154 | } |
||
| 155 | } |
||
| 156 | |||
| 157 | self::$staticListeners[$calledClass] = []; |
||
| 158 | |||
| 159 | return; |
||
| 160 | } |
||
| 161 | |||
| 162 | if (self::$staticListeners[$calledClass][$evtName] ?? false) { |
||
| 163 | foreach (self::$staticListeners[$calledClass][$evtName] as &$callback) { |
||
| 164 | self::emit(self::EVENT_LISTENER_REMOVED, ['eventName' => $evtName, 'callback' => &$callback[1]]); |
||
| 165 | } |
||
| 166 | |||
| 167 | unset(self::$staticListeners[$calledClass][$evtName]); |
||
| 168 | } |
||
| 169 | } |
||
| 170 | |||
| 171 | private static function _removeListenerStatic(string $evtName, callable $callback) :void { |
||
| 172 | $calledClass = get_called_class(); |
||
| 173 | if (!(self::$staticListeners[$calledClass][$evtName] ?? false)) { |
||
| 174 | return; |
||
| 175 | } |
||
| 176 | |||
| 177 | self::$staticListeners[$calledClass][$evtName] = array_filter(self::$staticListeners[$calledClass][$evtName], function ($item) use (&$callback) { return $item[1] !== $callback; }); |
||
| 178 | |||
| 179 | if (empty(self::$staticListeners[$calledClass][$evtName])) { |
||
| 180 | unset(self::$staticListeners[$calledClass][$evtName]); |
||
| 181 | } |
||
| 182 | |||
| 183 | self::emit(self::EVENT_LISTENER_REMOVED, ['eventName' => $evtName, 'callback' => &$callback]); |
||
| 184 | } |
||
| 185 | |||
| 186 | private static function _setMaxListenersStatic(int $listenersCount) :void { |
||
| 192 | } |
||
| 193 | |||
| 194 | private static function _getMaxListenersStatic() :int { |
||
| 195 | return self::$staticMaxListeners[get_called_class()] ?? 10; |
||
| 196 | } |
||
| 197 | |||
| 198 | protected static function isValidCallback($callback) :bool { |
||
| 200 | } |
||
| 201 | |||
| 202 | protected static function storeCallback(array &$arrayToStore, string $eventName, &$callback, bool $once = false, bool $prepend = false, ?int $maxListeners = null) :void { |
||
| 220 | } |
||
| 221 | } |