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 declare(strict_types=1); |
||
| 41 | class SimpleEventEmitter implements EventEmitterInterface, EventDispatcherInterface |
||
| 42 | { |
||
| 43 | /** |
||
| 44 | * All events known to system with or without corresponding event handler. |
||
| 45 | * |
||
| 46 | * @var array |
||
| 47 | */ |
||
| 48 | private $allowedEvents = []; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @var array |
||
| 52 | */ |
||
| 53 | private $subscribers = []; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @var bool |
||
| 57 | */ |
||
| 58 | private $cancellingEnabled = false; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @inheritdoc |
||
| 62 | */ |
||
| 63 | 5 | public function emit(string $eventName, array $arguments = []): void |
|
| 69 | |||
| 70 | /** |
||
| 71 | * @inheritdoc |
||
| 72 | */ |
||
| 73 | 1 | public function dispatch(EventInterface $event): void |
|
| 77 | |||
| 78 | /** |
||
| 79 | * @param string $eventName |
||
| 80 | * |
||
| 81 | * @return SimpleEventEmitter |
||
| 82 | */ |
||
| 83 | 5 | public function addAllowedEvent(string $eventName): self |
|
| 89 | |||
| 90 | /** |
||
| 91 | * @param string[] $eventNames |
||
| 92 | * |
||
| 93 | * @return SimpleEventEmitter |
||
| 94 | */ |
||
| 95 | 2 | public function addAllowedEvents(array $eventNames): self |
|
| 103 | |||
| 104 | /** |
||
| 105 | * @param string $eventName |
||
| 106 | * @param callable $subscriber |
||
| 107 | * |
||
| 108 | * @return self |
||
| 109 | * |
||
| 110 | * @SuppressWarnings(PHPMD.ElseExpression) |
||
| 111 | */ |
||
| 112 | 5 | public function subscribe(string $eventName, callable $subscriber): self |
|
| 125 | |||
| 126 | /** |
||
| 127 | * @param string $eventName |
||
| 128 | * @param callable $subscriber |
||
| 129 | * |
||
| 130 | * @return self |
||
| 131 | */ |
||
| 132 | 1 | public function unSubscribe(string $eventName, callable $subscriber): self |
|
| 147 | |||
| 148 | /** |
||
| 149 | * @return bool |
||
| 150 | */ |
||
| 151 | 5 | public function isCancellingEnabled(): bool |
|
| 155 | |||
| 156 | /** |
||
| 157 | * @return self |
||
| 158 | */ |
||
| 159 | 2 | public function enableCancelling(): self |
|
| 165 | |||
| 166 | /** |
||
| 167 | * @return self |
||
| 168 | */ |
||
| 169 | 3 | public function disableCancelling(): self |
|
| 175 | |||
| 176 | /** |
||
| 177 | * @param array $data |
||
| 178 | * |
||
| 179 | * @return self |
||
|
|
|||
| 180 | */ |
||
| 181 | 3 | public function setData(array $data): self |
|
| 191 | |||
| 192 | /** |
||
| 193 | * @return array |
||
| 194 | */ |
||
| 195 | 4 | public function getData(): array |
|
| 216 | |||
| 217 | /** |
||
| 218 | * @param string $eventName |
||
| 219 | * |
||
| 220 | * @return bool |
||
| 221 | */ |
||
| 222 | 5 | protected function isEventAllowed(string $eventName): bool |
|
| 226 | |||
| 227 | /** |
||
| 228 | * @return array |
||
| 229 | */ |
||
| 230 | 4 | protected function getAllowedEvents(): array |
|
| 234 | |||
| 235 | /** |
||
| 236 | * @param array $allowedEvents |
||
| 237 | * |
||
| 238 | * @return self |
||
| 239 | */ |
||
| 240 | 3 | protected function setAllowedEvents(array $allowedEvents): self |
|
| 246 | |||
| 247 | /** |
||
| 248 | * @return array |
||
| 249 | */ |
||
| 250 | 5 | protected function getSubscribers(): array |
|
| 254 | |||
| 255 | /** |
||
| 256 | * @param callable[] $subscribers |
||
| 257 | * |
||
| 258 | * @return self |
||
| 259 | */ |
||
| 260 | 3 | protected function setSubscribers(array $subscribers): self |
|
| 266 | |||
| 267 | /** |
||
| 268 | * @param string $eventName |
||
| 269 | * @param array $arguments |
||
| 270 | * |
||
| 271 | * @return void |
||
| 272 | */ |
||
| 273 | 5 | protected function emitWithoutCancellingPropagationCheck(string $eventName, array $arguments = []): void |
|
| 279 | |||
| 280 | /** |
||
| 281 | * @param string $eventName |
||
| 282 | * @param array $arguments |
||
| 283 | * |
||
| 284 | * @return void |
||
| 285 | */ |
||
| 286 | 1 | protected function emitWithCancellingPropagationCheck(string $eventName, array $arguments = []): void |
|
| 294 | |||
| 295 | /** |
||
| 296 | * @param string $eventName |
||
| 297 | * |
||
| 298 | * @return array |
||
| 299 | */ |
||
| 300 | 5 | private function getEventSubscribers(string $eventName): array |
|
| 310 | |||
| 311 | /** |
||
| 312 | * @param string $eventName |
||
| 313 | * @param callable[] $eventSubscribers |
||
| 314 | * |
||
| 315 | * @return self |
||
| 316 | */ |
||
| 317 | 1 | private function setEventSubscribers(string $eventName, array $eventSubscribers): self |
|
| 323 | |||
| 324 | /** |
||
| 325 | * This debugging function checks subscribers are |
||
| 326 | * [ |
||
| 327 | * ... |
||
| 328 | * 'string_event_name' => [static callable, static callable, ...], |
||
| 329 | * ... |
||
| 330 | * ] |
||
| 331 | * |
||
| 332 | * @param array $subscribers |
||
| 333 | * |
||
| 334 | * @return bool |
||
| 335 | */ |
||
| 336 | 4 | private function checkAllSubscribersAreStatic(array $subscribers): bool |
|
| 355 | |||
| 356 | /** |
||
| 357 | * @param $mightBeCallable |
||
| 358 | * |
||
| 359 | * @return null|ReflectionMethod |
||
| 360 | * |
||
| 361 | * @SuppressWarnings(PHPMD.ElseExpression) |
||
| 362 | */ |
||
| 363 | 6 | private function parseStaticMethod($mightBeCallable): ?ReflectionMethod |
|
| 388 | |||
| 389 | /** |
||
| 390 | * @param ReflectionMethod $staticMethod |
||
| 391 | * |
||
| 392 | * @return callable |
||
| 393 | */ |
||
| 394 | 5 | private function getUnifiedStaticMethodRepresentation(ReflectionMethod $staticMethod): callable |
|
| 398 | } |
||
| 399 |
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.