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 | ||
| 17 | class CurlRemoteFilesystem extends Util\RemoteFilesystem | ||
| 18 | { | ||
| 19 | protected $io; | ||
| 20 | protected $config; | ||
| 21 | protected $options; | ||
| 22 | |||
| 23 | protected $retryAuthFailure = true; | ||
| 24 | |||
| 25 | protected $pluginConfig; | ||
| 26 | |||
| 27 | private $_lastHeaders = array(); | ||
| 28 | |||
| 29 | // global flags | ||
| 30 | private $_retry = false; | ||
| 31 | private $_degradedMode = false; | ||
| 32 | |||
| 33 | /** @var Aspects\JoinPoint */ | ||
| 34 | public $onPreDownload; | ||
| 35 | |||
| 36 | /** @var Aspects\JoinPoint */ | ||
| 37 | public $onPostDownload; | ||
| 38 | |||
| 39 | /** | ||
| 40 | * @param IO\IOInterface $io | ||
| 41 | * @param Config $config | ||
| 42 | * @param array $options | ||
| 43 | */ | ||
| 44 | public function __construct(IO\IOInterface $io, Config $config = null, array $options = array()) | ||
| 45 |     { | ||
| 46 | $this->io = $io; | ||
| 47 | $this->config = $config; | ||
| 48 | $this->options = $options; | ||
| 49 | } | ||
| 50 | |||
| 51 | public function setPluginConfig(array $pluginConfig) | ||
| 52 |     { | ||
| 53 | $this->pluginConfig = $pluginConfig; | ||
| 54 | } | ||
| 55 | |||
| 56 | /** | ||
| 57 | * Copy the remote file in local. | ||
| 58 | * | ||
| 59 | * @param string $origin host/domain text | ||
| 60 | * @param string $fileUrl targeturl | ||
| 61 | * @param string $fileName the local filename | ||
| 62 | * @param bool $progress Display the progression | ||
| 63 | * @param array $options Additional context options | ||
| 64 | * | ||
| 65 | * @return bool true | ||
| 66 | */ | ||
| 67 | public function copy($origin, $fileUrl, $fileName, $progress=true, $options=array()) | ||
| 68 |     { | ||
| 69 | $that = $this; // for PHP5.3 | ||
| 70 | |||
| 71 |         return $this->fetch($origin, $fileUrl, $progress, $options, function ($ch, $request) use ($that, $fileName) { | ||
| 72 | $outputFile = new OutputFile($fileName); | ||
| 73 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, false); | ||
| 74 | curl_setopt($ch, CURLOPT_FILE, $outputFile->getPointer()); | ||
| 75 | |||
| 76 | list($execStatus, $response) = $result = $that->exec($ch, $request); | ||
|  | |||
| 77 | |||
| 78 | curl_setopt($ch, CURLOPT_FILE, STDOUT); | ||
| 79 | |||
| 80 |             if (200 !== $response->info['http_code']) { | ||
| 81 | $outputFile->setFailure(); | ||
| 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 { | ||
| 114 | $this->_retry = false; | ||
| 115 | |||
| 116 | $request = new Aspects\HttpGetRequest($origin, $fileUrl, $this->io); | ||
| 117 | $request->setSpecial(array( | ||
| 118 |                 'github' => $this->config->get('github-domains') ?: array(), | ||
| 119 |                 'gitlab' => $this->config->get('gitlab-domains') ?: array(), | ||
| 120 | )); | ||
| 121 | $this->onPreDownload = Factory::getPreEvent($request); | ||
| 122 | $this->onPostDownload = Factory::getPostEvent($request); | ||
| 123 |             if ($this->_degradedMode) { | ||
| 124 | $this->onPreDownload->attach(new Aspects\AspectDegradedMode); | ||
| 125 | } | ||
| 126 | |||
| 127 | $options += $this->options; | ||
| 128 | // override | ||
| 129 |             if ('github' === $request->special && isset($options['github-token'])) { | ||
| 130 | $request->query['access_token'] = $options['github-token']; | ||
| 131 | } | ||
| 132 |             if ('gitlab' === $request->special && isset($options['gitlab-token'])) { | ||
| 133 | $request->query['access_token'] = $options['gitlab-token']; | ||
| 134 | } | ||
| 135 | |||
| 136 |             if ($this->io->isDebug()) { | ||
| 137 |                 $this->io->write('Downloading ' . $fileUrl); | ||
| 138 | } | ||
| 139 | |||
| 140 |             if ($progress) { | ||
| 141 |                 $this->io->write("    Downloading: <comment>Connecting...</comment>", false); | ||
| 142 | $request->curlOpts[CURLOPT_NOPROGRESS] = false; | ||
| 143 | $request->curlOpts[CURLOPT_PROGRESSFUNCTION] = array($this, 'progress'); | ||
| 144 |             } else { | ||
| 145 | $request->curlOpts[CURLOPT_NOPROGRESS] = true; | ||
| 146 | $request->curlOpts[CURLOPT_PROGRESSFUNCTION] = null; | ||
| 147 | } | ||
| 148 | |||
| 149 | $this->onPreDownload->notify(); | ||
| 150 | |||
| 151 | $opts = $request->getCurlOpts(); | ||
| 152 |             if (empty($opts[CURLOPT_USERPWD])) { | ||
| 153 | unset($opts[CURLOPT_USERPWD]); | ||
| 154 | } | ||
| 155 | $ch = Factory::getConnection($origin, isset($opts[CURLOPT_USERPWD])); | ||
| 156 | |||
| 157 |             if ($this->pluginConfig['insecure']) { | ||
| 158 | $opts[CURLOPT_SSL_VERIFYPEER] = false; | ||
| 159 | } | ||
| 160 |             if (! empty($pluginConfig['capath'])) { | ||
| 161 | $opts[CURLOPT_CAPATH] = $pluginConfig['capath']; | ||
| 162 | } | ||
| 163 | |||
| 164 | curl_setopt_array($ch, $opts); | ||
| 165 | |||
| 166 | list($execStatus, $response) = $exec($ch, $request); | ||
| 167 | } while ($this->_retry); | ||
| 168 | |||
| 169 |         if ($progress) { | ||
| 170 |             $this->io->overwrite("    Downloading: <comment>100%</comment>"); | ||
| 171 | } | ||
| 172 | |||
| 173 | return $execStatus; | ||
| 174 | } | ||
| 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) | ||
| 203 |     { | ||
| 204 | $this->_lastHeaders = array(); | ||
| 205 | curl_setopt($ch, CURLOPT_HEADERFUNCTION, array($this, 'processHeader')); | ||
| 206 | $execStatus = curl_exec($ch); | ||
| 207 | |||
| 208 | $response = new Aspects\HttpGetResponse( | ||
| 209 | curl_errno($ch), | ||
| 210 | curl_error($ch), | ||
| 211 | curl_getinfo($ch) | ||
| 212 | ); | ||
| 213 | $this->onPostDownload->setResponse($response); | ||
| 214 | $this->onPostDownload->notify(); | ||
| 215 | |||
| 216 |         if ($response->needAuth()) { | ||
| 217 | $this->promptAuth($request, $response); | ||
| 218 | } | ||
| 219 | |||
| 220 | return array($execStatus, $response); | ||
| 221 | } | ||
| 222 | |||
| 223 | /** | ||
| 224 | * @internal | ||
| 225 | * @param resource $ch | ||
| 226 | * @param int $downBytesMax | ||
| 227 | * @param int $downBytes | ||
| 228 | * @param int $upBytesMax | ||
| 229 | * @param int $upBytes | ||
| 230 | */ | ||
| 231 | public function progress() | ||
| 232 |     { | ||
| 233 | // @codeCoverageIgnoreStart | ||
| 234 |         if (PHP_VERSION_ID >= 50500) { | ||
| 235 | list($ch, $downBytesMax, $downBytes, $upBytesMax, $upBytes) = func_get_args(); | ||
| 236 |         } else { | ||
| 237 | list($downBytesMax, $downBytes, $upBytesMax, $upBytes) = func_get_args(); | ||
| 238 | } | ||
| 239 | // @codeCoverageIgnoreEnd | ||
| 240 | |||
| 241 |         if ($downBytesMax <= 0 || $downBytesMax < $downBytes) { | ||
| 242 | return 0; | ||
| 243 | } | ||
| 244 | |||
| 245 | $progression = intval($downBytes / $downBytesMax * 100); | ||
| 246 |         $this->io->overwrite("    Downloading: <comment>$progression%</comment>", false); | ||
| 247 | return 0; | ||
| 248 | } | ||
| 249 | |||
| 250 | /** | ||
| 251 | * @internal | ||
| 252 | * @param resource $ch | ||
| 253 | * @param string $header | ||
| 254 | * @return int | ||
| 255 | */ | ||
| 256 | public function processHeader($ch, $header) | ||
| 261 | |||
| 262 | protected function promptAuth(Aspects\HttpGetRequest $req, Aspects\HttpGetResponse $res) | ||
| 263 |     { | ||
| 264 | $io = $this->io; | ||
| 265 | $httpCode = $res->info['http_code']; | ||
| 266 | |||
| 267 | View Code Duplication |         if ('github' === $req->special) { | |
| 347 | } | ||
| 348 | 
This checks looks for assignemnts to variables using the
list(...)function, where not all assigned variables are subsequently used.Consider the following code example.
Only the variables
$aand$care used. There was no need to assign$b.Instead, the list call could have been.