Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like 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 Hook, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 23 | class Hook { |
||
| 24 | /** |
||
| 25 | * An array of the callbacks for each hook. |
||
| 26 | * @var array |
||
| 27 | */ |
||
| 28 | protected static $hooks = array(); |
||
| 29 | /** |
||
| 30 | * A copy of the HookOverride_extend file. |
||
| 31 | * @var string |
||
| 32 | */ |
||
| 33 | private static $hookFile; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Add a callback. |
||
| 37 | * |
||
| 38 | * A callback is called either before a method runs or after. The callback |
||
| 39 | * is passed an array of arguments or return value which it can freely |
||
| 40 | * manipulate. If the callback runs before the method and sets the arguments |
||
| 41 | * array to false (or causes an error), the method will not be run. |
||
| 42 | * Callbacks before a method are passed the arguments given when the method |
||
| 43 | * was called, while callbacks after a method are passed the return value |
||
| 44 | * (in an array) of that method. |
||
| 45 | * |
||
| 46 | * The callback can receive up to 5 arguments, in this order: |
||
| 47 | * |
||
| 48 | * - &$arguments - An array of either arguments or a return value. |
||
| 49 | * - $name - The name of the hook. |
||
| 50 | * - &$object - The object on which the hook caught a method call. |
||
| 51 | * - &$function - A callback for the method call which was caught. Altering |
||
| 52 | * this will cause a different function/method to run. |
||
| 53 | * - &$data - An array in which callbacks can store data to communicate with |
||
| 54 | * later callbacks. |
||
| 55 | * |
||
| 56 | * A hook is the name of whatever method it should catch. A hook can also |
||
| 57 | * have an arbitrary name, but be wary that it may already exist and it may |
||
| 58 | * result in your callback being falsely called. In order to reduce the |
||
| 59 | * chance of this, always use a plus sign (+) and your component's name to |
||
| 60 | * begin arbitrary hook names. E.g. "+com_games_player_bonus". |
||
| 61 | * |
||
| 62 | * If the hook is called explicitly, callbacks defined to run before the |
||
| 63 | * hook will run immediately followed by callbacks defined to run after. |
||
| 64 | * |
||
| 65 | * A negative $order value means the callback will be run before the method, |
||
| 66 | * while a positive value means it will be run after. The smaller the order |
||
| 67 | * number, the sooner the callback will be run. You can think of the order |
||
| 68 | * value as a timeline of callbacks, zero (0) being the actual method being |
||
| 69 | * hooked. |
||
| 70 | * |
||
| 71 | * Additional identical callbacks can be added in order to have a callback |
||
| 72 | * called multiple times for one hook. |
||
| 73 | * |
||
| 74 | * The hook "all" is a pseudo hook which will run regardless of what was |
||
| 75 | * actually caught. Callbacks attached to the "all" hook will run before |
||
| 76 | * callbacks attached to the actual hook. |
||
| 77 | * |
||
| 78 | * Note: Be careful to avoid recursive callbacks, as they may result in an |
||
| 79 | * infinite loop. All methods under $_ are automatically hooked. |
||
| 80 | * |
||
| 81 | * @param string $hook The name of the hook to catch. |
||
| 82 | * @param int $order The order can be negative, which will run before the method, or positive, which will run after the method. It cannot be zero. |
||
| 83 | * @param callback The callback. |
||
| 84 | * @return array An array containing the IDs of the new callback and all matching callbacks. |
||
| 85 | * @uses \SciActive\Hook::sortCallbacks() To resort the callback array in the correct order. |
||
| 86 | */ |
||
| 87 | public static function addCallback($hook, $order, $function) { |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Delete a callback by its ID. |
||
| 99 | * |
||
| 100 | * @param string $hook The name of the callback's hook. |
||
| 101 | * @param int $id The ID of the callback. |
||
| 102 | * @return int 1 if the callback was deleted, 2 if it didn't exist. |
||
| 103 | */ |
||
| 104 | public static function delCallbackByID($hook, $id) { |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Get the array of callbacks. |
||
| 114 | * |
||
| 115 | * Callbacks are stored in arrays inside this array. The keys of this array |
||
| 116 | * are the name of the hook whose callbacks are contained in its value as an |
||
| 117 | * array. Each array contains the values $order, $function, in that order. |
||
| 118 | * |
||
| 119 | * @return array An array of callbacks. |
||
| 120 | */ |
||
| 121 | public static function getCallbacks() { |
||
| 124 | |||
| 125 | /** |
||
| 126 | * Hook an object. |
||
| 127 | * |
||
| 128 | * This hooks all (public) methods defined in the given object. |
||
| 129 | * |
||
| 130 | * @param object &$object The object to hook. |
||
| 131 | * @param string $prefix The prefix used to call the object's methods. Usually something like "$object->". |
||
| 132 | * @param bool $recursive Whether to hook objects recursively. |
||
| 133 | * @return bool True on success, false on failure. |
||
| 134 | */ |
||
| 135 | public static function hookObject(&$object, $prefix = '', $recursive = true) { |
||
| 244 | |||
| 245 | /** |
||
| 246 | * Run the callbacks for a given hook. |
||
| 247 | * |
||
| 248 | * Each callback is run and passed the array of arguments, and the name of |
||
| 249 | * the given hook. If any callback changes $arguments to FALSE, the |
||
| 250 | * following callbacks will not be called, and FALSE will be returned. |
||
| 251 | * |
||
| 252 | * @param string $name The name of the hook. |
||
| 253 | * @param array &$arguments An array of arguments to be passed to the callbacks. |
||
| 254 | * @param string $type The type of callbacks to run. 'before', 'after', or 'all'. |
||
| 255 | * @param mixed &$object The object on which the hook was called. |
||
| 256 | * @param callback &$function The function which is called at "0". You can change this in the "before" callbacks to effectively takeover a function. |
||
| 257 | * @param array &$data A data array for callback communication. |
||
| 258 | */ |
||
| 259 | public static function runCallbacks($name, &$arguments = array(), $type = 'all', &$object = null, &$function = null, &$data = array()) { |
||
| 281 | |||
| 282 | /** |
||
| 283 | * Sort function for callback sorting. |
||
| 284 | * |
||
| 285 | * This assures that callbacks are executed in the correct order. Callback |
||
| 286 | * IDs are preserved as long as uasort() is used. |
||
| 287 | * |
||
| 288 | * @param array $a The first callback in the comparison. |
||
| 289 | * @param array $b The second callback in the comparison. |
||
| 290 | * @return int 0 for equal, -1 for less than, 1 for greater than. |
||
| 291 | * @access private |
||
| 292 | */ |
||
| 293 | private static function sortCallbacks($a, $b) { |
||
| 299 | |||
| 300 | public static function getHookFile() { |
||
| 303 | } |
||
| 304 | |||
| 306 |