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 |
||
| 18 | class CMSMenu extends Object implements IteratorAggregate, i18nEntityProvider { |
||
| 19 | |||
| 20 | /** |
||
| 21 | * Sort by menu priority, highest to lowest |
||
| 22 | */ |
||
| 23 | const MENU_PRIORITY = 'menu_priority'; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * Sort by url priority, highest to lowest |
||
| 27 | */ |
||
| 28 | const URL_PRIORITY = 'url_priority'; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * An array of changes to be made to the menu items, in the order that the changes should be |
||
| 32 | * applied. Each item is a map in one of the two forms: |
||
| 33 | * - array('type' => 'add', 'item' => new CMSMenuItem(...) ) |
||
| 34 | * - array('type' => 'remove', 'code' => 'codename' ) |
||
| 35 | */ |
||
| 36 | protected static $menu_item_changes = array(); |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Set to true if clear_menu() is called, to indicate that the default menu shouldn't be |
||
| 40 | * included |
||
| 41 | */ |
||
| 42 | protected static $menu_is_cleared = false; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Generate CMS main menu items by collecting valid |
||
| 46 | * subclasses of {@link LeftAndMain} |
||
| 47 | */ |
||
| 48 | public static function populate_menu() { |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Add a LeftAndMain controller to the CMS menu. |
||
| 54 | * |
||
| 55 | * @param string $controllerClass The class name of the controller |
||
| 56 | * @return The result of the operation |
||
| 57 | * @todo A director rule is added when a controller link is added, but it won't be removed |
||
| 58 | * when the item is removed. Functionality needed in {@link Director}. |
||
| 59 | */ |
||
| 60 | public static function add_controller($controllerClass) { |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Return a CMSMenuItem to add the given controller to the CMSMenu |
||
| 68 | */ |
||
| 69 | protected static function menuitem_for_controller($controllerClass) { |
||
| 87 | |||
| 88 | |||
| 89 | /** |
||
| 90 | * Add an arbitrary URL to the CMS menu. |
||
| 91 | * |
||
| 92 | * @param string $code A unique identifier (used to create a CSS ID and its key in {@link $menu_items}) |
||
| 93 | * @param string $menuTitle The link's title in the CMS menu |
||
| 94 | * @param string $url The url of the link |
||
| 95 | * @param integer $priority The menu priority (sorting order) of the menu item. Higher priorities will be further |
||
| 96 | * left. |
||
| 97 | * @param array $attributes an array of attributes to include on the link. |
||
| 98 | * |
||
| 99 | * @return boolean The result of the operation. |
||
| 100 | */ |
||
| 101 | public static function add_link($code, $menuTitle, $url, $priority = -1, $attributes = null) { |
||
| 104 | |||
| 105 | /** |
||
| 106 | * Add a navigation item to the main administration menu showing in the top bar. |
||
| 107 | * |
||
| 108 | * uses {@link CMSMenu::$menu_items} |
||
| 109 | * |
||
| 110 | * @param string $code Unique identifier for this menu item (e.g. used by {@link replace_menu_item()} and |
||
| 111 | * {@link remove_menu_item}. Also used as a CSS-class for icon customization. |
||
| 112 | * @param string $menuTitle Localized title showing in the menu bar |
||
| 113 | * @param string $url A relative URL that will be linked in the menu bar. |
||
| 114 | * @param string $controllerClass The controller class for this menu, used to check permisssions. |
||
| 115 | * If blank, it's assumed that this is public, and always shown to users who |
||
| 116 | * have the rights to access some other part of the admin area. |
||
| 117 | * @param array $attributes an array of attributes to include on the link. |
||
| 118 | * |
||
| 119 | * @return boolean Success |
||
| 120 | */ |
||
| 121 | public static function add_menu_item($code, $menuTitle, $url, $controllerClass = null, $priority = -1, |
||
| 130 | |||
| 131 | /** |
||
| 132 | * Get a single menu item by its code value. |
||
| 133 | * |
||
| 134 | * @param string $code |
||
| 135 | * @return array |
||
| 136 | */ |
||
| 137 | public static function get_menu_item($code) { |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Get all menu entries. |
||
| 144 | * |
||
| 145 | * @return array |
||
| 146 | */ |
||
| 147 | public static function get_menu_items() { |
||
| 186 | |||
| 187 | /** |
||
| 188 | * Get all menu items that the passed member can view. |
||
| 189 | * Defaults to {@link Member::currentUser()}. |
||
| 190 | * |
||
| 191 | * @param Member $member |
||
| 192 | * @return array |
||
| 193 | */ |
||
| 194 | public static function get_viewable_menu_items($member = null) { |
||
| 219 | |||
| 220 | /** |
||
| 221 | * Removes an existing item from the menu. |
||
| 222 | * |
||
| 223 | * @param string $code Unique identifier for this menu item |
||
| 224 | */ |
||
| 225 | public static function remove_menu_item($code) { |
||
| 228 | |||
| 229 | /** |
||
| 230 | * Clears the entire menu |
||
| 231 | */ |
||
| 232 | public static function clear_menu() { |
||
| 236 | |||
| 237 | /** |
||
| 238 | * Replace a navigation item to the main administration menu showing in the top bar. |
||
| 239 | * |
||
| 240 | * @param string $code Unique identifier for this menu item (e.g. used by {@link replace_menu_item()} and |
||
| 241 | * {@link remove_menu_item}. Also used as a CSS-class for icon customization. |
||
| 242 | * @param string $menuTitle Localized title showing in the menu bar |
||
| 243 | * @param string $url A relative URL that will be linked in the menu bar. |
||
| 244 | * Make sure to add a matching route via {@link Director::$rules} to this url. |
||
| 245 | * @param string $controllerClass The controller class for this menu, used to check permisssions. |
||
| 246 | * If blank, it's assumed that this is public, and always shown to users who |
||
| 247 | * have the rights to access some other part of the admin area. |
||
| 248 | * @param array $attributes an array of attributes to include on the link. |
||
| 249 | * |
||
| 250 | * @return boolean Success |
||
| 251 | */ |
||
| 252 | public static function replace_menu_item($code, $menuTitle, $url, $controllerClass = null, $priority = -1, |
||
| 266 | |||
| 267 | /** |
||
| 268 | * Add a previously built menu item object to the menu |
||
| 269 | */ |
||
| 270 | protected static function add_menu_item_obj($code, $cmsMenuItem) { |
||
| 277 | |||
| 278 | /** |
||
| 279 | * A utility funciton to retrieve subclasses of a given class that |
||
| 280 | * are instantiable (ie, not abstract) and have a valid menu title. |
||
| 281 | * |
||
| 282 | * Sorted by url_priority config. |
||
| 283 | * |
||
| 284 | * @todo A variation of this function could probably be moved to {@link ClassInfo} |
||
| 285 | * @param string $root The root class to begin finding subclasses |
||
| 286 | * @param boolean $recursive Look for subclasses recursively? |
||
| 287 | * @param string $sort Name of config on which to sort. Can be 'menu_priority' or 'url_priority' |
||
| 288 | * @return array Valid, unique subclasses |
||
| 289 | */ |
||
| 290 | public static function get_cms_classes($root = null, $recursive = true, $sort = self::MENU_PRIORITY) { |
||
| 323 | |||
| 324 | /** |
||
| 325 | * IteratorAggregate Interface Method. Iterates over the menu items. |
||
| 326 | */ |
||
| 327 | public function getIterator() { |
||
| 330 | |||
| 331 | /** |
||
| 332 | * Provide menu titles to the i18n entity provider |
||
| 333 | */ |
||
| 334 | public function provideI18nEntities() { |
||
| 344 | } |
||
| 345 |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: