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 | 92 | public function __construct( |
|
| 96 | |||
| 97 | /** |
||
| 98 | * @param mixed[] $options |
||
| 99 | * |
||
| 100 | * @return bool |
||
| 101 | */ |
||
| 102 | 92 | 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 | 38 | private function clear($path, $clear = null, callable $notifier = null) |
|
| 130 | { |
||
| 131 | 38 | if ($clear === null) { |
|
| 132 | 11 | $clear = $this->notify($notifier, self::NOTIFY_CLEAR, $path); |
|
| 133 | 10 | } |
|
| 134 | |||
| 135 | 38 | if ($clear === null) { |
|
| 136 | 9 | $clear = $this->clear; |
|
| 137 | 8 | } |
|
| 138 | |||
| 139 | 38 | if ($clear === self::CLEAR_DROP) { |
|
| 140 | 20 | $files = iterator_to_array(new \RecursiveIteratorIterator( |
|
| 141 | 20 | new \RecursiveDirectoryIterator($path, \RecursiveDirectoryIterator::SKIP_DOTS), |
|
| 142 | 2 | \RecursiveIteratorIterator::CHILD_FIRST |
|
| 143 | 18 | )); |
|
| 144 | |||
| 145 | 20 | $this->notify($notifier, self::NOTIFY_CLEAR_SIZE, count($files)); |
|
| 146 | |||
| 147 | 20 | foreach ($files as $file) { |
|
| 148 | 20 | $filePath = $file->getRealPath(); |
|
| 149 | 20 | $this->notify($notifier, self::NOTIFY_CLEAR_PROGRESS, $filePath); |
|
| 150 | |||
| 151 | 20 | if ($dir = $file->isDir()) { |
|
| 152 | 20 | $success = @rmdir($filePath); |
|
| 153 | 18 | } else { |
|
| 154 | 20 | $success = @unlink($filePath); |
|
| 155 | } |
||
| 156 | |||
| 157 | 20 | if (!$success) { |
|
| 158 | throw $this->createException(sprintf( |
||
| 159 | 'Unable to remove the %s "%s".', |
||
| 160 | 2 | $dir ? 'directory' : 'file', |
|
| 161 | $filePath |
||
| 162 | )); |
||
| 163 | } |
||
| 164 | 18 | } |
|
| 165 | |||
| 166 | 20 | $this->notify($notifier, self::NOTIFY_CLEAR_COMPLETE); |
|
| 167 | 18 | } |
|
| 168 | |||
| 169 | 38 | return $clear; |
|
| 170 | } |
||
| 171 | |||
| 172 | /** |
||
| 173 | * @param string $release |
||
| 174 | * @param string $version |
||
| 175 | * @param callable|null $notifier |
||
| 176 | * |
||
| 177 | * @return string |
||
| 178 | */ |
||
| 179 | 92 | private function download($release, $version, callable $notifier = null) |
|
| 200 | |||
| 201 | /** |
||
| 202 | * @param callable|null $notifier |
||
| 203 | * |
||
| 204 | * @return resource |
||
| 205 | */ |
||
| 206 | 92 | private function createStreamContext(callable $notifier = null) |
|
| 233 | |||
| 234 | /** |
||
| 235 | * @param string $origin |
||
| 236 | * @param string $destination |
||
| 237 | * @param string $release |
||
| 238 | * @param string $version |
||
| 239 | * @param string[] $excludes |
||
| 240 | * @param callable|null $notifier |
||
| 241 | */ |
||
| 242 | 92 | private function extract($origin, $destination, $release, $version, array $excludes, callable $notifier = null) |
|
| 243 | { |
||
| 244 | 92 | $this->notify($notifier, self::NOTIFY_EXTRACT, $destination); |
|
| 245 | |||
| 246 | 92 | $zip = new \ZipArchive(); |
|
| 247 | 92 | $zip->open($origin); |
|
| 248 | |||
| 249 | 92 | $this->notify($notifier, self::NOTIFY_EXTRACT_SIZE, $zip->numFiles); |
|
| 250 | |||
| 251 | 92 | $offset = 20 + strlen($release) + strlen($version); |
|
| 252 | |||
| 253 | 92 | for ($i = 0; $i < $zip->numFiles; ++$i) { |
|
| 254 | 92 | $this->extractFile( |
|
| 255 | 92 | $file = $zip->getNameIndex($i), |
|
| 256 | 82 | substr($file, $offset), |
|
| 257 | 82 | $origin, |
|
| 258 | 82 | $destination, |
|
| 259 | 82 | $excludes, |
|
| 260 | $notifier |
||
| 261 | 82 | ); |
|
| 262 | 82 | } |
|
| 263 | |||
| 264 | 92 | $zip->close(); |
|
| 265 | |||
| 266 | 92 | $this->notify($notifier, self::NOTIFY_EXTRACT_COMPLETE); |
|
| 267 | 92 | $this->notify($notifier, self::NOTIFY_CLEAR_ARCHIVE, $origin); |
|
| 268 | |||
| 269 | 92 | if (!@unlink($origin)) { |
|
| 270 | throw $this->createException(sprintf('Unable to remove the CKEditor ZIP archive "%s".', $origin)); |
||
| 271 | } |
||
| 272 | 92 | } |
|
| 273 | |||
| 274 | /** |
||
| 275 | * @param string $file |
||
| 276 | * @param string $rewrite |
||
| 277 | * @param string $origin |
||
| 278 | * @param string $destination |
||
| 279 | * @param string[] $excludes |
||
| 280 | * @param callable|null $notifier |
||
| 281 | */ |
||
| 282 | 92 | private function extractFile($file, $rewrite, $origin, $destination, array $excludes, callable $notifier = null) |
|
| 307 | |||
| 308 | /** |
||
| 309 | * @param callable|null $notifier |
||
| 310 | * @param string $type |
||
| 311 | * @param mixed $data |
||
| 312 | * |
||
| 313 | * @return mixed |
||
| 314 | */ |
||
| 315 | 92 | private function notify(callable $notifier = null, $type, $data = null) |
|
| 321 | |||
| 322 | /** |
||
| 323 | * @param string $message |
||
| 324 | * |
||
| 325 | * @return \RuntimeException |
||
| 326 | */ |
||
| 327 | private function createException($message) |
||
| 337 | } |
||
| 338 |