Complex classes like CKEditorInstaller 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 CKEditorInstaller, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 17 | class CKEditorInstaller |
||
| 18 | { |
||
| 19 | const RELEASE_BASIC = 'basic'; |
||
| 20 | const RELEASE_FULL = 'full'; |
||
| 21 | const RELEASE_STANDARD = 'standard'; |
||
| 22 | |||
| 23 | const VERSION_LATEST = 'latest'; |
||
| 24 | |||
| 25 | const CLEAR_DROP = 'drop'; |
||
| 26 | const CLEAR_KEEP = 'keep'; |
||
| 27 | const CLEAR_ABORT = 'abort'; |
||
| 28 | |||
| 29 | const NOTIFY_CLEAR = 'clear'; |
||
| 30 | const NOTIFY_CLEAR_ARCHIVE = 'clear-archive'; |
||
| 31 | const NOTIFY_CLEAR_COMPLETE = 'clear-complete'; |
||
| 32 | const NOTIFY_CLEAR_PROGRESS = 'clear-progress'; |
||
| 33 | const NOTIFY_CLEAR_QUESTION = 'clear-question'; |
||
| 34 | const NOTIFY_CLEAR_SIZE = 'clear-size'; |
||
| 35 | |||
| 36 | const NOTIFY_DOWNLOAD = 'download'; |
||
| 37 | const NOTIFY_DOWNLOAD_COMPLETE = 'download-complete'; |
||
| 38 | const NOTIFY_DOWNLOAD_PROGRESS = 'download-progress'; |
||
| 39 | const NOTIFY_DOWNLOAD_SIZE = 'download-size'; |
||
| 40 | |||
| 41 | const NOTIFY_EXTRACT = 'extract'; |
||
| 42 | const NOTIFY_EXTRACT_COMPLETE = 'extract-complete'; |
||
| 43 | const NOTIFY_EXTRACT_PROGRESS = 'extract-progress'; |
||
| 44 | const NOTIFY_EXTRACT_SIZE = 'extract-size'; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var string |
||
| 48 | */ |
||
| 49 | private static $archive = 'https://github.com/ckeditor/ckeditor-releases/archive/%s/%s.zip'; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var string |
||
| 53 | */ |
||
| 54 | private $path; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @var string |
||
| 58 | */ |
||
| 59 | private $release; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @var string |
||
| 63 | */ |
||
| 64 | private $version; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @var string |
||
| 68 | */ |
||
| 69 | private $clear; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @var string[] |
||
| 73 | */ |
||
| 74 | private $excludes; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @param string|null $path |
||
| 78 | * @param string|null $release |
||
| 79 | * @param string|null $version |
||
| 80 | * @param string|null $clear |
||
| 81 | * @param string[] $excludes |
||
| 82 | */ |
||
| 83 | 91 | public function __construct( |
|
| 96 | |||
| 97 | /** |
||
| 98 | * @param mixed[] $options |
||
| 99 | * |
||
| 100 | * @return bool |
||
| 101 | */ |
||
| 102 | 91 | public function install(array $options = []) |
|
| 121 | |||
| 122 | /** |
||
| 123 | * @param string $path |
||
| 124 | * @param int|null $clear |
||
| 125 | * @param callable|null $notifier |
||
| 126 | * |
||
| 127 | * @return int |
||
| 128 | */ |
||
| 129 | 91 | private function clear($path, $clear = null, callable $notifier = null) |
|
| 175 | |||
| 176 | /** |
||
| 177 | * @param string $release |
||
| 178 | * @param string $version |
||
| 179 | * @param callable|null $notifier |
||
| 180 | * |
||
| 181 | * @return string |
||
| 182 | */ |
||
| 183 | 91 | private function download($release, $version, callable $notifier = null) |
|
| 204 | |||
| 205 | /** |
||
| 206 | * @param callable|null $notifier |
||
| 207 | * |
||
| 208 | * @return resource |
||
| 209 | */ |
||
| 210 | 91 | private function createStreamContext(callable $notifier = null) |
|
| 237 | |||
| 238 | /** |
||
| 239 | * @param string $origin |
||
| 240 | * @param string $destination |
||
| 241 | * @param string $release |
||
| 242 | * @param string $version |
||
| 243 | * @param string[] $excludes |
||
| 244 | * @param callable|null $notifier |
||
| 245 | */ |
||
| 246 | 91 | private function extract($origin, $destination, $release, $version, array $excludes, callable $notifier = null) |
|
| 277 | |||
| 278 | /** |
||
| 279 | * @param string $file |
||
| 280 | * @param string $rewrite |
||
| 281 | * @param string $origin |
||
| 282 | * @param string $destination |
||
| 283 | * @param string[] $excludes |
||
| 284 | * @param callable|null $notifier |
||
| 285 | */ |
||
| 286 | 91 | private function extractFile($file, $rewrite, $origin, $destination, array $excludes, callable $notifier = null) |
|
| 311 | |||
| 312 | /** |
||
| 313 | * @param callable|null $notifier |
||
| 314 | * @param string $type |
||
| 315 | * @param mixed $data |
||
| 316 | * |
||
| 317 | * @return mixed |
||
| 318 | */ |
||
| 319 | 91 | private function notify(callable $notifier = null, $type, $data = null) |
|
| 325 | |||
| 326 | /** |
||
| 327 | * @param string $message |
||
| 328 | * |
||
| 329 | * @return \RuntimeException |
||
| 330 | */ |
||
| 331 | private function createException($message) |
||
| 341 | } |
||
| 342 |