Complex classes like ShortListController 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 ShortListController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 3 | class ShortListController extends Page_Controller |
||
|
1 ignored issue
–
show
|
|||
| 4 | { |
||
| 5 | private static $allowed_actions = array( |
||
| 6 | 'renderList', |
||
| 7 | 'addOrRemove' |
||
| 8 | ); |
||
| 9 | |||
| 10 | private static $url_handlers = array( |
||
| 11 | 'add' => 'addOrRemove', |
||
| 12 | 'remove' => 'addOrRemove', |
||
| 13 | '$URL!' => 'renderList', |
||
| 14 | ); |
||
| 15 | |||
| 16 | public function init() |
||
| 26 | |||
| 27 | /** |
||
| 28 | * When landing on the homepage, if there is a shortlist for the current |
||
| 29 | * user, redirect to the correct URL. Otherwise, 404. |
||
| 30 | * */ |
||
| 31 | public function index($request) |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Get the absolute URL of this controller. |
||
| 46 | * */ |
||
| 47 | public function Link($action = null) |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Create a new list if necessary. If a bot, do nothing! |
||
| 61 | * */ |
||
| 62 | public function initList() |
||
| 75 | |||
| 76 | public function renderList($request) |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Add an item to the shortlist. |
||
| 99 | * |
||
| 100 | * @param ID id of the object to add. |
||
| 101 | * @param type classname of the item to remove |
||
| 102 | * @param session session id. |
||
| 103 | * |
||
| 104 | * */ |
||
| 105 | public function addToShortList($ID = false, $type = null, $session = false) |
||
| 138 | |||
| 139 | public function addOrRemove($request) |
||
| 187 | |||
| 188 | /** |
||
| 189 | * Remove an item from the shortlist. |
||
| 190 | * |
||
| 191 | * @param ID id of the object to remove. |
||
| 192 | * @param type classname of the item to remove |
||
| 193 | * @param session session id. |
||
| 194 | * |
||
| 195 | * */ |
||
| 196 | private function removeFromShortList($ID = false, $type = null, $session = false) |
||
| 214 | |||
| 215 | /** |
||
| 216 | * Get the number of items in the current short list. |
||
| 217 | * |
||
| 218 | * @param session The session to check & find a shortlist for. |
||
| 219 | * @return mixed false if no session exists - else the number of items in the shortlist. |
||
| 220 | * */ |
||
| 221 | public function shortListCount($session = false) |
||
| 235 | |||
| 236 | |||
| 237 | /** |
||
| 238 | * Get a paginated list of the shortlist items. |
||
| 239 | * |
||
| 240 | * @return mixed the paginated list of items, or false if the list cannot be found. |
||
| 241 | * */ |
||
| 242 | public function paginatedItems() |
||
| 265 | |||
| 266 | public function nextPage() |
||
| 274 | |||
| 275 | public function prevPage() |
||
| 283 | |||
| 284 | |||
| 285 | private function getSessionShortList() |
||
| 289 | } |
||
| 290 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.