Complex classes like ServiceAdapter 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 ServiceAdapter, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 23 | class ServiceAdapter implements ServiceInterface |
||
| 24 | { |
||
| 25 | /** @var ConfigurationInterface */ |
||
| 26 | private $configuration; |
||
| 27 | /** @var string */ |
||
| 28 | private $url; |
||
| 29 | /** @var string */ |
||
| 30 | private $subDomain; |
||
| 31 | /** @var string */ |
||
| 32 | private $mainDomain; |
||
| 33 | /** @var string */ |
||
| 34 | private $applicationDomain; |
||
| 35 | /** @var string */ |
||
| 36 | private $documentRoot; |
||
| 37 | /** @var string */ |
||
| 38 | private $selectedModule; |
||
| 39 | /** @var string */ |
||
| 40 | private $selectedApplication; |
||
| 41 | /** @var string */ |
||
| 42 | private $selectedApplicationUri; |
||
| 43 | /** @var string */ |
||
| 44 | private $selectedTheme; |
||
| 45 | /** @var string */ |
||
| 46 | private $selectedThemeResourcePath; |
||
| 47 | /** @var array */ |
||
| 48 | private $environmentData; |
||
| 49 | /** @var bool */ |
||
| 50 | private $isHttps; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * ServiceAdapter constructor. |
||
| 54 | * |
||
| 55 | * @param ConfigurationInterface $configuration |
||
| 56 | * @param array $getData |
||
| 57 | * @param array $postData |
||
| 58 | * @param array $serverData |
||
| 59 | * @param array $cookieData |
||
| 60 | * @param array $filesData |
||
| 61 | */ |
||
| 62 | 5 | public function __construct( |
|
| 99 | |||
| 100 | /** |
||
| 101 | * Gets the document root path. |
||
| 102 | * |
||
| 103 | * @return string |
||
| 104 | */ |
||
| 105 | 1 | public function getDocumentRoot() : string |
|
| 109 | |||
| 110 | /** |
||
| 111 | * Gets the application domain. |
||
| 112 | * |
||
| 113 | * @return string |
||
| 114 | */ |
||
| 115 | 1 | public function getApplicationDomain() : string |
|
| 119 | |||
| 120 | /** |
||
| 121 | * Gets the application SSL status. |
||
| 122 | * |
||
| 123 | * @return bool |
||
| 124 | */ |
||
| 125 | 1 | public function isSecuredApplication() : bool |
|
| 129 | |||
| 130 | /** |
||
| 131 | * Gets the selected application. |
||
| 132 | * |
||
| 133 | * @return string |
||
| 134 | */ |
||
| 135 | 4 | public function getSelectedApplication() : string |
|
| 139 | |||
| 140 | /** |
||
| 141 | * Get the URI path for the selected application. Required for the RouterAdapter to work with directory-based |
||
| 142 | * applications correctly. |
||
| 143 | * |
||
| 144 | * @return string |
||
| 145 | */ |
||
| 146 | 4 | public function getSelectedApplicationUri() : string |
|
| 150 | |||
| 151 | /** |
||
| 152 | * Gets the request URI |
||
| 153 | * |
||
| 154 | * @return string |
||
| 155 | */ |
||
| 156 | 1 | public function getRequestUri() : string |
|
| 160 | |||
| 161 | /** |
||
| 162 | * Gets the selected module. |
||
| 163 | * |
||
| 164 | * @return string |
||
| 165 | */ |
||
| 166 | 1 | public function getSelectedModule() : string |
|
| 170 | |||
| 171 | /** |
||
| 172 | * Gets the selected theme. |
||
| 173 | * |
||
| 174 | * @return string |
||
| 175 | */ |
||
| 176 | 1 | public function getSelectedTheme() : string |
|
| 180 | |||
| 181 | /** |
||
| 182 | * Gets the resource path for the selected theme. |
||
| 183 | * |
||
| 184 | * @return string |
||
| 185 | */ |
||
| 186 | 2 | public function getResourcePath() : string |
|
| 190 | |||
| 191 | /** |
||
| 192 | * Gets the request method. |
||
| 193 | * |
||
| 194 | * @return string |
||
| 195 | */ |
||
| 196 | 1 | public function getRequestMethod(): string |
|
| 200 | |||
| 201 | /** |
||
| 202 | * Gets environment data. |
||
| 203 | * |
||
| 204 | * @param string $key |
||
| 205 | * @return array |
||
| 206 | */ |
||
| 207 | 1 | public function getEnvironmentData(string $key) : array |
|
| 215 | |||
| 216 | /** |
||
| 217 | * Gets the client IP address. |
||
| 218 | * |
||
| 219 | * @return string |
||
| 220 | */ |
||
| 221 | 1 | public function getClientIp() : string |
|
| 233 | /** |
||
| 234 | * Parses server data and tries to set domain information. |
||
| 235 | * |
||
| 236 | * @return ServiceAdapter |
||
| 237 | */ |
||
| 238 | 5 | private function setDomain() : ServiceAdapter |
|
| 280 | |||
| 281 | /** |
||
| 282 | * From the parsed domain data, selects the application, module and theme. |
||
| 283 | * |
||
| 284 | * @return ServiceAdapter |
||
| 285 | */ |
||
| 286 | 5 | private function selectModuleApplicationAndTheme() : ServiceAdapter |
|
| 324 | |||
| 325 | /** |
||
| 326 | * Checks from type, path it the current URI segment is valid. |
||
| 327 | * |
||
| 328 | * @param string $applicationName |
||
| 329 | * @param array $applicationData |
||
| 330 | * @param string $subDirectory |
||
| 331 | * @return bool |
||
| 332 | */ |
||
| 333 | 5 | private function checkDirectoryIsValid(string $applicationName, array $applicationData, string $subDirectory) : bool |
|
| 341 | |||
| 342 | /** |
||
| 343 | * Checks from type and path if the domain is valid. If so, it sets the $subDirectory to the default. |
||
| 344 | * |
||
| 345 | * @param string $applicationName |
||
| 346 | * @param array $applicationData |
||
| 347 | * @param string $subDirectory |
||
| 348 | * @return bool |
||
| 349 | */ |
||
| 350 | 4 | private function checkDomainIsValid(string $applicationName, array $applicationData, string&$subDirectory) : bool |
|
| 367 | } |
||
| 368 |