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 |
||
13 | class Mediator implements Observable |
||
14 | { |
||
15 | const PRIORITY_URGENT = 0; |
||
16 | const PRIORITY_HIGHEST = 1; |
||
17 | const PRIORITY_HIGH = 2; |
||
18 | const PRIORITY_NORMAL = 3; |
||
19 | const PRIORITY_LOW = 4; |
||
20 | const PRIORITY_LOWEST = 5; |
||
21 | |||
22 | /** |
||
23 | * @api |
||
24 | * |
||
25 | * @var array Holds any published events to which no handler has yet subscribed |
||
26 | * |
||
27 | * @since 1.0 |
||
28 | */ |
||
29 | public $held = []; |
||
30 | |||
31 | /** |
||
32 | * @internal |
||
33 | * |
||
34 | * @var bool Whether we should put published events for which there are no subscribers onto the list. |
||
35 | * |
||
36 | * @since 1.0 |
||
37 | */ |
||
38 | protected $holdingUnheardEvents = false; |
||
39 | |||
40 | /** |
||
41 | * @internal |
||
42 | * |
||
43 | * @var array Contains registered events and their handlers by priority |
||
44 | * |
||
45 | * @since 1.0 |
||
46 | */ |
||
47 | protected $subscribers = []; |
||
48 | |||
49 | /** |
||
50 | * Registers event handler(s) to event name(s). |
||
51 | * |
||
52 | * @api |
||
53 | * |
||
54 | * @throws BadMethodCallException if validation of any handler fails |
||
55 | * |
||
56 | * @param array $eventHandlers Associative array of event names & handlers |
||
57 | * |
||
58 | * @return array The results of firing any held events |
||
59 | * |
||
60 | * @since 1.0 |
||
61 | * |
||
62 | * @version 1.0 |
||
63 | */ |
||
64 | public function subscribe(array $eventHandlers) |
||
90 | |||
91 | /** |
||
92 | * Let any relevant subscribers know an event needs to be handled. |
||
93 | * |
||
94 | * Note: The event object can be used to share information to other similar event handlers. |
||
95 | * |
||
96 | * @api |
||
97 | * |
||
98 | * @param Event $event An event object, usually freshly created |
||
99 | * |
||
100 | * @return mixed Result of the event |
||
101 | * |
||
102 | * @since 1.0 |
||
103 | * |
||
104 | * @version 1.0 |
||
105 | */ |
||
106 | public function publish(Event $event) |
||
126 | |||
127 | /** |
||
128 | * Detach a given handler (or all) from an event name. |
||
129 | * |
||
130 | * @api |
||
131 | * |
||
132 | * @param array $eventHandlers Associative array of event names & handlers |
||
133 | * |
||
134 | * @return self This object |
||
135 | * |
||
136 | * @since 1.0 |
||
137 | * |
||
138 | * @version 1.0 |
||
139 | */ |
||
140 | public function unsubscribe(array $eventHandlers) |
||
166 | |||
167 | /** |
||
168 | * Determine if the event name has any subscribers. |
||
169 | * |
||
170 | * @api |
||
171 | * |
||
172 | * @param string $eventName The desired event's name |
||
173 | * |
||
174 | * @return bool Whether or not the event was published |
||
175 | * |
||
176 | * @since 1.0 |
||
177 | * |
||
178 | * @version 1.0 |
||
179 | */ |
||
180 | public function hasSubscribers($eventName) |
||
185 | |||
186 | /** |
||
187 | * Get or set the value of the holdingUnheardEvents property. |
||
188 | * |
||
189 | * @api |
||
190 | * |
||
191 | * @param bool|null $val true or false to set the value, omit to retrieve |
||
192 | * |
||
193 | * @return bool the value of the property |
||
194 | * |
||
195 | * @since 1.0 |
||
196 | * |
||
197 | * @version 1.0 |
||
198 | */ |
||
199 | public function holdUnheardEvents($val = null) |
||
212 | |||
213 | /** |
||
214 | * Determine if the described event has been subscribed to or not by the callback. |
||
215 | * |
||
216 | * @api |
||
217 | * |
||
218 | * @param string $eventName The desired event's name |
||
219 | * @param callable $callback The specific callback we're looking for |
||
220 | * |
||
221 | * @return int|false Subscriber's array index if found, false otherwise; use === |
||
222 | * |
||
223 | * @since 1.0 |
||
224 | * |
||
225 | * @version 1.0 |
||
226 | */ |
||
227 | public function isSubscribed($eventName, callable $callback) |
||
233 | |||
234 | /** |
||
235 | * If any events are held for $eventName, re-publish them now. |
||
236 | * |
||
237 | * @internal |
||
238 | * |
||
239 | * @param string $eventName The event name to check for |
||
240 | * |
||
241 | * @since 1.0 |
||
242 | * |
||
243 | * @version 1.0 |
||
244 | */ |
||
245 | protected function fireHeldEvents($eventName) |
||
259 | |||
260 | /** |
||
261 | * Handles inserting the new subscriber into the sorted internal array. |
||
262 | * |
||
263 | * @internal |
||
264 | * |
||
265 | * @param string $eventName The event it will listen for |
||
266 | * @param int $interval The timer interval, if it's a timer (0 if not) |
||
267 | * @param array $handler Each individual handler coming from the Observer |
||
268 | * |
||
269 | * @since 1.0 |
||
270 | * |
||
271 | * @version 1.0 |
||
272 | */ |
||
273 | protected function addNewSub($eventName, $interval, array $handler) |
||
310 | |||
311 | /** |
||
312 | * Takes care of actually calling the event handling functions |
||
313 | * |
||
314 | * @internal |
||
315 | * |
||
316 | * @param string $eventName |
||
317 | * @param Event $event |
||
318 | * @param mixed $result |
||
319 | * |
||
320 | * @since 1.0 |
||
321 | * |
||
322 | * @version 1.0 |
||
323 | */ |
||
324 | protected function fireMatchingSubs($eventName, Event $event, $result = null) |
||
357 | |||
358 | /** |
||
359 | * |
||
360 | */ |
||
361 | protected function formatCallback($eventName, $callback) |
||
380 | |||
381 | /** |
||
382 | * |
||
383 | */ |
||
384 | protected function realignPriorities($eventName, $priority, $inc = 1) |
||
390 | |||
391 | /** |
||
392 | * |
||
393 | * @param callable $callback |
||
394 | */ |
||
395 | protected function searchAndDestroy($eventName, $callback) |
||
417 | |||
418 | /** |
||
419 | * Puts an event on the held list if enabled and not a timer. |
||
420 | * |
||
421 | * @internal |
||
422 | * |
||
423 | * @param Event $event The event object to be held |
||
424 | * |
||
425 | * @since 1.0 |
||
426 | * |
||
427 | * @version 1.0 |
||
428 | */ |
||
429 | protected function tryHolding(Event $event) |
||
435 | |||
436 | /** |
||
437 | * Searches a multi-dimensional array for a value in any dimension. |
||
438 | * |
||
439 | * @internal |
||
440 | * |
||
441 | * @param mixed $needle The value to be searched for |
||
442 | * @param array $haystack The array |
||
443 | * |
||
444 | * @return int|bool The top-level key containing the needle if found, false otherwise |
||
445 | * |
||
446 | * @since 1.0 |
||
447 | * |
||
448 | * @version 1.0 |
||
449 | */ |
||
450 | protected static function arraySearchDeep($needle, array $haystack) |
||
472 | |||
473 | /** |
||
474 | * |
||
475 | */ |
||
476 | protected static function isValidHandler($handler) |
||
483 | |||
484 | /** |
||
485 | * Returns the current timestamp in milliseconds. |
||
486 | * Named for the similar function in Java. |
||
487 | * |
||
488 | * @internal |
||
489 | * |
||
490 | * @return int Current timestamp in milliseconds |
||
491 | * |
||
492 | * @since 1.0 |
||
493 | * |
||
494 | * @version 1.0 |
||
495 | */ |
||
496 | final protected static function currentTimeMillis() |
||
503 | } |
||
504 |