Complex classes like Mediator 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 Mediator, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 44 | class Mediator implements MediatorInterface |
||
| 45 | { |
||
| 46 | /** |
||
| 47 | * @param string $eventName |
||
| 48 | * @param callable $listener |
||
| 49 | * @param int|string $priority |
||
| 50 | * |
||
| 51 | * @return MediatorInterface Fluent interface |
||
| 52 | * @throws \DomainException |
||
| 53 | * @throws \InvalidArgumentException |
||
| 54 | */ |
||
| 55 | public function addListener(string $eventName, callable $listener, $priority = 0): MediatorInterface |
||
| 67 | /** |
||
| 68 | * @param array $events |
||
| 69 | * |
||
| 70 | * @return MediatorInterface Fluent interface. |
||
| 71 | * @throws \DomainException |
||
| 72 | * @throws \InvalidArgumentException |
||
| 73 | * @throws \LengthException |
||
| 74 | */ |
||
| 75 | public function addListenersByEventList(array $events): MediatorInterface |
||
| 80 | /** |
||
| 81 | * @param SubscriberInterface $sub |
||
| 82 | * |
||
| 83 | * @return MediatorInterface Fluent interface |
||
| 84 | * @throws \DomainException |
||
| 85 | * @throws \InvalidArgumentException |
||
| 86 | * @throws \LengthException |
||
| 87 | */ |
||
| 88 | public function addSubscriber(SubscriberInterface $sub): MediatorInterface |
||
| 92 | /** |
||
| 93 | * @param string $eventName |
||
| 94 | * |
||
| 95 | * @return array |
||
| 96 | * @throws \InvalidArgumentException |
||
| 97 | */ |
||
| 98 | public function getListeners(string $eventName = ''): array |
||
| 106 | /** |
||
| 107 | * @param string $eventName |
||
| 108 | * |
||
| 109 | * @return bool |
||
| 110 | * @throws \InvalidArgumentException |
||
| 111 | */ |
||
| 112 | public function hasListeners(string $eventName = ''): bool |
||
| 116 | /** |
||
| 117 | * @param string $eventName |
||
| 118 | * @param callable $listener |
||
| 119 | * @param int|string $priority |
||
| 120 | * |
||
| 121 | * @return MediatorInterface Fluent interface |
||
| 122 | * @throws \DomainException |
||
| 123 | * @throws \InvalidArgumentException |
||
| 124 | */ |
||
| 125 | public function removeListener(string $eventName, callable $listener, $priority = 0): MediatorInterface |
||
| 159 | /** |
||
| 160 | * @param array $events Events to be removed. |
||
| 161 | * |
||
| 162 | * @return MediatorInterface Fluent interface. |
||
| 163 | * @throws \DomainException |
||
| 164 | * @throws \InvalidArgumentException |
||
| 165 | * @throws \LengthException |
||
| 166 | */ |
||
| 167 | public function removeListenersByEventList(array $events): MediatorInterface |
||
| 172 | /** |
||
| 173 | * @param SubscriberInterface $sub |
||
| 174 | * |
||
| 175 | * @return MediatorInterface Fluent interface |
||
| 176 | * @throws \DomainException |
||
| 177 | * @throws \InvalidArgumentException |
||
| 178 | * @throws \LengthException |
||
| 179 | */ |
||
| 180 | public function removeSubscriber(SubscriberInterface $sub): MediatorInterface |
||
| 184 | /** |
||
| 185 | * @param string $eventName |
||
| 186 | * @param EventInterface|null $event |
||
| 187 | * |
||
| 188 | * @return EventInterface |
||
| 189 | * @throws \DomainException |
||
| 190 | * @throws \InvalidArgumentException |
||
| 191 | */ |
||
| 192 | public function trigger(string $eventName, EventInterface $event = null): EventInterface |
||
| 216 | /** |
||
| 217 | * @param $eventName |
||
| 218 | * |
||
| 219 | * @throws \DomainException |
||
| 220 | * @throws \InvalidArgumentException |
||
| 221 | */ |
||
| 222 | protected function checkEventName(string $eventName) |
||
| 233 | /** |
||
| 234 | * @param string $eventName |
||
| 235 | * @param string|int $priority |
||
| 236 | * |
||
| 237 | * @return int |
||
| 238 | */ |
||
| 239 | protected function getActualPriority(string $eventName, $priority): int |
||
| 250 | /** |
||
| 251 | * @param array $events |
||
| 252 | * @param callable $callback |
||
| 253 | * |
||
| 254 | * @throws \LengthException |
||
| 255 | */ |
||
| 256 | protected function walkEventList(array $events, callable $callback) |
||
| 284 | /** |
||
| 285 | * @param string $eventName |
||
| 286 | * |
||
| 287 | * @return MediatorInterface Fluent interface |
||
| 288 | * @throws \InvalidArgumentException |
||
| 289 | */ |
||
| 290 | private function sortListeners(string $eventName = ''): MediatorInterface |
||
| 309 | /** |
||
| 310 | * @var array $listeners |
||
| 311 | */ |
||
| 312 | private $listeners = []; |
||
| 313 | } |
||
| 314 |