Complex classes like RemoteTemplateFinder 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 RemoteTemplateFinder, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 23 | class RemoteTemplateFinder |
||
| 24 | { |
||
| 25 | /** |
||
| 26 | * @var string |
||
| 27 | */ |
||
| 28 | protected $remotePathDelimiter; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @var Client |
||
| 32 | */ |
||
| 33 | protected $client; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @var Repository |
||
| 37 | */ |
||
| 38 | protected $config; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @var Closure[] |
||
| 42 | */ |
||
| 43 | protected $handler; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @var Filesystem |
||
| 47 | */ |
||
| 48 | protected $files; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @var Closure |
||
| 52 | */ |
||
| 53 | protected $templateUrlCallback; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Create a new file view loader instance. |
||
| 57 | * |
||
| 58 | * @param Filesystem $files |
||
| 59 | * @param Repository $config |
||
| 60 | * @param Client $client |
||
| 61 | */ |
||
| 62 | public function __construct(Filesystem $files, Repository $config, Client $client) |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Returns true if template is a remote resource. |
||
| 72 | * |
||
| 73 | * @param string $name Name of template |
||
| 74 | * |
||
| 75 | * @return bool |
||
| 76 | */ |
||
| 77 | public function hasRemoteInformation($name): bool |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Fetch template from remote resource, store locally and return path to local file. |
||
| 84 | * |
||
| 85 | * @param string $name Remote URL to fetch template from |
||
| 86 | * |
||
| 87 | * @return string |
||
| 88 | * @throws IgnoredUrlSuffixException |
||
| 89 | * @throws RemoteTemplateNotFoundException |
||
| 90 | * @throws RemoteHostNotConfiguredException |
||
| 91 | * @throws UrlIsForbiddenException |
||
| 92 | */ |
||
| 93 | public function findRemotePathView($name): string |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Returns true if given url is forbidden. |
||
| 140 | * |
||
| 141 | * @param string $url |
||
| 142 | * @param array $remoteHost |
||
| 143 | * |
||
| 144 | * @return bool |
||
| 145 | */ |
||
| 146 | private function isForbiddenUrl($url, $remoteHost): bool |
||
| 158 | |||
| 159 | /** |
||
| 160 | * Check for valid namespace. |
||
| 161 | * |
||
| 162 | * @param string $name |
||
| 163 | * |
||
| 164 | * @return bool |
||
| 165 | */ |
||
| 166 | protected function hasNamespace($name): bool |
||
| 176 | |||
| 177 | /** |
||
| 178 | * Get the segments of a template with a named path. |
||
| 179 | * |
||
| 180 | * @param string $name |
||
| 181 | * |
||
| 182 | * @return array |
||
| 183 | * |
||
| 184 | * @throws InvalidArgumentException |
||
| 185 | */ |
||
| 186 | protected function parseRemoteNamespaceSegments($name): array |
||
| 195 | |||
| 196 | /** |
||
| 197 | * Return array with remote host config. |
||
| 198 | * |
||
| 199 | * @param string $namespace |
||
| 200 | * |
||
| 201 | * @return array |
||
| 202 | * @throws RemoteHostNotConfiguredException |
||
| 203 | */ |
||
| 204 | protected function getRemoteHost($namespace): array |
||
| 214 | |||
| 215 | /** |
||
| 216 | * Returns true if given url is static. |
||
| 217 | * |
||
| 218 | * @param string $url |
||
| 219 | * @param array $remoteHost |
||
| 220 | * |
||
| 221 | * @return bool |
||
| 222 | */ |
||
| 223 | protected function urlHasIgnoredSuffix($url, $remoteHost): bool |
||
| 235 | |||
| 236 | /** |
||
| 237 | * Returns remote url for given $identifier. |
||
| 238 | * |
||
| 239 | * @param string $identifier |
||
| 240 | * @param array $remoteHost |
||
| 241 | * |
||
| 242 | * @return string |
||
| 243 | */ |
||
| 244 | protected function getTemplateUrlForIdentifier($identifier, $remoteHost): string |
||
| 256 | |||
| 257 | /** |
||
| 258 | * Call callback that will be called after template url has been set. |
||
| 259 | * |
||
| 260 | * @param string $url |
||
| 261 | * |
||
| 262 | * @return string |
||
| 263 | */ |
||
| 264 | protected function callModifyTemplateUrlCallback(string $url): string |
||
| 274 | |||
| 275 | /** |
||
| 276 | * Get folder where fetched views will be stored. |
||
| 277 | * |
||
| 278 | * @param string $namespace |
||
| 279 | * |
||
| 280 | * @return string |
||
| 281 | * @throws RuntimeException |
||
| 282 | */ |
||
| 283 | protected function getViewFolder($namespace): string |
||
| 295 | |||
| 296 | /** |
||
| 297 | * Fetch content from $url. |
||
| 298 | * |
||
| 299 | * @param string $url |
||
| 300 | * @param array $remoteHost |
||
| 301 | * |
||
| 302 | * @return ResponseInterface|Response |
||
| 303 | * @throws RemoteTemplateNotFoundException |
||
| 304 | */ |
||
| 305 | public function fetchContentFromRemoteHost($url, $remoteHost) |
||
| 319 | |||
| 320 | /** |
||
| 321 | * Call handler if any defined. |
||
| 322 | * |
||
| 323 | * @param ResponseInterface $result |
||
| 324 | * @param array $remoteHost |
||
| 325 | * |
||
| 326 | * @return ResponseInterface|\Illuminate\Http\Response|Response |
||
| 327 | */ |
||
| 328 | protected function callResponseHandler($result, array $remoteHost) |
||
| 338 | |||
| 339 | /** |
||
| 340 | * Push a handler to the stack. |
||
| 341 | * |
||
| 342 | * @param int|array $statusCodes |
||
| 343 | * @param callable $callback |
||
| 344 | */ |
||
| 345 | public function pushResponseHandler($statusCodes, $callback) |
||
| 351 | |||
| 352 | /** |
||
| 353 | * Set a callback that will be called after template url has been set. |
||
| 354 | * |
||
| 355 | * @param Closure $callback |
||
| 356 | */ |
||
| 357 | public function setModifyTemplateUrlCallback($callback) |
||
| 361 | } |
||
| 362 |