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 | 23 | 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 | 7 | 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 | 5 | public function addSubscriber(SubscriberInterface $sub): MediatorInterface |
|
92 | /** |
||
93 | * @param string $eventName |
||
94 | * |
||
95 | * @return array |
||
96 | * @throws \InvalidArgumentException |
||
97 | */ |
||
98 | 24 | public function getListeners(string $eventName = ''): array |
|
106 | /** |
||
107 | * @param string $eventName |
||
108 | * |
||
109 | * @return bool |
||
110 | * @throws \InvalidArgumentException |
||
111 | */ |
||
112 | 1 | 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 | 6 | public function removeListener(string $eventName, callable $listener, $priority = 0): MediatorInterface |
|
157 | /** |
||
158 | * @param array $events Events to be removed. |
||
159 | * |
||
160 | * @return MediatorInterface Fluent interface. |
||
161 | * @throws \DomainException |
||
162 | * @throws \InvalidArgumentException |
||
163 | * @throws \LengthException |
||
164 | */ |
||
165 | 2 | public function removeListenersByEventList(array $events): MediatorInterface |
|
170 | /** |
||
171 | * @param SubscriberInterface $sub |
||
172 | * |
||
173 | * @return MediatorInterface Fluent interface |
||
174 | * @throws \DomainException |
||
175 | * @throws \InvalidArgumentException |
||
176 | * @throws \LengthException |
||
177 | */ |
||
178 | 2 | public function removeSubscriber(SubscriberInterface $sub): MediatorInterface |
|
182 | /** |
||
183 | * @param string $eventName |
||
184 | * @param EventInterface|null $event |
||
185 | * |
||
186 | * @return EventInterface |
||
187 | * @throws \DomainException |
||
188 | * @throws \InvalidArgumentException |
||
189 | */ |
||
190 | 11 | public function trigger(string $eventName, EventInterface $event = \null): EventInterface |
|
214 | /** |
||
215 | * @param $eventName |
||
216 | * |
||
217 | * @throws \DomainException |
||
218 | * @throws \InvalidArgumentException |
||
219 | */ |
||
220 | 46 | protected function checkEventName(string $eventName) |
|
231 | /** |
||
232 | * @param string $eventName |
||
233 | * @param string|int $priority |
||
234 | * |
||
235 | * @return int |
||
236 | */ |
||
237 | 19 | protected function getActualPriority(string $eventName, $priority): int |
|
249 | /** |
||
250 | * @param array $events |
||
251 | * @param callable $callback |
||
252 | * |
||
253 | * @throws \LengthException |
||
254 | */ |
||
255 | 13 | protected function walkEventList(array $events, callable $callback) |
|
283 | /** |
||
284 | * @param string $eventName |
||
285 | * @param int $priority |
||
286 | * @param int $key |
||
287 | */ |
||
288 | 4 | private function bubbleUpUnsetListener(string $eventName, int $priority, int $key) |
|
298 | /** |
||
299 | * @param string $eventName |
||
300 | * |
||
301 | * @return MediatorInterface Fluent interface |
||
302 | * @throws \InvalidArgumentException |
||
303 | */ |
||
304 | 24 | private function sortListeners(string $eventName = ''): MediatorInterface |
|
323 | /** |
||
324 | * @var array $listeners |
||
325 | */ |
||
326 | private $listeners = []; |
||
327 | } |
||
328 |