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 |
||
| 27 | class ServiceAdapter implements ServiceInterface |
||
| 28 | { |
||
| 29 | /** @var ConfigurationInterface */ |
||
| 30 | protected $configuration; |
||
| 31 | /** @var Extract */ |
||
| 32 | protected $domainAdapter; |
||
| 33 | /** @var string */ |
||
| 34 | protected $url; |
||
| 35 | /** @var string */ |
||
| 36 | protected $subDomain; |
||
| 37 | /** @var string */ |
||
| 38 | protected $mainDomain; |
||
| 39 | /** @var string */ |
||
| 40 | protected $applicationDomain; |
||
| 41 | /** @var string */ |
||
| 42 | protected $documentRoot; |
||
| 43 | /** @var string */ |
||
| 44 | protected $applicationRoot; |
||
| 45 | /** @var string */ |
||
| 46 | protected $selectedModule; |
||
| 47 | /** @var string */ |
||
| 48 | protected $selectedApplication; |
||
| 49 | /** @var string */ |
||
| 50 | protected $selectedApplicationUri; |
||
| 51 | /** @var string */ |
||
| 52 | protected $selectedTheme; |
||
| 53 | /** @var string */ |
||
| 54 | protected $selectedThemeResourcePath; |
||
| 55 | /** @var array */ |
||
| 56 | protected $environmentData; |
||
| 57 | /** @var bool */ |
||
| 58 | protected $isHttps; |
||
| 59 | /** @var array */ |
||
| 60 | protected $options = []; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * ServiceAdapter constructor. |
||
| 64 | * |
||
| 65 | * @param ConfigurationInterface $configuration |
||
| 66 | * @param array $getData |
||
| 67 | * @param array $postData |
||
| 68 | * @param array $serverData |
||
| 69 | * @param array $cookieData |
||
| 70 | * @param array $filesData |
||
| 71 | * @param array $optionsData |
||
| 72 | * @throws Exception |
||
| 73 | */ |
||
| 74 | 5 | public function __construct( |
|
| 116 | |||
| 117 | /** |
||
| 118 | * Gets the document root path. |
||
| 119 | * |
||
| 120 | * @return string |
||
| 121 | */ |
||
| 122 | 11 | public function getDocumentRoot() : string |
|
| 126 | |||
| 127 | /** |
||
| 128 | * Gets the application path. |
||
| 129 | * |
||
| 130 | * @return string |
||
| 131 | */ |
||
| 132 | 1 | public function getApplicationRoot(): string |
|
| 136 | |||
| 137 | /** |
||
| 138 | * Gets the application domain. |
||
| 139 | * |
||
| 140 | * @return string |
||
| 141 | */ |
||
| 142 | 9 | public function getApplicationDomain() : string |
|
| 146 | |||
| 147 | /** |
||
| 148 | * Gets the application SSL status. |
||
| 149 | * |
||
| 150 | * @return bool |
||
| 151 | */ |
||
| 152 | 10 | public function isSecuredApplication() : bool |
|
| 156 | |||
| 157 | /** |
||
| 158 | * Gets the selected application. |
||
| 159 | * |
||
| 160 | * @return string |
||
| 161 | */ |
||
| 162 | 17 | public function getSelectedApplication() : string |
|
| 166 | |||
| 167 | /** |
||
| 168 | * Get the URI path for the selected application. Required for the RouterAdapter to work with directory-based |
||
| 169 | * applications correctly. |
||
| 170 | * |
||
| 171 | * @return string |
||
| 172 | */ |
||
| 173 | 17 | public function getSelectedApplicationUri() : string |
|
| 177 | |||
| 178 | /** |
||
| 179 | * Gets the request URI |
||
| 180 | * |
||
| 181 | * @return string |
||
| 182 | */ |
||
| 183 | 1 | public function getRequestUri() : string |
|
| 187 | |||
| 188 | /** |
||
| 189 | * Gets the selected module. |
||
| 190 | * |
||
| 191 | * @return string |
||
| 192 | */ |
||
| 193 | 17 | public function getSelectedModule() : string |
|
| 197 | |||
| 198 | /** |
||
| 199 | * Gets the selected theme. |
||
| 200 | * |
||
| 201 | * @return string |
||
| 202 | */ |
||
| 203 | 11 | public function getSelectedTheme() : string |
|
| 207 | |||
| 208 | /** |
||
| 209 | * Gets the resource path for the selected theme. |
||
| 210 | * |
||
| 211 | * @return string |
||
| 212 | */ |
||
| 213 | 2 | public function getResourcePath() : string |
|
| 217 | |||
| 218 | /** |
||
| 219 | * Gets the request method. |
||
| 220 | * |
||
| 221 | * @return string |
||
| 222 | */ |
||
| 223 | 1 | public function getRequestMethod(): string |
|
| 227 | |||
| 228 | /** |
||
| 229 | * Gets environment data. |
||
| 230 | * |
||
| 231 | * @param string $key |
||
| 232 | * @return array |
||
| 233 | */ |
||
| 234 | 10 | public function getEnvironmentData(string $key) : array |
|
| 242 | |||
| 243 | /** |
||
| 244 | * Gets the client IP address. |
||
| 245 | * |
||
| 246 | * @return string |
||
| 247 | */ |
||
| 248 | 1 | public function getClientIp() : string |
|
| 260 | |||
| 261 | /** |
||
| 262 | * Gets the execution parameters (CLI). |
||
| 263 | * |
||
| 264 | * @return array |
||
| 265 | */ |
||
| 266 | 1 | public function getOptions() : array |
|
| 270 | |||
| 271 | /** |
||
| 272 | * Parses server data and tries to set domain information. |
||
| 273 | * |
||
| 274 | * @throws Exception |
||
| 275 | * @return ServiceAdapter |
||
| 276 | */ |
||
| 277 | 5 | private function setDomain() : ServiceAdapter |
|
| 306 | |||
| 307 | /** |
||
| 308 | * Sets application related data. |
||
| 309 | * |
||
| 310 | * @throws Exception |
||
| 311 | * @return ServiceAdapter |
||
| 312 | */ |
||
| 313 | 5 | private function setApplication() : ServiceAdapter |
|
| 352 | |||
| 353 | /** |
||
| 354 | * Checks from type, path it the current URI segment is valid. |
||
| 355 | * |
||
| 356 | * @param string $applicationName |
||
| 357 | * @param string $subDirectory |
||
| 358 | * @return bool |
||
| 359 | */ |
||
| 360 | 5 | private function checkDirectoryIsValid(string $applicationName, string $subDirectory) : bool |
|
| 371 | |||
| 372 | /** |
||
| 373 | * Checks from type and path if the domain is valid. If so, it sets the $subDirectory to the default. |
||
| 374 | * |
||
| 375 | * @param string $applicationName |
||
| 376 | * @return bool |
||
| 377 | */ |
||
| 378 | 4 | private function checkDomainIsValid(string $applicationName) : bool |
|
| 386 | } |
||
| 387 |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountIdthat can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theidproperty of an instance of theAccountclass. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.