Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like CurlRemoteFilesystem 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 CurlRemoteFilesystem, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 18 | class CurlRemoteFilesystem extends Util\RemoteFilesystem |
||
| 19 | { |
||
| 20 | protected $io; |
||
| 21 | protected $config; |
||
| 22 | protected $options; |
||
| 23 | |||
| 24 | protected $retryAuthFailure = true; |
||
| 25 | |||
| 26 | protected $pluginConfig; |
||
| 27 | |||
| 28 | // global flags |
||
| 29 | private $retry = false; |
||
|
|
|||
| 30 | private $degradedMode = false; |
||
| 31 | |||
| 32 | /** @var Aspects\JoinPoint */ |
||
| 33 | public $onPreDownload; |
||
| 34 | |||
| 35 | /** @var Aspects\JoinPoint */ |
||
| 36 | public $onPostDownload; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @param IO\IOInterface $io |
||
| 40 | * @param Config $config |
||
| 41 | * @param array $options |
||
| 42 | */ |
||
| 43 | public function __construct(IO\IOInterface $io, Config $config = null, array $options = array()) |
||
| 44 | { |
||
| 45 | $this->io = $io; |
||
| 46 | $this->config = $config; |
||
| 47 | $this->options = $options; |
||
| 48 | } |
||
| 49 | |||
| 50 | public function setPluginConfig(array $pluginConfig) |
||
| 51 | { |
||
| 52 | $this->pluginConfig = $pluginConfig; |
||
| 53 | } |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Copy the remote file in local. |
||
| 57 | * |
||
| 58 | * @param string $origin host/domain text |
||
| 59 | * @param string $fileUrl targeturl |
||
| 60 | * @param string $fileName the local filename |
||
| 61 | * @param bool $progress Display the progression |
||
| 62 | * @param array $options Additional context options |
||
| 63 | * |
||
| 64 | * @return bool true |
||
| 65 | */ |
||
| 66 | public function copy($origin, $fileUrl, $fileName, $progress=true, $options=array()) |
||
| 67 | { |
||
| 68 | $that = $this; // for PHP5.3 |
||
| 69 | |||
| 70 | return $this->fetch($origin, $fileUrl, $progress, $options, function ($ch, $request) use ($that, $fileName) { |
||
| 71 | $fp = $that->createFile($fileName); |
||
| 72 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, false); |
||
| 73 | curl_setopt($ch, CURLOPT_FILE, $fp); |
||
| 74 | |||
| 75 | list($execStatus, $response) = $result = $that->exec($ch, $request); |
||
| 76 | |||
| 77 | curl_setopt($ch, CURLOPT_FILE, STDOUT); |
||
| 78 | fclose($fp); |
||
| 79 | |||
| 80 | if (200 !== $response->info['http_code']) { |
||
| 81 | unlink($fileName); |
||
| 82 | } |
||
| 83 | |||
| 84 | return $result; |
||
| 85 | }); |
||
| 86 | } |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Get the content. |
||
| 90 | * |
||
| 91 | * @param string $originUrl The origin URL |
||
| 92 | * @param string $fileUrl The file URL |
||
| 93 | * @param bool $progress Display the progression |
||
| 94 | * @param array $options Additional context options |
||
| 95 | * |
||
| 96 | * @return bool|string The content |
||
| 97 | */ |
||
| 98 | public function getContents($origin, $fileUrl, $progress=true, $options=array()) |
||
| 110 | |||
| 111 | protected function fetch($origin, $fileUrl, $progress, $options, $exec) |
||
| 112 | { |
||
| 113 | do { |
||
| 175 | |||
| 176 | /** |
||
| 177 | * Retrieve the options set in the constructor |
||
| 178 | * |
||
| 179 | * @return array Options |
||
| 180 | */ |
||
| 181 | public function getOptions() |
||
| 185 | |||
| 186 | /** |
||
| 187 | * Returns the headers of the last request |
||
| 188 | * |
||
| 189 | * @return array |
||
| 190 | */ |
||
| 191 | public function getLastHeaders() |
||
| 195 | |||
| 196 | /** |
||
| 197 | * @internal |
||
| 198 | * @param resource<curl> $ch |
||
| 199 | * @param Aspects\HttpGetRequest $request |
||
| 200 | * @return array(int, Aspects\HttpGetResponse) |
||
| 201 | */ |
||
| 202 | public function exec($ch, $request) |
||
| 220 | |||
| 221 | /** |
||
| 222 | * @param resource<curl> $ch |
||
| 223 | * @param int $downBytesMax |
||
| 224 | * @param int $downBytes |
||
| 225 | * @param int $upBytesMax |
||
| 226 | * @param int $upBytes |
||
| 227 | */ |
||
| 228 | public function progress() |
||
| 246 | |||
| 247 | public static function createFile($fileName) |
||
| 274 | |||
| 275 | protected function promptAuth(Aspects\HttpGetRequest $req, Aspects\HttpGetResponse $res) |
||
| 360 | } |
||
| 361 |