Complex classes like SimpleEventEmitter 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 SimpleEventEmitter, and based on these observations, apply Extract Interface, too.
| 1 | <?php namespace Limoncello\Events; |
||
| 30 | class SimpleEventEmitter implements EventEmitterInterface, EventDispatcherInterface |
||
| 31 | { |
||
| 32 | /** |
||
| 33 | * All events known to system with or without corresponding event handler. |
||
| 34 | * |
||
| 35 | * @var array |
||
| 36 | */ |
||
| 37 | private $allowedEvents = []; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @var array |
||
| 41 | */ |
||
| 42 | private $subscribers = []; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @var bool |
||
| 46 | */ |
||
| 47 | private $cancellingEnabled = false; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @inheritdoc |
||
| 51 | */ |
||
| 52 | 5 | public function emit(string $eventName, array $arguments = []): void |
|
| 58 | |||
| 59 | /** |
||
| 60 | * @inheritdoc |
||
| 61 | */ |
||
| 62 | 1 | public function dispatch(EventInterface $event): void |
|
| 66 | |||
| 67 | /** |
||
| 68 | * @param string $eventName |
||
| 69 | * |
||
| 70 | * @return SimpleEventEmitter |
||
| 71 | */ |
||
| 72 | 5 | public function addAllowedEvent(string $eventName): self |
|
| 78 | |||
| 79 | /** |
||
| 80 | * @param string[] $eventNames |
||
| 81 | * |
||
| 82 | * @return SimpleEventEmitter |
||
| 83 | */ |
||
| 84 | 2 | public function addAllowedEvents(array $eventNames): self |
|
| 92 | |||
| 93 | /** |
||
| 94 | * @param string $eventName |
||
| 95 | * @param callable $subscriber |
||
| 96 | * |
||
| 97 | * @return self |
||
| 98 | * |
||
| 99 | * @SuppressWarnings(PHPMD.ElseExpression) |
||
| 100 | */ |
||
| 101 | 5 | public function subscribe(string $eventName, callable $subscriber): self |
|
| 114 | |||
| 115 | /** |
||
| 116 | * @param string $eventName |
||
| 117 | * @param callable $subscriber |
||
| 118 | * |
||
| 119 | * @return self |
||
| 120 | */ |
||
| 121 | 1 | public function unSubscribe(string $eventName, callable $subscriber): self |
|
| 136 | |||
| 137 | /** |
||
| 138 | * @return bool |
||
| 139 | */ |
||
| 140 | 5 | public function isCancellingEnabled(): bool |
|
| 144 | |||
| 145 | /** |
||
| 146 | * @return self |
||
| 147 | */ |
||
| 148 | 2 | public function enableCancelling(): self |
|
| 154 | |||
| 155 | /** |
||
| 156 | * @return self |
||
| 157 | */ |
||
| 158 | 3 | public function disableCancelling(): self |
|
| 164 | |||
| 165 | /** |
||
| 166 | * @param array $data |
||
| 167 | * |
||
| 168 | * @return self |
||
|
|
|||
| 169 | */ |
||
| 170 | 3 | public function setData(array $data): self |
|
| 180 | |||
| 181 | /** |
||
| 182 | * @return array |
||
| 183 | */ |
||
| 184 | 4 | public function getData(): array |
|
| 205 | |||
| 206 | /** |
||
| 207 | * @param string $eventName |
||
| 208 | * |
||
| 209 | * @return bool |
||
| 210 | */ |
||
| 211 | 5 | protected function isEventAllowed(string $eventName): bool |
|
| 215 | |||
| 216 | /** |
||
| 217 | * @return array |
||
| 218 | */ |
||
| 219 | 4 | protected function getAllowedEvents(): array |
|
| 223 | |||
| 224 | /** |
||
| 225 | * @param array $allowedEvents |
||
| 226 | * |
||
| 227 | * @return self |
||
| 228 | */ |
||
| 229 | 3 | protected function setAllowedEvents(array $allowedEvents): self |
|
| 235 | |||
| 236 | /** |
||
| 237 | * @return array |
||
| 238 | */ |
||
| 239 | 5 | protected function getSubscribers(): array |
|
| 243 | |||
| 244 | /** |
||
| 245 | * @param callable[] $subscribers |
||
| 246 | * |
||
| 247 | * @return self |
||
| 248 | */ |
||
| 249 | 3 | protected function setSubscribers(array $subscribers): self |
|
| 255 | |||
| 256 | /** |
||
| 257 | * @param string $eventName |
||
| 258 | * @param array $arguments |
||
| 259 | * |
||
| 260 | * @return void |
||
| 261 | */ |
||
| 262 | 5 | protected function emitWithoutCancellingPropagationCheck(string $eventName, array $arguments = []): void |
|
| 268 | |||
| 269 | /** |
||
| 270 | * @param string $eventName |
||
| 271 | * @param array $arguments |
||
| 272 | * |
||
| 273 | * @return void |
||
| 274 | */ |
||
| 275 | 1 | protected function emitWithCancellingPropagationCheck(string $eventName, array $arguments = []): void |
|
| 283 | |||
| 284 | /** |
||
| 285 | * @param string $eventName |
||
| 286 | * |
||
| 287 | * @return array |
||
| 288 | */ |
||
| 289 | 5 | private function getEventSubscribers(string $eventName): array |
|
| 299 | |||
| 300 | /** |
||
| 301 | * @param string $eventName |
||
| 302 | * @param callable[] $eventSubscribers |
||
| 303 | * |
||
| 304 | * @return self |
||
| 305 | */ |
||
| 306 | 1 | private function setEventSubscribers(string $eventName, array $eventSubscribers): self |
|
| 312 | |||
| 313 | /** |
||
| 314 | * This debugging function checks subscribers are |
||
| 315 | * [ |
||
| 316 | * ... |
||
| 317 | * 'string_event_name' => [static callable, static callable, ...], |
||
| 318 | * ... |
||
| 319 | * ] |
||
| 320 | * |
||
| 321 | * @param array $subscribers |
||
| 322 | * |
||
| 323 | * @return bool |
||
| 324 | */ |
||
| 325 | 4 | private function checkAllSubscribersAreStatic(array $subscribers): bool |
|
| 344 | |||
| 345 | /** |
||
| 346 | * @param $mightBeCallable |
||
| 347 | * |
||
| 348 | * @return null|ReflectionMethod |
||
| 349 | * |
||
| 350 | * @SuppressWarnings(PHPMD.ElseExpression) |
||
| 351 | */ |
||
| 352 | 6 | private function parseStaticMethod($mightBeCallable): ?ReflectionMethod |
|
| 377 | |||
| 378 | /** |
||
| 379 | * @param ReflectionMethod $staticMethod |
||
| 380 | * |
||
| 381 | * @return callable |
||
| 382 | */ |
||
| 383 | 5 | private function getUnifiedStaticMethodRepresentation(ReflectionMethod $staticMethod): callable |
|
| 387 | } |
||
| 388 |
This check compares the return type specified in the
@returnannotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.