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() |
||
| 24 | |||
| 25 | /** |
||
| 26 | * When landing on the homepage, if there is a shortlist for the current |
||
| 27 | * user, redirect to the correct URL. Otherwise, 404. |
||
| 28 | * */ |
||
| 29 | public function index($request) |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Get the absolute URL of this controller. |
||
| 41 | * */ |
||
| 42 | public function Link($action = null) |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Create a new list if necessary. If a bot, do nothing! |
||
| 56 | * */ |
||
| 57 | public function initList() |
||
| 74 | |||
| 75 | public function renderList($request) |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Add an item to the shortlist. |
||
| 98 | * |
||
| 99 | * @param ID id of the object to add. |
||
| 100 | * @param type classname of the item to remove |
||
| 101 | * @param session session id. |
||
| 102 | * |
||
| 103 | * */ |
||
| 104 | public function addToShortList($ID = false, $type = null, $session = false) |
||
| 131 | |||
| 132 | public function addOrRemove($request) |
||
| 176 | |||
| 177 | /** |
||
| 178 | * Remove an item from the shortlist. |
||
| 179 | * |
||
| 180 | * @param ID id of the object to remove. |
||
| 181 | * @param type classname of the item to remove |
||
| 182 | * @param session session id. |
||
| 183 | * |
||
| 184 | * */ |
||
| 185 | private function removeFromShortList($ID = false, $type = null, $session = false) |
||
| 203 | |||
| 204 | /** |
||
| 205 | * Get the number of items in the current short list. |
||
| 206 | * |
||
| 207 | * @param session The session to check & find a shortlist for. |
||
| 208 | * @return mixed false if no session exists - else the number of items in the shortlist. |
||
| 209 | * */ |
||
| 210 | public function shortListCount($session = false) |
||
| 224 | |||
| 225 | |||
| 226 | /** |
||
| 227 | * Get a paginated list of the shortlist items. |
||
| 228 | * |
||
| 229 | * @return mixed the paginated list of items, or false if the list cannot be found. |
||
| 230 | * */ |
||
| 231 | public function paginatedItems() |
||
| 254 | |||
| 255 | public function nextPage() |
||
| 263 | |||
| 264 | public function prevPage() |
||
| 272 | |||
| 273 | |||
| 274 | private function getSessionShortList() |
||
| 278 | } |
||
| 279 |
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.