Complex classes like CMSMenu 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 CMSMenu, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 34 | class |
||
| 35 | CMSMenu extends Object implements IteratorAggregate, i18nEntityProvider { |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Sort by menu priority, highest to lowest |
||
| 39 | */ |
||
| 40 | const MENU_PRIORITY = 'menu_priority'; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Sort by url priority, highest to lowest |
||
| 44 | */ |
||
| 45 | const URL_PRIORITY = 'url_priority'; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * An array of changes to be made to the menu items, in the order that the changes should be |
||
| 49 | * applied. Each item is a map in one of the two forms: |
||
| 50 | * - array('type' => 'add', 'item' => new CMSMenuItem(...) ) |
||
| 51 | * - array('type' => 'remove', 'code' => 'codename' ) |
||
| 52 | */ |
||
| 53 | protected static $menu_item_changes = array(); |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Set to true if clear_menu() is called, to indicate that the default menu shouldn't be |
||
| 57 | * included |
||
| 58 | */ |
||
| 59 | protected static $menu_is_cleared = false; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Generate CMS main menu items by collecting valid |
||
| 63 | * subclasses of {@link LeftAndMain} |
||
| 64 | */ |
||
| 65 | public static function populate_menu() { |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Add a LeftAndMain controller to the CMS menu. |
||
| 71 | * |
||
| 72 | * @param string $controllerClass The class name of the controller |
||
| 73 | * @todo A director rule is added when a controller link is added, but it won't be removed |
||
| 74 | * when the item is removed. Functionality needed in {@link Director}. |
||
| 75 | */ |
||
| 76 | public static function add_controller($controllerClass) { |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Return a CMSMenuItem to add the given controller to the CMSMenu |
||
| 84 | * |
||
| 85 | * @param string $controllerClass |
||
| 86 | * @return CMSMenuItem |
||
| 87 | */ |
||
| 88 | protected static function menuitem_for_controller($controllerClass) { |
||
| 107 | |||
| 108 | |||
| 109 | /** |
||
| 110 | * Add an arbitrary URL to the CMS menu. |
||
| 111 | * |
||
| 112 | * @param string $code A unique identifier (used to create a CSS ID and its key in {@link $menu_items}) |
||
| 113 | * @param string $menuTitle The link's title in the CMS menu |
||
| 114 | * @param string $url The url of the link |
||
| 115 | * @param integer $priority The menu priority (sorting order) of the menu item. Higher priorities will be further |
||
| 116 | * left. |
||
| 117 | * @param array $attributes an array of attributes to include on the link. |
||
| 118 | * |
||
| 119 | * @return boolean The result of the operation. |
||
| 120 | */ |
||
| 121 | public static function add_link($code, $menuTitle, $url, $priority = -1, $attributes = null) { |
||
| 124 | |||
| 125 | /** |
||
| 126 | * Add a navigation item to the main administration menu showing in the top bar. |
||
| 127 | * |
||
| 128 | * uses {@link CMSMenu::$menu_items} |
||
| 129 | * |
||
| 130 | * @param string $code Unique identifier for this menu item (e.g. used by {@link replace_menu_item()} and |
||
| 131 | * {@link remove_menu_item}. Also used as a CSS-class for icon customization. |
||
| 132 | * @param string $menuTitle Localized title showing in the menu bar |
||
| 133 | * @param string $url A relative URL that will be linked in the menu bar. |
||
| 134 | * @param string $controllerClass The controller class for this menu, used to check permisssions. |
||
| 135 | * If blank, it's assumed that this is public, and always shown to users who |
||
| 136 | * have the rights to access some other part of the admin area. |
||
| 137 | * @param int $priority |
||
| 138 | * @param array $attributes an array of attributes to include on the link. |
||
| 139 | * @return bool Success |
||
| 140 | */ |
||
| 141 | public static function add_menu_item($code, $menuTitle, $url, $controllerClass = null, $priority = -1, |
||
| 150 | |||
| 151 | /** |
||
| 152 | * Get a single menu item by its code value. |
||
| 153 | * |
||
| 154 | * @param string $code |
||
| 155 | * @return array |
||
| 156 | */ |
||
| 157 | public static function get_menu_item($code) { |
||
| 161 | |||
| 162 | /** |
||
| 163 | * Get menu code for class |
||
| 164 | * |
||
| 165 | * @param string $cmsClass Controller class name |
||
| 166 | * @return string |
||
| 167 | */ |
||
| 168 | public static function get_menu_code($cmsClass) { |
||
| 171 | |||
| 172 | /** |
||
| 173 | * Get all menu entries. |
||
| 174 | * |
||
| 175 | * @return array |
||
| 176 | */ |
||
| 177 | public static function get_menu_items() { |
||
| 219 | |||
| 220 | /** |
||
| 221 | * Get all menu items that the passed member can view. |
||
| 222 | * Defaults to {@link Member::currentUser()}. |
||
| 223 | * |
||
| 224 | * @param Member $member |
||
| 225 | * @return array |
||
| 226 | */ |
||
| 227 | public static function get_viewable_menu_items($member = null) { |
||
| 252 | |||
| 253 | /** |
||
| 254 | * Removes an existing item from the menu. |
||
| 255 | * |
||
| 256 | * @param string $code Unique identifier for this menu item |
||
| 257 | */ |
||
| 258 | public static function remove_menu_item($code) { |
||
| 261 | |||
| 262 | /** |
||
| 263 | * Remove menu item by class name. |
||
| 264 | * |
||
| 265 | * @param string $className Name of class |
||
| 266 | */ |
||
| 267 | public static function remove_menu_class($className) { |
||
| 271 | |||
| 272 | /** |
||
| 273 | * Clears the entire menu |
||
| 274 | */ |
||
| 275 | public static function clear_menu() { |
||
| 279 | |||
| 280 | /** |
||
| 281 | * Replace a navigation item to the main administration menu showing in the top bar. |
||
| 282 | * |
||
| 283 | * @param string $code Unique identifier for this menu item (e.g. used by {@link replace_menu_item()} and |
||
| 284 | * {@link remove_menu_item}. Also used as a CSS-class for icon customization. |
||
| 285 | * @param string $menuTitle Localized title showing in the menu bar |
||
| 286 | * @param string $url A relative URL that will be linked in the menu bar. |
||
| 287 | * Make sure to add a matching route via {@link Director::$rules} to this url. |
||
| 288 | * @param string $controllerClass The controller class for this menu, used to check permisssions. |
||
| 289 | * If blank, it's assumed that this is public, and always shown to users who |
||
| 290 | * have the rights to access some other part of the admin area. |
||
| 291 | * @param int $priority |
||
| 292 | * @param array $attributes an array of attributes to include on the link. |
||
| 293 | * @return bool Success |
||
| 294 | */ |
||
| 295 | public static function replace_menu_item($code, $menuTitle, $url, $controllerClass = null, $priority = -1, |
||
| 309 | |||
| 310 | /** |
||
| 311 | * Add a previously built menu item object to the menu |
||
| 312 | * |
||
| 313 | * @param string $code |
||
| 314 | * @param CMSMenuItem $cmsMenuItem |
||
| 315 | */ |
||
| 316 | protected static function add_menu_item_obj($code, $cmsMenuItem) { |
||
| 323 | |||
| 324 | /** |
||
| 325 | * A utility funciton to retrieve subclasses of a given class that |
||
| 326 | * are instantiable (ie, not abstract) and have a valid menu title. |
||
| 327 | * |
||
| 328 | * Sorted by url_priority config. |
||
| 329 | * |
||
| 330 | * @todo A variation of this function could probably be moved to {@link ClassInfo} |
||
| 331 | * @param string $root The root class to begin finding subclasses |
||
| 332 | * @param boolean $recursive Look for subclasses recursively? |
||
| 333 | * @param string $sort Name of config on which to sort. Can be 'menu_priority' or 'url_priority' |
||
| 334 | * @return array Valid, unique subclasses |
||
| 335 | */ |
||
| 336 | public static function get_cms_classes($root = null, $recursive = true, $sort = self::MENU_PRIORITY) { |
||
| 371 | |||
| 372 | /** |
||
| 373 | * IteratorAggregate Interface Method. Iterates over the menu items. |
||
| 374 | */ |
||
| 375 | public function getIterator() { |
||
| 378 | |||
| 379 | /** |
||
| 380 | * Provide menu titles to the i18n entity provider |
||
| 381 | */ |
||
| 382 | public function provideI18nEntities() { |
||
| 392 | } |
||
| 393 |