Complex classes like WP_Hook 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 WP_Hook, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
18 | final class WP_Hook implements Iterator, ArrayAccess { |
||
19 | |||
20 | /** |
||
21 | * Hook callbacks. |
||
22 | * |
||
23 | * @since 4.7.0 |
||
24 | * @access public |
||
25 | * @var array |
||
26 | */ |
||
27 | public $callbacks = array(); |
||
28 | |||
29 | /** |
||
30 | * The priority keys of actively running iterations of a hook. |
||
31 | * |
||
32 | * @since 4.7.0 |
||
33 | * @access private |
||
34 | * @var array |
||
35 | */ |
||
36 | private $iterations = array(); |
||
37 | |||
38 | /** |
||
39 | * The current priority of actively running iterations of a hook. |
||
40 | * |
||
41 | * @since 4.7.0 |
||
42 | * @access private |
||
43 | * @var array |
||
44 | */ |
||
45 | private $current_priority = array(); |
||
46 | |||
47 | /** |
||
48 | * Number of levels this hook can be recursively called. |
||
49 | * |
||
50 | * @since 4.7.0 |
||
51 | * @access private |
||
52 | * @var int |
||
53 | */ |
||
54 | private $nesting_level = 0; |
||
55 | |||
56 | /** |
||
57 | * Flag for if we're current doing an action, rather than a filter. |
||
58 | * |
||
59 | * @since 4.7.0 |
||
60 | * @access private |
||
61 | * @var bool |
||
62 | */ |
||
63 | private $doing_action = false; |
||
64 | |||
65 | /** |
||
66 | * Hooks a function or method to a specific filter action. |
||
67 | * |
||
68 | * @since 4.7.0 |
||
69 | * @access public |
||
70 | * |
||
71 | * @param string $tag The name of the filter to hook the $function_to_add callback to. |
||
72 | * @param callable $function_to_add The callback to be run when the filter is applied. |
||
73 | * @param int $priority The order in which the functions associated with a |
||
74 | * particular action are executed. Lower numbers correspond with |
||
75 | * earlier execution, and functions with the same priority are executed |
||
76 | * in the order in which they were added to the action. |
||
77 | * @param int $accepted_args The number of arguments the function accepts. |
||
78 | */ |
||
79 | public function add_filter( $tag, $function_to_add, $priority, $accepted_args ) { |
||
97 | |||
98 | /** |
||
99 | * Handles reseting callback priority keys mid-iteration. |
||
100 | * |
||
101 | * @since 4.7.0 |
||
102 | * @access private |
||
103 | * |
||
104 | * @param bool|int $new_priority Optional. The priority of the new filter being added. Default false, |
||
105 | * for no priority being added. |
||
106 | * @param bool $priority_existed Optional. Flag for whether the priority already existed before the new |
||
107 | * filter was added. Default false. |
||
108 | */ |
||
109 | private function resort_active_iterations( $new_priority = false, $priority_existed = false ) { |
||
166 | |||
167 | /** |
||
168 | * Unhooks a function or method from a specific filter action. |
||
169 | * |
||
170 | * @since 4.7.0 |
||
171 | * @access public |
||
172 | * |
||
173 | * @param string $tag The filter hook to which the function to be removed is hooked. Used |
||
174 | * for building the callback ID when SPL is not available. |
||
175 | * @param callable $function_to_remove The callback to be removed from running when the filter is applied. |
||
176 | * @param int $priority The exact priority used when adding the original filter callback. |
||
177 | * @return bool Whether the callback existed before it was removed. |
||
178 | */ |
||
179 | public function remove_filter( $tag, $function_to_remove, $priority ) { |
||
194 | |||
195 | /** |
||
196 | * Checks if a specific action has been registered for this hook. |
||
197 | * |
||
198 | * @since 4.7.0 |
||
199 | * @access public |
||
200 | * |
||
201 | * @param callable|bool $function_to_check Optional. The callback to check for. Default false. |
||
202 | * @param string $tag Optional. The name of the filter hook. Used for building |
||
203 | * the callback ID when SPL is not available. Default empty. |
||
204 | * @return bool|int The priority of that hook is returned, or false if the function is not attached. |
||
205 | */ |
||
206 | public function has_filter( $tag = '', $function_to_check = false ) { |
||
224 | |||
225 | /** |
||
226 | * Checks if any callbacks have been registered for this hook. |
||
227 | * |
||
228 | * @since 4.7.0 |
||
229 | * @access public |
||
230 | * |
||
231 | * @return bool True if callbacks have been registered for the current hook, otherwise false. |
||
232 | */ |
||
233 | public function has_filters() { |
||
241 | |||
242 | /** |
||
243 | * Removes all callbacks from the current filter. |
||
244 | * |
||
245 | * @since 4.7.0 |
||
246 | * @access public |
||
247 | * |
||
248 | * @param int|bool $priority Optional. The priority number to remove. Default false. |
||
249 | */ |
||
250 | public function remove_all_filters( $priority = false ) { |
||
265 | |||
266 | /** |
||
267 | * Calls the callback functions added to a filter hook. |
||
268 | * |
||
269 | * @since 4.7.0 |
||
270 | * @access public |
||
271 | * |
||
272 | * @param mixed $value The value to filter. |
||
273 | * @param array $args Arguments to pass to callbacks. |
||
274 | * @return mixed The filtered value after all hooked functions are applied to it. |
||
275 | */ |
||
276 | public function apply_filters( $value, $args ) { |
||
312 | |||
313 | /** |
||
314 | * Executes the callback functions hooked on a specific action hook. |
||
315 | * |
||
316 | * @since 4.7.0 |
||
317 | * @access public |
||
318 | * |
||
319 | * @param mixed $args Arguments to pass to the hook callbacks. |
||
320 | */ |
||
321 | public function do_action( $args ) { |
||
330 | |||
331 | /** |
||
332 | * Processes the functions hooked into the 'all' hook. |
||
333 | * |
||
334 | * @since 4.7.0 |
||
335 | * @access public |
||
336 | * |
||
337 | * @param array $args Arguments to pass to the hook callbacks. Passed by reference. |
||
338 | */ |
||
339 | public function do_all_hook( &$args ) { |
||
353 | |||
354 | /** |
||
355 | * Return the current priority level of the currently running iteration of the hook. |
||
356 | * |
||
357 | * @since 4.7.0 |
||
358 | * @access public |
||
359 | * |
||
360 | * @return int|false If the hook is running, return the current priority level. If it isn't running, return false. |
||
361 | */ |
||
362 | public function current_priority() { |
||
369 | |||
370 | /** |
||
371 | * Normalizes filters set up before WordPress has initialized to WP_Hook objects. |
||
372 | * |
||
373 | * @since 4.7.0 |
||
374 | * @access public |
||
375 | * @static |
||
376 | * |
||
377 | * @param array $filters Filters to normalize. |
||
378 | * @return WP_Hook[] Array of normalized filters. |
||
379 | */ |
||
380 | public static function build_preinitialized_hooks( $filters ) { |
||
403 | |||
404 | /** |
||
405 | * Determines whether an offset value exists. |
||
406 | * |
||
407 | * @since 4.7.0 |
||
408 | * @access public |
||
409 | * |
||
410 | * @link http://php.net/manual/en/arrayaccess.offsetexists.php |
||
411 | * |
||
412 | * @param mixed $offset An offset to check for. |
||
413 | * @return bool True if the offset exists, false otherwise. |
||
414 | */ |
||
415 | public function offsetExists( $offset ) { |
||
418 | |||
419 | /** |
||
420 | * Retrieves a value at a specified offset. |
||
421 | * |
||
422 | * @since 4.7.0 |
||
423 | * @access public |
||
424 | * |
||
425 | * @link http://php.net/manual/en/arrayaccess.offsetget.php |
||
426 | * |
||
427 | * @param mixed $offset The offset to retrieve. |
||
428 | * @return mixed If set, the value at the specified offset, null otherwise. |
||
429 | */ |
||
430 | public function offsetGet( $offset ) { |
||
433 | |||
434 | /** |
||
435 | * Sets a value at a specified offset. |
||
436 | * |
||
437 | * @since 4.7.0 |
||
438 | * @access public |
||
439 | * |
||
440 | * @link http://php.net/manual/en/arrayaccess.offsetset.php |
||
441 | * |
||
442 | * @param mixed $offset The offset to assign the value to. |
||
443 | * @param mixed $value The value to set. |
||
444 | */ |
||
445 | public function offsetSet( $offset, $value ) { |
||
452 | |||
453 | /** |
||
454 | * Unsets a specified offset. |
||
455 | * |
||
456 | * @since 4.7.0 |
||
457 | * @access public |
||
458 | * |
||
459 | * @link http://php.net/manual/en/arrayaccess.offsetunset.php |
||
460 | * |
||
461 | * @param mixed $offset The offset to unset. |
||
462 | */ |
||
463 | public function offsetUnset( $offset ) { |
||
466 | |||
467 | /** |
||
468 | * Returns the current element. |
||
469 | * |
||
470 | * @since 4.7.0 |
||
471 | * @access public |
||
472 | * |
||
473 | * @link http://php.net/manual/en/iterator.current.php |
||
474 | * |
||
475 | * @return array Of callbacks at current priority. |
||
476 | */ |
||
477 | public function current() { |
||
480 | |||
481 | /** |
||
482 | * Moves forward to the next element. |
||
483 | * |
||
484 | * @since 4.7.0 |
||
485 | * @access public |
||
486 | * |
||
487 | * @link http://php.net/manual/en/iterator.next.php |
||
488 | * |
||
489 | * @return array Of callbacks at next priority. |
||
490 | */ |
||
491 | public function next() { |
||
494 | |||
495 | /** |
||
496 | * Returns the key of the current element. |
||
497 | * |
||
498 | * @since 4.7.0 |
||
499 | * @access public |
||
500 | * |
||
501 | * @link http://php.net/manual/en/iterator.key.php |
||
502 | * |
||
503 | * @return mixed Returns current priority on success, or NULL on failure |
||
504 | */ |
||
505 | public function key() { |
||
508 | |||
509 | /** |
||
510 | * Checks if current position is valid. |
||
511 | * |
||
512 | * @since 4.7.0 |
||
513 | * @access public |
||
514 | * |
||
515 | * @link http://php.net/manual/en/iterator.valid.php |
||
516 | * |
||
517 | * @return boolean |
||
518 | */ |
||
519 | public function valid() { |
||
522 | |||
523 | /** |
||
524 | * Rewinds the Iterator to the first element. |
||
525 | * |
||
526 | * @since 4.7.0 |
||
527 | * @access public |
||
528 | * |
||
529 | * @link http://php.net/manual/en/iterator.rewind.php |
||
530 | */ |
||
531 | public function rewind() { |
||
534 | |||
535 | } |
||
536 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.