Complex classes like WordPoints_Hook_Router 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 WordPoints_Hook_Router, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
31 | class WordPoints_Hook_Router { |
||
32 | |||
33 | /** |
||
34 | * The actions registry object. |
||
35 | * |
||
36 | * @since 1.0.0 |
||
37 | * |
||
38 | * @var WordPoints_Hook_Actions |
||
39 | */ |
||
40 | protected $actions; |
||
41 | |||
42 | /** |
||
43 | * The events registry object. |
||
44 | * |
||
45 | * @since 1.0.0 |
||
46 | * |
||
47 | * @var WordPoints_Hook_Events |
||
48 | */ |
||
49 | protected $events; |
||
50 | |||
51 | /** |
||
52 | * The actions, indexed by WordPress action/filter hooks. |
||
53 | * |
||
54 | * The indexes are of this format: "$action_or_filter_name,$priority". |
||
55 | * |
||
56 | * @since 1.0.0 |
||
57 | * |
||
58 | * @var array |
||
59 | */ |
||
60 | protected $action_index = array(); |
||
61 | |||
62 | /** |
||
63 | * The events, indexed by action slug and action type. |
||
64 | * |
||
65 | * The events are the indexes in the arrays for each action type, the values in |
||
66 | * the arrays are unused. |
||
67 | * |
||
68 | * @since 1.0.0 |
||
69 | * |
||
70 | * @var array[] |
||
71 | */ |
||
72 | protected $event_index = array(); |
||
73 | |||
74 | /** |
||
75 | * The reactor hit types, indexed by reactor and action type. |
||
76 | * |
||
77 | * Tells us what type of hit to tell a reactor to perform when it is hit by a |
||
78 | * fire of a particular type of action. |
||
79 | * |
||
80 | * @since 1.0.0 |
||
81 | * |
||
82 | * @var string[][] |
||
83 | */ |
||
84 | protected $reactor_index = array(); |
||
85 | |||
86 | /** |
||
87 | * @since 1.0.0 |
||
88 | */ |
||
89 | public function __call( $name, $args ) { |
||
101 | |||
102 | /** |
||
103 | * Routes a WordPress action to WordPoints hook actions, and fires their events. |
||
104 | * |
||
105 | * @since 1.0.0 |
||
106 | * |
||
107 | * @param string $name The action ID. This is not the slug of a hook action, but |
||
108 | * rather a unique ID for the WordPress action based on the |
||
109 | * action name and the priority. |
||
110 | * @param array $args The args the action was fired with. |
||
111 | */ |
||
112 | protected function route_action( $name, $args ) { |
||
166 | |||
167 | /** |
||
168 | * Fire an event at each of the reactions. |
||
169 | * |
||
170 | * @since 1.0.0 |
||
171 | * |
||
172 | * @param string $action_type The type of action triggering |
||
173 | * this fire of this event. |
||
174 | * @param string $event_slug The slug of the event. |
||
175 | * @param WordPoints_Hook_Event_Args $event_args The event args. |
||
176 | */ |
||
177 | public function fire_event( |
||
210 | |||
211 | /** |
||
212 | * Fire for a particular reaction. |
||
213 | * |
||
214 | * @since 1.0.0 |
||
215 | * |
||
216 | * @param WordPoints_Hook_Fire $fire The hook fire object. |
||
217 | */ |
||
218 | protected function fire_reaction( $fire ) { |
||
259 | |||
260 | /** |
||
261 | * Register an action with the router. |
||
262 | * |
||
263 | * The arg number will be automatically determined based on $data['arg_index'] |
||
264 | * and $data['requirements']. So in most cases $arg_number may be omitted. |
||
265 | * |
||
266 | * @since 1.0.0 |
||
267 | * |
||
268 | * @param string $slug The slug of the action. |
||
269 | * @param array $args { |
||
270 | * Other arguments. |
||
271 | * |
||
272 | * @type string $action The name of the WordPress action for this hook action. |
||
273 | * @type int $priority The priority for the WordPress action. Default: 10. |
||
274 | * @type int $arg_number The number of args the action object expects. Default: 1. |
||
275 | * @type array $data { |
||
276 | * Args that will be passed to the action object's constructor. |
||
277 | * |
||
278 | * @type int[] $arg_index List of args (starting from 0), indexed by slug. |
||
279 | * @type array $requirements List of requirements, indexed by arg index (from 0). |
||
280 | * } |
||
281 | * } |
||
282 | */ |
||
283 | public function add_action( $slug, array $args ) { |
||
334 | |||
335 | /** |
||
336 | * Deregister an action with the router. |
||
337 | * |
||
338 | * @since 1.0.0 |
||
339 | * |
||
340 | * @param string $slug The action slug. |
||
341 | */ |
||
342 | public function remove_action( $slug ) { |
||
360 | |||
361 | /** |
||
362 | * Hook an event to an action. |
||
363 | * |
||
364 | * @since 1.0.0 |
||
365 | * |
||
366 | * @param string $event_slug The slug of the event. |
||
367 | * @param string $action_slug The slug of the action. |
||
368 | * @param string $action_type The type of action. Default is 'fire'. |
||
369 | */ |
||
370 | public function add_event_to_action( $event_slug, $action_slug, $action_type = 'fire' ) { |
||
373 | |||
374 | /** |
||
375 | * Unhook an event from an action. |
||
376 | * |
||
377 | * @since 1.0.0 |
||
378 | * |
||
379 | * @param string $event_slug The slug of the event. |
||
380 | * @param string $action_slug The slug of the action. |
||
381 | * @param string $action_type The type of action. Default is 'fire'. |
||
382 | */ |
||
383 | public function remove_event_from_action( $event_slug, $action_slug, $action_type = 'fire' ) { |
||
386 | |||
387 | /** |
||
388 | * Hook an action type to a reactor. |
||
389 | * |
||
390 | * @since 1.0.0 |
||
391 | * |
||
392 | * @param string $action_type The slug of the action type. |
||
393 | * @param string $reactor_slug The slug of the reactor. |
||
394 | * @param string $hit_type The type of hit the reactor should perform when |
||
395 | * hit by this type of event. |
||
396 | */ |
||
397 | public function add_action_type_to_reactor( $action_type, $reactor_slug, $hit_type ) { |
||
400 | |||
401 | /** |
||
402 | * Unhook an action type from a reactor. |
||
403 | * |
||
404 | * @since 1.0.0 |
||
405 | * |
||
406 | * @param string $action_type The slug of the action type. |
||
407 | * @param string $reactor_slug The slug of the reactor. |
||
408 | */ |
||
409 | public function remove_action_type_from_reactor( $action_type, $reactor_slug ) { |
||
412 | |||
413 | /** |
||
414 | * Get the event index. |
||
415 | * |
||
416 | * @since 1.0.0 |
||
417 | * |
||
418 | * @return array[] The event index. |
||
419 | */ |
||
420 | public function get_event_index() { |
||
428 | |||
429 | /** |
||
430 | * Get the reactor index. |
||
431 | * |
||
432 | * @since 1.0.0 |
||
433 | * |
||
434 | * @return string[][] |
||
435 | */ |
||
436 | public function get_reactor_index() { |
||
444 | } |
||
445 | |||
447 |