Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
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 |
||
| 4 | class ShortListController extends Page_Controller |
||
| 5 | { |
||
| 6 | private static $allowed_actions = array( |
||
|
1 ignored issue
–
show
|
|||
| 7 | 'add', |
||
| 8 | 'remove', |
||
| 9 | 'renderList' |
||
| 10 | ); |
||
| 11 | |||
| 12 | private static $url_handlers = array( |
||
|
1 ignored issue
–
show
|
|||
| 13 | 'add' => 'add', |
||
| 14 | 'remove' => 'remove', |
||
| 15 | '$URL!' => 'renderList', |
||
| 16 | ); |
||
| 17 | |||
| 18 | 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) |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Create a new list if necessary. If a bot, do nothing! |
||
| 45 | * */ |
||
| 46 | public function initList() |
||
| 62 | |||
| 63 | public function renderList($request) |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Add an item to the shortlist. |
||
| 86 | * |
||
| 87 | * $request params: |
||
| 88 | * |
||
| 89 | * * type - classname of the |
||
| 90 | * * id - id of the object to add. |
||
| 91 | * * s - session id. |
||
| 92 | * |
||
| 93 | * */ |
||
| 94 | View Code Duplication | public function add($request) |
|
| 128 | |||
| 129 | public function addToShortList($ID = false, $type = null, $session = false) |
||
| 156 | |||
| 157 | View Code Duplication | public function remove($request) |
|
| 192 | |||
| 193 | private function removeFromShortList($ID = false, $type = null, $session = false) |
||
| 209 | |||
| 210 | /** |
||
| 211 | * Get the number of items in the current short list. |
||
| 212 | * |
||
| 213 | * @param session The session to check & find a shortlist for. |
||
| 214 | * @return mixed false if no session exists - else the number of items in the shortlist. |
||
| 215 | * */ |
||
| 216 | public function shortListCount($session = false) |
||
| 230 | |||
| 231 | |||
| 232 | public function paginatedItems() |
||
| 255 | |||
| 256 | public function nextPage() |
||
| 264 | |||
| 265 | public function prevPage() |
||
| 273 | |||
| 274 | |||
| 275 | private function getSessionShortList() |
||
| 279 | |||
| 280 | /** |
||
| 281 | * Get the absolute URL of this controller. |
||
| 282 | * */ |
||
| 283 | public function Link($action = null) |
||
| 294 | } |
||
| 295 |
This check marks private properties in classes that are never used. Those properties can be removed.