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 | protected $configuration; |
||
| 27 | /** @var string */ |
||
| 28 | protected $url; |
||
| 29 | /** @var string */ |
||
| 30 | protected $subDomain; |
||
| 31 | /** @var string */ |
||
| 32 | protected $mainDomain; |
||
| 33 | /** @var string */ |
||
| 34 | protected $applicationDomain; |
||
| 35 | /** @var string */ |
||
| 36 | protected $documentRoot; |
||
| 37 | /** @var string */ |
||
| 38 | protected $applicationRoot; |
||
| 39 | /** @var string */ |
||
| 40 | protected $selectedModule; |
||
| 41 | /** @var string */ |
||
| 42 | protected $selectedApplication; |
||
| 43 | /** @var string */ |
||
| 44 | protected $selectedApplicationUri; |
||
| 45 | /** @var string */ |
||
| 46 | protected $selectedTheme; |
||
| 47 | /** @var string */ |
||
| 48 | protected $selectedThemeResourcePath; |
||
| 49 | /** @var array */ |
||
| 50 | protected $environmentData; |
||
| 51 | /** @var bool */ |
||
| 52 | protected $isHttps; |
||
| 53 | /** @var array */ |
||
| 54 | protected $options = []; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * ServiceAdapter constructor. |
||
| 58 | * |
||
| 59 | * @param ConfigurationInterface $configuration |
||
| 60 | * @param array $getData |
||
| 61 | * @param array $postData |
||
| 62 | * @param array $serverData |
||
| 63 | * @param array $cookieData |
||
| 64 | * @param array $filesData |
||
| 65 | * @param array $optionsData |
||
| 66 | */ |
||
| 67 | 5 | public function __construct( |
|
| 107 | |||
| 108 | /** |
||
| 109 | * Gets the document root path. |
||
| 110 | * |
||
| 111 | * @return string |
||
| 112 | */ |
||
| 113 | 11 | public function getDocumentRoot() : string |
|
| 117 | |||
| 118 | /** |
||
| 119 | * Gets the application path. |
||
| 120 | * |
||
| 121 | * @return string |
||
| 122 | */ |
||
| 123 | 1 | public function getApplicationRoot(): string |
|
| 127 | |||
| 128 | /** |
||
| 129 | * Gets the application domain. |
||
| 130 | * |
||
| 131 | * @return string |
||
| 132 | */ |
||
| 133 | 6 | public function getApplicationDomain() : string |
|
| 137 | |||
| 138 | /** |
||
| 139 | * Gets the application SSL status. |
||
| 140 | * |
||
| 141 | * @return bool |
||
| 142 | */ |
||
| 143 | 10 | public function isSecuredApplication() : bool |
|
| 147 | |||
| 148 | /** |
||
| 149 | * Gets the selected application. |
||
| 150 | * |
||
| 151 | * @return string |
||
| 152 | */ |
||
| 153 | 4 | public function getSelectedApplication() : string |
|
| 157 | |||
| 158 | /** |
||
| 159 | * Get the URI path for the selected application. Required for the RouterAdapter to work with directory-based |
||
| 160 | * applications correctly. |
||
| 161 | * |
||
| 162 | * @return string |
||
| 163 | */ |
||
| 164 | 17 | public function getSelectedApplicationUri() : string |
|
| 168 | |||
| 169 | /** |
||
| 170 | * Gets the request URI |
||
| 171 | * |
||
| 172 | * @return string |
||
| 173 | */ |
||
| 174 | 1 | public function getRequestUri() : string |
|
| 178 | |||
| 179 | /** |
||
| 180 | * Gets the selected module. |
||
| 181 | * |
||
| 182 | * @return string |
||
| 183 | */ |
||
| 184 | 17 | public function getSelectedModule() : string |
|
| 188 | |||
| 189 | /** |
||
| 190 | * Gets the selected theme. |
||
| 191 | * |
||
| 192 | * @return string |
||
| 193 | */ |
||
| 194 | 11 | public function getSelectedTheme() : string |
|
| 198 | |||
| 199 | /** |
||
| 200 | * Gets the resource path for the selected theme. |
||
| 201 | * |
||
| 202 | * @return string |
||
| 203 | */ |
||
| 204 | 2 | public function getResourcePath() : string |
|
| 208 | |||
| 209 | /** |
||
| 210 | * Gets the request method. |
||
| 211 | * |
||
| 212 | * @return string |
||
| 213 | */ |
||
| 214 | 1 | public function getRequestMethod(): string |
|
| 218 | |||
| 219 | /** |
||
| 220 | * Gets environment data. |
||
| 221 | * |
||
| 222 | * @param string $key |
||
| 223 | * @return array |
||
| 224 | */ |
||
| 225 | 10 | public function getEnvironmentData(string $key) : array |
|
| 233 | |||
| 234 | /** |
||
| 235 | * Gets the client IP address. |
||
| 236 | * |
||
| 237 | * @return string |
||
| 238 | */ |
||
| 239 | 1 | public function getClientIp() : string |
|
| 251 | |||
| 252 | /** |
||
| 253 | * Gets the execution parameters (CLI). |
||
| 254 | * |
||
| 255 | * @return array |
||
| 256 | */ |
||
| 257 | 1 | public function getOptions() : array |
|
| 261 | |||
| 262 | /** |
||
| 263 | * Parses server data and tries to set domain information. |
||
| 264 | * |
||
| 265 | * @return ServiceAdapter |
||
| 266 | */ |
||
| 267 | 5 | private function setDomain() : ServiceAdapter |
|
| 309 | |||
| 310 | /** |
||
| 311 | * From the parsed domain data, selects the application, module and theme. |
||
| 312 | * |
||
| 313 | * @return ServiceAdapter |
||
| 314 | */ |
||
| 315 | 5 | private function selectModuleApplicationAndTheme() : ServiceAdapter |
|
| 353 | |||
| 354 | /** |
||
| 355 | * Checks from type, path it the current URI segment is valid. |
||
| 356 | * |
||
| 357 | * @param string $applicationName |
||
| 358 | * @param array $applicationData |
||
| 359 | * @param string $subDirectory |
||
| 360 | * @return bool |
||
| 361 | */ |
||
| 362 | 5 | private function checkDirectoryIsValid(string $applicationName, array $applicationData, string $subDirectory) : bool |
|
| 370 | |||
| 371 | /** |
||
| 372 | * Checks from type and path if the domain is valid. If so, it sets the $subDirectory to the default. |
||
| 373 | * |
||
| 374 | * @param string $applicationName |
||
| 375 | * @param array $applicationData |
||
| 376 | * @param string $subDirectory |
||
| 377 | * @return bool |
||
| 378 | */ |
||
| 379 | 4 | private function checkDomainIsValid(string $applicationName, array $applicationData, string&$subDirectory) : bool |
|
| 396 | } |
||
| 397 |