Complex classes like Hooks 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 Hooks, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 35 | class Hooks |
||
| 36 | { |
||
| 37 | /** |
||
| 38 | * Hooks by location and appname |
||
| 39 | * |
||
| 40 | * @var array $location => $app => array($file, ...) |
||
| 41 | */ |
||
| 42 | protected static $locations; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Executes all the hooks (the user has rights to) for a given location |
||
| 46 | * |
||
| 47 | * If no $order given, hooks are executed in the order of the applications! |
||
| 48 | * |
||
| 49 | * @param string|array $args location-name as string or array with keys location and |
||
| 50 | * further data to be passed to the hook, if its a new method-hook |
||
| 51 | * @param string|array $order appname(s as value), which should be executes first |
||
| 52 | * @param boolean $no_permission_check if True execute all hooks, not only the ones a user has rights to |
||
| 53 | * $no_permission_check should *ONLY* be used when it *HAS* to be. (jengo) |
||
| 54 | * @return array with results of each hook call (with appname as key) and value: |
||
| 55 | * - False if no hook exists (should no longer be the case), |
||
| 56 | * - True if old hook exists and |
||
| 57 | * - array of return-values, if an app implements more then one hook |
||
| 58 | * - whatever the new method-hook returns (can be True or False too!) |
||
| 59 | */ |
||
| 60 | public static function process($args, $order = array(), $no_permission_check = False) |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Executes a single hook of a given location and application |
||
| 89 | * |
||
| 90 | * @param string|array $args location-name as string or array with keys location, appname and |
||
| 91 | * further data to be passed to the hook, if its a new method-hook |
||
| 92 | * @param string $appname name of the app, which's hook to execute, if empty the current app is used |
||
| 93 | * @param boolean $no_permission_check =false if True execute all hooks, not only the ones a user has rights to |
||
| 94 | * $no_permission_check should *ONLY* be used when it *HAS* to be. (jengo) |
||
| 95 | * @param boolean $try_unregistered =false If true, try to include old file-hook anyway (for setup) |
||
| 96 | * @return mixed False if no hook exists, True if old hook exists and whatever the new method-hook returns (can be True or False too!). |
||
| 97 | */ |
||
| 98 | public static function single($args, $appname = '', $no_permission_check = False, $try_unregistered = False) |
||
| 170 | |||
| 171 | /** |
||
| 172 | * loop through the applications and count the apps implementing a hooks |
||
| 173 | * |
||
| 174 | * @param string $location location-name |
||
| 175 | * @return int the number of found hooks |
||
| 176 | */ |
||
| 177 | public static function count($location) |
||
| 183 | |||
| 184 | /** |
||
| 185 | * check if a given hook for an application is registered |
||
| 186 | * |
||
| 187 | * @param string $location location-name |
||
| 188 | * @param string $app appname |
||
| 189 | * @param boolean $return_methods =false true: return hook-method(s) |
||
| 190 | * @return int|array the number of found hooks or for $return_methods array with methods |
||
| 191 | */ |
||
| 192 | public static function exists($location, $app, $return_methods=false) |
||
| 199 | |||
| 200 | /** |
||
| 201 | * check which apps implement a given hook |
||
| 202 | * |
||
| 203 | * @param string $location location-name |
||
| 204 | * @return array of apps implementing given hook |
||
| 205 | */ |
||
| 206 | public static function implemented($location) |
||
| 213 | |||
| 214 | /** |
||
| 215 | * Disable a hook for this request |
||
| 216 | * |
||
| 217 | * @param string $hook |
||
| 218 | * @return boolean true if hook existed, false otherwise |
||
| 219 | */ |
||
| 220 | static public function disable($hook) |
||
| 230 | |||
| 231 | /** |
||
| 232 | * Read all hooks into self::$locations |
||
| 233 | * |
||
| 234 | * @param boolan $force_rescan =false true: do not use instance cache |
||
| 235 | */ |
||
| 236 | public static function read($force_rescan=false) |
||
| 279 | |||
| 280 | /** |
||
| 281 | * Static function to build pgp encryption sidebox menu |
||
| 282 | * @param type $appname application name |
||
| 283 | */ |
||
| 284 | public static function pgp_encryption_menu($appname) |
||
| 295 | } |
||
| 296 |
This function has been deprecated. The supplier of the file has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.