| Total Complexity | 51 |
| Total Lines | 202 |
| 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) { |
||
| 44 | } |
||
| 45 | |||
| 46 | private static function _emitStatic(string $eventName, $payload = null) :void { |
||
| 52 | } |
||
| 53 | } |
||
| 54 | |||
| 55 | protected static function propagateEvent(Event $evt, array &$eventsListeners) :bool { |
||
| 56 | $listeners = &$eventsListeners[$evt->getEventName()] ?? false; |
||
| 57 | |||
| 58 | if (!$listeners) { |
||
| 59 | return true; |
||
| 60 | } |
||
| 61 | |||
| 62 | $res = true; |
||
| 63 | |||
| 64 | foreach ($listeners as $key => &$listener) { |
||
| 65 | if (in_array($evt->getEventName(), [self::EVENT_LISTENER_ADDED, self::EVENT_LISTENER_REMOVED,]) |
||
| 66 | && $listener[1] === $evt->getPayload()['callback']) { |
||
| 67 | continue; |
||
| 68 | } |
||
| 69 | |||
| 70 | call_user_func($listener[1], $evt); |
||
| 71 | |||
| 72 | if ($listener[0]) { |
||
| 73 | unset($listeners[$key]); |
||
| 74 | } |
||
| 75 | |||
| 76 | if (!$evt->isPropagatable()) { |
||
| 77 | $res = false; |
||
| 78 | break; |
||
| 79 | } |
||
| 80 | } |
||
| 81 | |||
| 82 | if (empty($listeners)) { |
||
| 83 | unset($listeners); |
||
| 84 | } |
||
| 85 | |||
| 86 | return $res; |
||
| 87 | } |
||
| 88 | |||
| 89 | private static function _getEventNamesStatic() :array { |
||
| 90 | return \array_keys(self::$staticListeners[get_called_class()] ?? []); |
||
| 91 | } |
||
| 92 | |||
| 93 | private static function _getListenersStatic(?string $eventName = null) :array { |
||
| 94 | return $eventName ? self::$staticListeners[get_called_class()][$eventName] ?? [] : self::$staticListeners[get_called_class()]; |
||
| 95 | } |
||
| 96 | |||
| 97 | private static function _onStatic(string $eventName, callable $callback) :void { |
||
| 98 | $calledClass = get_called_class(); |
||
| 99 | |||
| 100 | if (!isset(self::$staticListeners[$calledClass])) { |
||
| 101 | self::$staticListeners[$calledClass] = []; |
||
| 102 | } |
||
| 103 | |||
| 104 | self::storeCallback(self::$staticListeners[$calledClass], $eventName, $callback, false, false, self::_getMaxListenersStatic()); |
||
| 105 | |||
| 106 | self::emit(self::EVENT_LISTENER_ADDED, ['eventName' => $eventName, 'callback' => $callback, 'once' => false]); |
||
| 107 | } |
||
| 108 | |||
| 109 | private static function _onceStatic(string $eventName, $callback) :void { |
||
| 110 | $calledClass = get_called_class(); |
||
| 111 | |||
| 112 | if (!isset(self::$staticListeners[$calledClass])) { |
||
| 113 | self::$staticListeners[$calledClass] = []; |
||
| 114 | } |
||
| 115 | |||
| 116 | self::storeCallback(self::$staticListeners[$calledClass], $eventName, $callback, true, false, self::_getMaxListenersStatic()); |
||
| 117 | |||
| 118 | self::emit(self::EVENT_LISTENER_ADDED, ['eventName' => $eventName, 'callback' => $callback, 'once' => true]); |
||
| 119 | } |
||
| 120 | |||
| 121 | private static function _prependListenerStatic(string $eventName, $callback) :void { |
||
| 122 | $calledClass = get_called_class(); |
||
| 123 | |||
| 124 | if (!isset(self::$staticListeners[$calledClass])) { |
||
| 125 | self::$staticListeners[$calledClass] = []; |
||
| 126 | } |
||
| 127 | |||
| 128 | self::storeCallback(self::$staticListeners[$calledClass], $eventName, $callback, false, true, self::_getMaxListenersStatic()); |
||
| 129 | |||
| 130 | self::emit(self::EVENT_LISTENER_ADDED, ['eventName' => $eventName, 'callback' => $callback, 'once' => false]); |
||
| 131 | } |
||
| 132 | |||
| 133 | private static function _prependOnceListenerStatic(string $eventName, $callback) :void { |
||
| 143 | } |
||
| 144 | |||
| 145 | private static function _removeAllListenersStatic(?string $eventName = null) :void { |
||
| 146 | $calledClass = get_called_class(); |
||
| 147 | |||
| 148 | if (!(self::$staticListeners[$calledClass] ?? false)) { |
||
| 149 | return; |
||
| 150 | } |
||
| 151 | |||
| 152 | if ($eventName) { |
||
| 153 | if (!(self::$staticListeners[$calledClass][$eventName] ?? false)) { |
||
| 154 | return; |
||
| 155 | } |
||
| 156 | |||
| 157 | foreach (self::$staticListeners[$calledClass][$eventName] as &$callback) { |
||
| 158 | self::emit(self::EVENT_LISTENER_REMOVED, ['eventName' => $eventName, 'callback' => &$callback[1]]); |
||
| 159 | } |
||
| 160 | |||
| 161 | unset(self::$staticListeners[$calledClass][$eventName]); |
||
| 162 | self::$staticListeners[$calledClass] = array_filter(self::$staticListeners[$calledClass], function ($item) { return !empty($item); }); |
||
| 163 | |||
| 164 | return; |
||
| 165 | } |
||
| 166 | |||
| 167 | foreach (self::$staticListeners[$calledClass] as $eventName => &$listeners) { |
||
| 168 | if (!$listeners) { |
||
| 169 | continue; |
||
| 170 | } |
||
| 171 | |||
| 172 | foreach ($listeners as &$callback) { |
||
| 173 | self::emit(self::EVENT_LISTENER_REMOVED, ['eventName' => $eventName, 'callback' => &$callback[1]]); |
||
| 174 | } |
||
| 175 | } |
||
| 176 | |||
| 177 | self::$staticListeners[$calledClass] = []; |
||
| 178 | } |
||
| 179 | |||
| 180 | private static function _removeListenerStatic(string $eventName, callable $callback) :void { |
||
| 181 | $calledClass = get_called_class(); |
||
| 182 | if (!(self::$staticListeners[$calledClass][$eventName] ?? false)) { |
||
| 183 | return; |
||
| 184 | } |
||
| 185 | |||
| 186 | self::$staticListeners[$calledClass][$eventName] = array_filter(self::$staticListeners[$calledClass][$eventName], |
||
| 187 | function ($item) use (&$callback) { return $item[1] !== $callback; }); |
||
| 188 | |||
| 189 | if (empty(self::$staticListeners[$calledClass][$eventName])) { |
||
| 190 | unset(self::$staticListeners[$calledClass][$eventName]); |
||
| 191 | self::$staticListeners[$calledClass] = array_filter(self::$staticListeners[$calledClass], function ($item) { return !empty($item); }); |
||
| 192 | } |
||
| 193 | |||
| 194 | self::emit(self::EVENT_LISTENER_REMOVED, ['eventName' => $eventName, 'callback' => &$callback]); |
||
| 195 | } |
||
| 196 | |||
| 197 | private static function _setMaxListenersStatic(int $listenersCount) :void { |
||
| 203 | } |
||
| 204 | |||
| 205 | private static function _getMaxListenersStatic() :int { |
||
| 206 | return self::$staticMaxListeners[get_called_class()] ?? 10; |
||
| 207 | } |
||
| 208 | |||
| 209 | protected static function isValidCallback($callback) :bool { |
||
| 211 | } |
||
| 212 | |||
| 213 | protected static function storeCallback(array &$arrayToStore, string $eventName, &$callback, bool $once = false, bool $prepend = false, ?int $maxListeners = null) :void { |
||
| 231 | } |
||
| 232 | } |