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 |
||
| 22 | abstract class PageBase extends TaskBase implements IRoutedTask |
||
| 23 | { |
||
| 24 | use TemplateOutput; |
||
| 25 | /** @var string Smarty template to display */ |
||
| 26 | protected $template = "base.tpl"; |
||
| 27 | /** @var string HTML title. Currently unused. */ |
||
| 28 | protected $htmlTitle; |
||
| 29 | /** @var bool Determines if the page is a redirect or not */ |
||
| 30 | protected $isRedirecting = false; |
||
| 31 | /** @var array Queue of headers to be sent on successful completion */ |
||
| 32 | protected $headerQueue = array(); |
||
| 33 | /** @var string The name of the route to use, as determined by the request router. */ |
||
| 34 | private $routeName = null; |
||
| 35 | /** @var TokenManager */ |
||
| 36 | protected $tokenManager; |
||
| 37 | /** @var string[] Extra CSS files to include */ |
||
| 38 | private $extraCss = array(); |
||
| 39 | /** @var string[] Extra JS files to include */ |
||
| 40 | private $extraJs = array(); |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Sets the route the request will take. Only should be called from the request router or barrier test. |
||
| 44 | * |
||
| 45 | * @param string $routeName The name of the route |
||
| 46 | * @param bool $skipCallableTest Don't use this unless you know what you're doing, and what the implications are. |
||
| 47 | * |
||
| 48 | * @throws Exception |
||
| 49 | * @category Security-Critical |
||
| 50 | */ |
||
| 51 | final public function setRoute($routeName, $skipCallableTest = false) |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Gets the name of the route that has been passed from the request router. |
||
| 64 | * @return string |
||
| 65 | */ |
||
| 66 | final public function getRouteName() |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Performs generic page setup actions |
||
| 73 | */ |
||
| 74 | final protected function setupPage() |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Runs the page logic as routed by the RequestRouter |
||
| 89 | * |
||
| 90 | * Only should be called after a security barrier! That means only from execute(). |
||
| 91 | */ |
||
| 92 | final protected function runPage() |
||
| 178 | |||
| 179 | /** |
||
| 180 | * Performs final tasks needed before rendering the page. |
||
| 181 | */ |
||
| 182 | protected function finalisePage() |
||
| 199 | |||
| 200 | /** |
||
| 201 | * @return TokenManager |
||
| 202 | */ |
||
| 203 | public function getTokenManager() |
||
| 207 | |||
| 208 | /** |
||
| 209 | * @param TokenManager $tokenManager |
||
| 210 | */ |
||
| 211 | public function setTokenManager($tokenManager) |
||
| 215 | |||
| 216 | /** |
||
| 217 | * Sends the redirect headers to perform a GET at the destination page. |
||
| 218 | * |
||
| 219 | * Also nullifies the set template so Smarty does not render it. |
||
| 220 | * |
||
| 221 | * @param string $page The page to redirect requests to (as used in the UR) |
||
| 222 | * @param null|string $action The action to use on the page. |
||
| 223 | * @param null|array $parameters |
||
| 224 | * @param null|string $script The script (relative to index.php) to redirect to |
||
| 225 | */ |
||
| 226 | final protected function redirect($page = '', $action = null, $parameters = null, $script = null) |
||
| 254 | |||
| 255 | /** |
||
| 256 | * Sends the redirect headers to perform a GET at the new address. |
||
| 257 | * |
||
| 258 | * Also nullifies the set template so Smarty does not render it. |
||
| 259 | * |
||
| 260 | * @param string $path URL to redirect to |
||
| 261 | */ |
||
| 262 | final protected function redirectUrl($path) |
||
| 271 | |||
| 272 | /** |
||
| 273 | * Sets the name of the template this page should display. |
||
| 274 | * |
||
| 275 | * @param string $name |
||
| 276 | * |
||
| 277 | * @throws Exception |
||
| 278 | */ |
||
| 279 | final protected function setTemplate($name) |
||
| 287 | |||
| 288 | /** |
||
| 289 | * Adds an extra CSS file to to the page |
||
| 290 | * |
||
| 291 | * @param string $path The path (relative to the application root) of the file |
||
| 292 | */ |
||
| 293 | final protected function addCss($path) { |
||
| 301 | |||
| 302 | /** |
||
| 303 | * Adds an extra JS file to to the page |
||
| 304 | * |
||
| 305 | * @param string $path The path (relative to the application root) of the file |
||
| 306 | */ |
||
| 307 | final protected function addJs($path){ |
||
| 315 | |||
| 316 | /** |
||
| 317 | * Main function for this page, when no specific actions are called. |
||
| 318 | * @return void |
||
| 319 | */ |
||
| 320 | abstract protected function main(); |
||
| 321 | |||
| 322 | /** |
||
| 323 | * @param string $title |
||
| 324 | */ |
||
| 325 | final protected function setHtmlTitle($title) |
||
| 329 | |||
| 330 | public function execute() |
||
| 344 | |||
| 345 | public function assignCSRFToken() |
||
| 350 | |||
| 351 | public function validateCSRFToken() |
||
| 357 | |||
| 358 | protected function sendResponseHeaders() |
||
| 373 | } |
||
| 374 |