Complex classes like PageController 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 PageController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 13 | class PageController extends AbstractActionController |
||
| 14 | { |
||
| 15 | public function indexAction() |
||
| 52 | |||
| 53 | private function isAdminSessionSet() |
||
| 58 | |||
| 59 | private function mustDestroyAdminSession($queryParams) |
||
| 63 | |||
| 64 | private function destroyAdminSession() |
||
| 71 | |||
| 72 | private function setAdminSession(array $queryParams) |
||
| 77 | |||
| 78 | protected function fetchPage($pageId, Array $params = null, $withBreakpoints = false) |
||
| 79 | { |
||
| 80 | $accessToken = $this->getAdminSession(); |
||
| 81 | if ($accessToken === null) { |
||
| 82 | $accessToken = $this->getRequest()->getHeaders()->get('Cookie')->columnis_token; |
||
| 83 | } |
||
| 84 | $page = new Page(); |
||
| 85 | $page->setId($pageId); |
||
| 86 | $pageService = $this->getPageService($withBreakpoints); |
||
| 87 | try { |
||
| 88 | $apiResponse = $pageService->fetch($page, $params, $accessToken); |
||
| 89 | if(!$apiResponse) { |
||
| 90 | return null; |
||
| 91 | } |
||
| 92 | } catch(PageWithoutTemplateException $e) { |
||
| 93 | return null; |
||
| 94 | } |
||
| 95 | $headersApi = $apiResponse->getHeaders(); |
||
| 96 | $headers = $this->getResponse()->getHeaders(); |
||
| 97 | |||
| 98 | // Pass Api response headers to express response headers |
||
| 99 | if (isset($headersApi["X-Cache"]) && !empty($headersApi["X-Cache"])) { |
||
| 100 | $headers->addHeaders([ |
||
| 101 | "X-Cache" => $headersApi["X-Cache"] |
||
| 102 | ]); |
||
| 103 | } |
||
| 104 | if (isset($headersApi["Age"]) && !empty($headersApi["Age"])) { |
||
| 105 | $headers->addHeaders([ |
||
| 106 | "Age" => $headersApi["Age"] |
||
| 107 | ]); |
||
| 108 | } |
||
| 109 | return $page; |
||
| 110 | } |
||
| 111 | |||
| 112 | private function getAdminSession() |
||
| 117 | |||
| 118 | public function getPageService($withBreakpoints = false) |
||
| 126 | |||
| 127 | private function setPageAssets(Template $template, &$pageData) |
||
| 162 | |||
| 163 | private function getExcludes() |
||
| 172 | |||
| 173 | private function getAssetsPath() |
||
| 185 | |||
| 186 | private function searchAssets($path, $extension, Array $excludes = null) |
||
| 195 | |||
| 196 | private function setPublicRelativePath(Array $assets = null) |
||
| 207 | |||
| 208 | private function getPublicPath() |
||
| 217 | } |
||
| 218 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: