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 Controller 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 Controller, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
31 | class Controller extends RequestHandler implements TemplateGlobalProvider { |
||
32 | |||
33 | /** |
||
34 | * An array of arguments extracted from the URL. |
||
35 | * |
||
36 | * @var array |
||
37 | */ |
||
38 | protected $urlParams; |
||
39 | |||
40 | /** |
||
41 | * Contains all GET and POST parameters passed to the current {@link SS_HTTPRequest}. |
||
42 | * |
||
43 | * @var array |
||
44 | */ |
||
45 | protected $requestParams; |
||
46 | |||
47 | /** |
||
48 | * The URL part matched on the current controller as determined by the "$Action" part of the |
||
49 | * {@link $url_handlers} definition. Should correlate to a public method on this controller. |
||
50 | * |
||
51 | * Used in {@link render()} and {@link getViewer()} to determine action-specific templates. |
||
52 | * |
||
53 | * @var string |
||
54 | */ |
||
55 | protected $action; |
||
56 | |||
57 | /** |
||
58 | * The {@link Session} object for this controller. |
||
59 | * |
||
60 | * @var Session |
||
61 | */ |
||
62 | protected $session; |
||
63 | |||
64 | /** |
||
65 | * Stack of current controllers. Controller::$controller_stack[0] is the current controller. |
||
66 | * |
||
67 | * @var array |
||
68 | */ |
||
69 | protected static $controller_stack = array(); |
||
70 | |||
71 | /** |
||
72 | * @var bool |
||
73 | */ |
||
74 | protected $basicAuthEnabled = true; |
||
75 | |||
76 | /** |
||
77 | * The response object that the controller returns. |
||
78 | * |
||
79 | * Set in {@link handleRequest()}. |
||
80 | * |
||
81 | * @var SS_HTTPResponse |
||
82 | */ |
||
83 | protected $response; |
||
84 | |||
85 | /** |
||
86 | * Default URL handlers. |
||
87 | * |
||
88 | * @var array |
||
89 | */ |
||
90 | private static $url_handlers = array( |
||
|
|||
91 | '$Action//$ID/$OtherID' => 'handleAction', |
||
92 | ); |
||
93 | |||
94 | /** |
||
95 | * @var array |
||
96 | */ |
||
97 | private static $allowed_actions = array( |
||
98 | 'handleAction', |
||
99 | 'handleIndex', |
||
100 | ); |
||
101 | |||
102 | /** |
||
103 | * Initialisation function that is run before any action on the controller is called. |
||
104 | * |
||
105 | * @uses BasicAuth::requireLogin() |
||
106 | */ |
||
107 | public function init() { |
||
113 | |||
114 | /** |
||
115 | * Returns a link to this controller. Overload with your own Link rules if they exist. |
||
116 | * |
||
117 | * @return string |
||
118 | */ |
||
119 | public function Link() { |
||
122 | |||
123 | /** |
||
124 | * Executes this controller, and return an {@link SS_HTTPResponse} object with the result. |
||
125 | * |
||
126 | * This method first does a few set-up activities: |
||
127 | * - Push this controller ont to the controller stack - see {@link Controller::curr()} for |
||
128 | * information about this. |
||
129 | * - Call {@link init()} |
||
130 | * - Defer to {@link RequestHandler->handleRequest()} to determine which action should be executed. |
||
131 | * |
||
132 | * Note: $requestParams['executeForm'] support was removed, make the following change in your URLs: |
||
133 | * "/?executeForm=FooBar" -> "/FooBar". |
||
134 | * |
||
135 | * Also make sure "FooBar" is in the $allowed_actions of your controller class. |
||
136 | * |
||
137 | * Note: You should rarely need to overload run() - this kind of change is only really appropriate |
||
138 | * for things like nested controllers - {@link ModelAsController} and {@link RootURLController} |
||
139 | * are two examples here. If you want to make more orthodox functionality, it's better to overload |
||
140 | * {@link init()} or {@link index()}. |
||
141 | * |
||
142 | * Important: If you are going to overload handleRequest, make sure that you start the method with |
||
143 | * $this->pushCurrent() and end the method with $this->popCurrent(). Failure to do this will create |
||
144 | * weird session errors. |
||
145 | * |
||
146 | * @param SS_HTTPRequest $request |
||
147 | * @param DataModel $model |
||
148 | * |
||
149 | * @return HTTPResponse |
||
150 | */ |
||
151 | public function handleRequest(HTTPRequest $request, DataModel $model) { |
||
207 | |||
208 | /** |
||
209 | * Controller's default action handler. It will call the method named in "$Action", if that method |
||
210 | * exists. If "$Action" isn't given, it will use "index" as a default. |
||
211 | * |
||
212 | * @param SS_HTTPRequest $request |
||
213 | * @param string $action |
||
214 | * |
||
215 | * @return HTMLText|SS_HTTPResponse |
||
216 | */ |
||
217 | protected function handleAction($request, $action) { |
||
251 | |||
252 | /** |
||
253 | * @param array $urlParams |
||
254 | */ |
||
255 | public function setURLParams($urlParams) { |
||
258 | |||
259 | /** |
||
260 | * Returns the parameters extracted from the URL by the {@link Director}. |
||
261 | * |
||
262 | * @return array |
||
263 | */ |
||
264 | public function getURLParams() { |
||
267 | |||
268 | /** |
||
269 | * Returns the SS_HTTPResponse object that this controller is building up. Can be used to set the |
||
270 | * status code and headers. |
||
271 | * |
||
272 | * @return HTTPResponse |
||
273 | */ |
||
274 | public function getResponse() { |
||
280 | |||
281 | /** |
||
282 | * Sets the SS_HTTPResponse object that this controller is building up. |
||
283 | * |
||
284 | * @param SS_HTTPResponse $response |
||
285 | * |
||
286 | * @return $this |
||
287 | */ |
||
288 | public function setResponse(HTTPResponse $response) { |
||
292 | |||
293 | /** |
||
294 | * @var bool |
||
295 | */ |
||
296 | protected $baseInitCalled = false; |
||
297 | |||
298 | /** |
||
299 | * Return the object that is going to own a form that's being processed, and handle its execution. |
||
300 | * Note that the result need not be an actual controller object. |
||
301 | * |
||
302 | * @return mixed |
||
303 | */ |
||
304 | public function getFormOwner() { |
||
318 | |||
319 | /** |
||
320 | * This is the default action handler used if a method doesn't exist. It will process the |
||
321 | * controller object with the template returned by {@link getViewer()}. |
||
322 | * |
||
323 | * @param string $action |
||
324 | * |
||
325 | * @return HTMLText |
||
326 | */ |
||
327 | public function defaultAction($action) { |
||
330 | |||
331 | /** |
||
332 | * Returns the action that is being executed on this controller. |
||
333 | * |
||
334 | * @return string |
||
335 | */ |
||
336 | public function getAction() { |
||
339 | |||
340 | /** |
||
341 | * Return the viewer identified being the default handler for this Controller/Action combination. |
||
342 | * |
||
343 | * @param string $action |
||
344 | * |
||
345 | * @return SSViewer |
||
346 | */ |
||
347 | public function getViewer($action) { |
||
383 | |||
384 | /** |
||
385 | * @param string $action |
||
386 | * |
||
387 | * @return bool |
||
388 | */ |
||
389 | public function hasAction($action) { |
||
392 | |||
393 | /** |
||
394 | * Removes all the "action" part of the current URL and returns the result. If no action parameter |
||
395 | * is present, returns the full URL. |
||
396 | * |
||
397 | * @param string $fullURL |
||
398 | * @param null|string $action |
||
399 | * |
||
400 | * @return string |
||
401 | */ |
||
402 | public function removeAction($fullURL, $action = null) { |
||
412 | |||
413 | /** |
||
414 | * Return the class that defines the given action, so that we know where to check allowed_actions. |
||
415 | * Overrides RequestHandler to also look at defined templates. |
||
416 | * |
||
417 | * @param string $action |
||
418 | * |
||
419 | * @return string |
||
420 | */ |
||
421 | protected function definingClassForAction($action) { |
||
433 | |||
434 | /** |
||
435 | * Returns TRUE if this controller has a template that is specifically designed to handle a |
||
436 | * specific action. |
||
437 | * |
||
438 | * @param string $action |
||
439 | * |
||
440 | * @return bool |
||
441 | */ |
||
442 | public function hasActionTemplate($action) { |
||
455 | |||
456 | /** |
||
457 | * Render the current controller with the templates determined by {@link getViewer()}. |
||
458 | * |
||
459 | * @param array $params |
||
460 | * |
||
461 | * @return string |
||
462 | */ |
||
463 | public function render($params = null) { |
||
473 | |||
474 | /** |
||
475 | * Call this to disable site-wide basic authentication for a specific controller. This must be |
||
476 | * called before Controller::init(). That is, you must call it in your controller's init method |
||
477 | * before it calls parent::init(). |
||
478 | */ |
||
479 | public function disableBasicAuth() { |
||
482 | |||
483 | /** |
||
484 | * Returns the current controller. |
||
485 | * |
||
486 | * @return Controller |
||
487 | */ |
||
488 | public static function curr() { |
||
495 | |||
496 | /** |
||
497 | * Tests whether we have a currently active controller or not. True if there is at least 1 |
||
498 | * controller in the stack. |
||
499 | * |
||
500 | * @return bool |
||
501 | */ |
||
502 | public static function has_curr() { |
||
505 | |||
506 | /** |
||
507 | * Returns true if the member is allowed to do the given action. Defaults to the currently logged |
||
508 | * in user. |
||
509 | * |
||
510 | * @param string $perm |
||
511 | * @param null|member $member |
||
512 | * |
||
513 | * @return bool |
||
514 | */ |
||
515 | public function can($perm, $member = null) { |
||
527 | |||
528 | /** |
||
529 | * Pushes this controller onto the stack of current controllers. This means that any redirection, |
||
530 | * session setting, or other things that rely on Controller::curr() will now write to this |
||
531 | * controller object. |
||
532 | */ |
||
533 | public function pushCurrent() { |
||
544 | |||
545 | /** |
||
546 | * Pop this controller off the top of the stack. |
||
547 | */ |
||
548 | public function popCurrent() { |
||
556 | |||
557 | /** |
||
558 | * Redirect to the given URL. |
||
559 | * |
||
560 | * @param string $url |
||
561 | * @param int $code |
||
562 | * |
||
563 | * @return HTTPResponse |
||
564 | */ |
||
565 | public function redirect($url, $code=302) { |
||
580 | |||
581 | /** |
||
582 | * Redirect back. Uses either the HTTP-Referer or a manually set request-variable called "BackURL". |
||
583 | * This variable is needed in scenarios where HTTP-Referer is not sent (e.g when calling a page by |
||
584 | * location.href in IE). If none of the two variables is available, it will redirect to the base |
||
585 | * URL (see {@link Director::baseURL()}). |
||
586 | * |
||
587 | * @uses redirect() |
||
588 | * |
||
589 | * @return bool|SS_HTTPResponse |
||
590 | */ |
||
591 | public function redirectBack() { |
||
621 | |||
622 | /** |
||
623 | * Tests whether a redirection has been requested. If redirect() has been called, it will return |
||
624 | * the URL redirected to. Otherwise, it will return null. |
||
625 | * |
||
626 | * @return null|string |
||
627 | */ |
||
628 | public function redirectedTo() { |
||
631 | |||
632 | /** |
||
633 | * Get the Session object representing this Controller's session. |
||
634 | * |
||
635 | * @return Session |
||
636 | */ |
||
637 | public function getSession() { |
||
640 | |||
641 | /** |
||
642 | * Set the Session object. |
||
643 | * |
||
644 | * @param Session $session |
||
645 | */ |
||
646 | public function setSession(Session $session) { |
||
649 | |||
650 | /** |
||
651 | * Joins two or more link segments together, putting a slash between them if necessary. Use this |
||
652 | * for building the results of {@link Link()} methods. If either of the links have query strings, |
||
653 | * then they will be combined and put at the end of the resulting url. |
||
654 | * |
||
655 | * Caution: All parameters are expected to be URI-encoded already. |
||
656 | * |
||
657 | * @param string |
||
658 | * |
||
659 | * @return string |
||
660 | */ |
||
661 | public static function join_links() { |
||
691 | |||
692 | /** |
||
693 | * @return array |
||
694 | */ |
||
695 | public static function get_template_global_variables() { |
||
700 | } |
||
701 | |||
703 |