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 |
||
| 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 | private static $extensions = array( |
||
| 17 | 'ShortListPaginationExtension' |
||
| 18 | ); |
||
| 19 | |||
| 20 | public function init() |
||
| 30 | |||
| 31 | /** |
||
| 32 | * When landing on the homepage, if there is a shortlist for the current |
||
| 33 | * user, redirect to the correct URL. Otherwise, 404. |
||
| 34 | * */ |
||
| 35 | public function index($request) |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Get the absolute URL of this controller. |
||
| 60 | * */ |
||
| 61 | public function Link($action = null) |
||
| 72 | |||
| 73 | public function renderList($request) |
||
| 100 | |||
| 101 | /** |
||
| 102 | * Add an item to the shortlist. |
||
| 103 | * |
||
| 104 | * @param ID id of the object to add. |
||
| 105 | * @param type classname of the item to remove |
||
| 106 | * @param session session id. |
||
| 107 | * |
||
| 108 | * */ |
||
| 109 | public function addToShortList($ID = false, $type = null, $session = false) |
||
| 143 | |||
| 144 | public function addOrRemove($request) |
||
| 191 | |||
| 192 | /** |
||
| 193 | * Remove an item from the shortlist. |
||
| 194 | * |
||
| 195 | * @param ID id of the object to remove. |
||
| 196 | * @param type classname of the item to remove |
||
| 197 | * @param session session id. |
||
| 198 | * |
||
| 199 | * */ |
||
| 200 | private function removeFromShortList($ID = false, $type = null, $session = false) |
||
| 218 | |||
| 219 | /** |
||
| 220 | * Get the number of items in the current short list. |
||
| 221 | * |
||
| 222 | * @param session The session to check & find a shortlist for. |
||
| 223 | * @return mixed false if no session exists - else the number of items in the shortlist. |
||
| 224 | * */ |
||
| 225 | public function shortListCount($session = false) |
||
| 239 | |||
| 240 | private function getSessionShortList() |
||
| 244 | |||
| 245 | public static function getShortListSession() |
||
| 249 | } |
||
| 250 |
Since your code implements the magic setter
_set, this function will be called for any write access on an undefined variable. You can add the@propertyannotation to your class or interface to document the existence of this variable.Since the property has write access only, you can use the @property-write annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.