Complex classes like PageBase 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 PageBase, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
21 | abstract class PageBase extends TaskBase implements IRoutedTask |
||
22 | { |
||
23 | use TemplateOutput; |
||
24 | /** @var string Smarty template to display */ |
||
25 | protected $template = "base.tpl"; |
||
26 | /** @var string HTML title. Currently unused. */ |
||
27 | protected $htmlTitle; |
||
28 | /** @var bool Determines if the page is a redirect or not */ |
||
29 | protected $isRedirecting = false; |
||
30 | /** @var array Queue of headers to be sent on successful completion */ |
||
31 | protected $headerQueue = array(); |
||
32 | /** @var string The name of the route to use, as determined by the request router. */ |
||
33 | private $routeName = null; |
||
34 | /** @var TokenManager */ |
||
35 | protected $tokenManager; |
||
36 | /** @var string[] Extra CSS files to include */ |
||
37 | private $extraCss = array(); |
||
38 | /** @var string[] Extra JS files to include */ |
||
39 | private $extraJs = array(); |
||
40 | |||
41 | /** |
||
42 | * Sets the route the request will take. Only should be called from the request router or barrier test. |
||
43 | * |
||
44 | * @param string $routeName The name of the route |
||
45 | * @param bool $skipCallableTest Don't use this unless you know what you're doing, and what the implications are. |
||
46 | * |
||
47 | * @throws Exception |
||
48 | * @category Security-Critical |
||
49 | */ |
||
50 | 5 | final public function setRoute($routeName, $skipCallableTest = false) |
|
60 | |||
61 | /** |
||
62 | * Gets the name of the route that has been passed from the request router. |
||
63 | * @return string |
||
64 | */ |
||
65 | 5 | final public function getRouteName() |
|
69 | |||
70 | /** |
||
71 | * Performs generic page setup actions |
||
72 | */ |
||
73 | final protected function setupPage() |
||
85 | |||
86 | /** |
||
87 | * Runs the page logic as routed by the RequestRouter |
||
88 | * |
||
89 | * Only should be called after a security barrier! That means only from execute(). |
||
90 | */ |
||
91 | final protected function runPage() |
||
165 | |||
166 | /** |
||
167 | * Performs final tasks needed before rendering the page. |
||
168 | */ |
||
169 | protected function finalisePage() |
||
186 | |||
187 | /** |
||
188 | * @return TokenManager |
||
189 | */ |
||
190 | public function getTokenManager() |
||
194 | |||
195 | /** |
||
196 | * @param TokenManager $tokenManager |
||
197 | */ |
||
198 | public function setTokenManager($tokenManager) |
||
202 | |||
203 | /** |
||
204 | * Sends the redirect headers to perform a GET at the destination page. |
||
205 | * |
||
206 | * Also nullifies the set template so Smarty does not render it. |
||
207 | * |
||
208 | * @param string $page The page to redirect requests to (as used in the UR) |
||
209 | * @param null|string $action The action to use on the page. |
||
210 | * @param null|array $parameters |
||
211 | * @param null|string $script The script (relative to index.php) to redirect to |
||
212 | */ |
||
213 | final protected function redirect($page = '', $action = null, $parameters = null, $script = null) |
||
241 | |||
242 | /** |
||
243 | * Sends the redirect headers to perform a GET at the new address. |
||
244 | * |
||
245 | * Also nullifies the set template so Smarty does not render it. |
||
246 | * |
||
247 | * @param string $path URL to redirect to |
||
248 | */ |
||
249 | final protected function redirectUrl($path) |
||
258 | |||
259 | /** |
||
260 | * Sets the name of the template this page should display. |
||
261 | * |
||
262 | * @param string $name |
||
263 | * |
||
264 | * @throws Exception |
||
265 | */ |
||
266 | final protected function setTemplate($name) |
||
274 | |||
275 | /** |
||
276 | * Adds an extra CSS file to to the page |
||
277 | * |
||
278 | * @param string $path The path (relative to the application root) of the file |
||
279 | */ |
||
280 | final protected function addCss($path) { |
||
288 | |||
289 | /** |
||
290 | * Adds an extra JS file to to the page |
||
291 | * |
||
292 | * @param string $path The path (relative to the application root) of the file |
||
293 | */ |
||
294 | final protected function addJs($path){ |
||
302 | |||
303 | /** |
||
304 | * Main function for this page, when no specific actions are called. |
||
305 | * @return void |
||
306 | */ |
||
307 | abstract protected function main(); |
||
308 | |||
309 | /** |
||
310 | * @param string $title |
||
311 | */ |
||
312 | final protected function setHtmlTitle($title) |
||
316 | |||
317 | public function execute() |
||
331 | |||
332 | public function assignCSRFToken() |
||
337 | |||
338 | public function validateCSRFToken() |
||
344 | |||
345 | protected function sendResponseHeaders() |
||
360 | } |
||
361 |
Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.