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 RequestHandler 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 RequestHandler, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
36 | class RequestHandler extends ViewableData { |
||
37 | |||
38 | /** |
||
39 | * @var SS_HTTPRequest $request The request object that the controller was called with. |
||
40 | * Set in {@link handleRequest()}. Useful to generate the {} |
||
41 | */ |
||
42 | protected $request = null; |
||
43 | |||
44 | /** |
||
45 | * The DataModel for this request |
||
46 | */ |
||
47 | protected $model = null; |
||
48 | |||
49 | /** |
||
50 | * This variable records whether RequestHandler::__construct() |
||
51 | * was called or not. Useful for checking if subclasses have |
||
52 | * called parent::__construct() |
||
53 | * |
||
54 | * @var boolean |
||
55 | */ |
||
56 | protected $brokenOnConstruct = true; |
||
57 | |||
58 | /** |
||
59 | * The default URL handling rules. This specifies that the next component of the URL corresponds to a method to |
||
60 | * be called on this RequestHandlingData object. |
||
61 | * |
||
62 | * The keys of this array are parse rules. See {@link SS_HTTPRequest::match()} for a description of the rules |
||
63 | * available. |
||
64 | * |
||
65 | * The values of the array are the method to be called if the rule matches. If this value starts with a '$', then |
||
66 | * the named parameter of the parsed URL wil be used to determine the method name. |
||
67 | * @config |
||
68 | */ |
||
69 | private static $url_handlers = array( |
||
70 | '$Action' => '$Action', |
||
71 | ); |
||
72 | |||
73 | |||
74 | /** |
||
75 | * Define a list of action handling methods that are allowed to be called directly by URLs. |
||
76 | * The variable should be an array of action names. This sample shows the different values that it can contain: |
||
77 | * |
||
78 | * <code> |
||
79 | * array( |
||
80 | * // someaction can be accessed by anyone, any time |
||
81 | * 'someaction', |
||
82 | * // So can otheraction |
||
83 | * 'otheraction' => true, |
||
84 | * // restrictedaction can only be people with ADMIN privilege |
||
85 | * 'restrictedaction' => 'ADMIN', |
||
86 | * // complexaction can only be accessed if $this->canComplexAction() returns true |
||
87 | * 'complexaction' '->canComplexAction' |
||
88 | * ); |
||
89 | * </code> |
||
90 | * |
||
91 | * Form getters count as URL actions as well, and should be included in allowed_actions. |
||
92 | * Form actions on the other handed (first argument to {@link FormAction()} should NOT be included, |
||
93 | * these are handled separately through {@link Form->httpSubmission}. You can control access on form actions |
||
94 | * either by conditionally removing {@link FormAction} in the form construction, |
||
95 | * or by defining $allowed_actions in your {@link Form} class. |
||
96 | * @config |
||
97 | */ |
||
98 | private static $allowed_actions = null; |
||
99 | |||
100 | /** |
||
101 | * @config |
||
102 | * @var boolean Enforce presence of $allowed_actions when checking acccess. |
||
103 | * Defaults to TRUE, meaning all URL actions will be denied. |
||
104 | * When set to FALSE, the controller will allow *all* public methods to be called. |
||
105 | * In most cases this isn't desireable, and in fact a security risk, |
||
106 | * since some helper methods can cause side effects which shouldn't be exposed through URLs. |
||
107 | */ |
||
108 | private static $require_allowed_actions = true; |
||
109 | |||
110 | public function __construct() { |
||
121 | |||
122 | /** |
||
123 | * Set the DataModel for this request. |
||
124 | */ |
||
125 | public function setDataModel($model) { |
||
128 | |||
129 | /** |
||
130 | * Handles URL requests. |
||
131 | * |
||
132 | * - ViewableData::handleRequest() iterates through each rule in {@link self::$url_handlers}. |
||
133 | * - If the rule matches, the named method will be called. |
||
134 | * - If there is still more URL to be processed, then handleRequest() |
||
135 | * is called on the object that that method returns. |
||
136 | * |
||
137 | * Once all of the URL has been processed, the final result is returned. |
||
138 | * However, if the final result is an array, this |
||
139 | * array is interpreted as being additional template data to customise the |
||
140 | * 2nd to last result with, rather than an object |
||
141 | * in its own right. This is most frequently used when a Controller's |
||
142 | * action will return an array of data with which to |
||
143 | * customise the controller. |
||
144 | * |
||
145 | * @param $request The {@link SS_HTTPRequest} object that is reponsible for distributing URL parsing |
||
146 | * @uses SS_HTTPRequest |
||
147 | * @uses SS_HTTPRequest->match() |
||
148 | * @return SS_HTTPResponse|RequestHandler|string|array |
||
149 | */ |
||
150 | public function handleRequest(SS_HTTPRequest $request, DataModel $model) { |
||
240 | |||
241 | protected function findAction($request) { |
||
268 | |||
269 | /** |
||
270 | * Given a request, and an action name, call that action name on this RequestHandler |
||
271 | * |
||
272 | * Must not raise SS_HTTPResponse_Exceptions - instead it should return |
||
273 | * |
||
274 | * @param $request |
||
275 | * @param $action |
||
276 | * @return SS_HTTPResponse |
||
277 | */ |
||
278 | protected function handleAction($request, $action) { |
||
295 | |||
296 | /** |
||
297 | * Get a array of allowed actions defined on this controller, |
||
298 | * any parent classes or extensions. |
||
299 | * |
||
300 | * Caution: Since 3.1, allowed_actions definitions only apply |
||
301 | * to methods on the controller they're defined on, |
||
302 | * so it is recommended to use the $class argument |
||
303 | * when invoking this method. |
||
304 | * |
||
305 | * @param String $limitToClass |
||
306 | * @return array|null |
||
307 | */ |
||
308 | public function allowedActions($limitToClass = null) { |
||
341 | |||
342 | /** |
||
343 | * Checks if this request handler has a specific action, |
||
344 | * even if the current user cannot access it. |
||
345 | * Includes class ancestry and extensions in the checks. |
||
346 | * |
||
347 | * @param string $action |
||
348 | * @return bool |
||
349 | */ |
||
350 | public function hasAction($action) { |
||
384 | |||
385 | /** |
||
386 | * Return the class that defines the given action, so that we know where to check allowed_actions. |
||
387 | */ |
||
388 | protected function definingClassForAction($actionOrigCasing) { |
||
400 | |||
401 | /** |
||
402 | * Check that the given action is allowed to be called from a URL. |
||
403 | * It will interrogate {@link self::$allowed_actions} to determine this. |
||
404 | */ |
||
405 | public function checkAccessAction($action) { |
||
455 | |||
456 | /** |
||
457 | * Throws a HTTP error response encased in a {@link SS_HTTPResponse_Exception}, which is later caught in |
||
458 | * {@link RequestHandler::handleAction()} and returned to the user. |
||
459 | * |
||
460 | * @param int $errorCode |
||
461 | * @param string $errorMessage Plaintext error message |
||
462 | * @uses SS_HTTPResponse_Exception |
||
463 | */ |
||
464 | public function httpError($errorCode, $errorMessage = null) { |
||
477 | |||
478 | /** |
||
479 | * Returns the SS_HTTPRequest object that this controller is using. |
||
480 | * Returns a placeholder {@link NullHTTPRequest} object unless |
||
481 | * {@link handleAction()} or {@link handleRequest()} have been called, |
||
482 | * which adds a reference to an actual {@link SS_HTTPRequest} object. |
||
483 | * |
||
484 | * @return SS_HTTPRequest|NullHTTPRequest |
||
485 | */ |
||
486 | public function getRequest() { |
||
489 | |||
490 | /** |
||
491 | * Typically the request is set through {@link handleAction()} |
||
492 | * or {@link handleRequest()}, but in some based we want to set it manually. |
||
493 | * |
||
494 | * @param SS_HTTPRequest |
||
495 | */ |
||
496 | public function setRequest($request) { |
||
499 | } |
||
500 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.