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 Adapter 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 Adapter, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 17 | class Adapter extends AbstractAdapter |
||
| 18 | { |
||
| 19 | /** |
||
| 20 | * @var Api |
||
| 21 | */ |
||
| 22 | protected $cosApi; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * @var string |
||
| 26 | */ |
||
| 27 | protected $bucket; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @var bool |
||
| 31 | */ |
||
| 32 | protected $debug; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Adapter constructor. |
||
| 36 | * |
||
| 37 | * @param Api $cosApi |
||
| 38 | * @param array $config |
||
| 39 | */ |
||
| 40 | public function __construct(Api $cosApi, array $config) |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @return string |
||
| 52 | */ |
||
| 53 | 16 | public function getBucket() |
|
| 57 | |||
| 58 | /** |
||
| 59 | * @param string $path |
||
| 60 | * |
||
| 61 | * @return string |
||
| 62 | */ |
||
| 63 | 1 | public function getUrl($path) |
|
| 67 | |||
| 68 | /** |
||
| 69 | * @param string $path |
||
| 70 | * @param \DateTimeInterface $expiration |
||
| 71 | * @param array $options |
||
| 72 | * |
||
| 73 | * @return string|bool |
||
| 74 | */ |
||
| 75 | 1 | public function getTemporaryUrl($path, DateTimeInterface $expiration, array $options = []) |
|
| 85 | |||
| 86 | /** |
||
| 87 | * @param string $path |
||
| 88 | * @param string $contents |
||
| 89 | * @param Config $config |
||
| 90 | * |
||
| 91 | * @throws RuntimeException |
||
| 92 | * |
||
| 93 | * @return array|bool |
||
| 94 | */ |
||
| 95 | View Code Duplication | public function write($path, $contents, Config $config) |
|
| 110 | |||
| 111 | /** |
||
| 112 | * @param string $path |
||
| 113 | * @param resource $resource |
||
| 114 | * @param Config $config |
||
| 115 | * |
||
| 116 | * @throws RuntimeException |
||
| 117 | * |
||
| 118 | * @return array|bool |
||
| 119 | */ |
||
| 120 | 1 | View Code Duplication | public function writeStream($path, $resource, Config $config) |
| 135 | |||
| 136 | /** |
||
| 137 | * @param string $path |
||
| 138 | * @param string $contents |
||
| 139 | * @param Config $config |
||
| 140 | * |
||
| 141 | * @throws RuntimeException |
||
| 142 | * |
||
| 143 | * @return array|bool |
||
| 144 | */ |
||
| 145 | View Code Duplication | public function update($path, $contents, Config $config) |
|
| 160 | |||
| 161 | /** |
||
| 162 | * @param string $path |
||
| 163 | * @param resource $resource |
||
| 164 | * @param Config $config |
||
| 165 | * |
||
| 166 | * @throws RuntimeException |
||
| 167 | * |
||
| 168 | * @return array|bool |
||
| 169 | */ |
||
| 170 | 1 | View Code Duplication | public function updateStream($path, $resource, Config $config) |
| 185 | |||
| 186 | /** |
||
| 187 | * @param string $path |
||
| 188 | * @param string $newpath |
||
| 189 | * |
||
| 190 | * @return bool |
||
| 191 | */ |
||
| 192 | 1 | public function rename($path, $newpath) |
|
| 198 | |||
| 199 | /** |
||
| 200 | * @param string $path |
||
| 201 | * @param string $newpath |
||
| 202 | * |
||
| 203 | * @return bool |
||
| 204 | */ |
||
| 205 | 1 | public function copy($path, $newpath) |
|
| 211 | |||
| 212 | /** |
||
| 213 | * @param string $path |
||
| 214 | * |
||
| 215 | * @return bool |
||
| 216 | */ |
||
| 217 | 1 | public function delete($path) |
|
| 223 | |||
| 224 | /** |
||
| 225 | * @param string $dirname |
||
| 226 | * |
||
| 227 | * @return bool |
||
| 228 | */ |
||
| 229 | 1 | public function deleteDir($dirname) |
|
| 235 | |||
| 236 | /** |
||
| 237 | * @param string $dirname |
||
| 238 | * @param Config $config |
||
| 239 | * |
||
| 240 | * @return array|bool |
||
| 241 | */ |
||
| 242 | 1 | public function createDir($dirname, Config $config) |
|
| 248 | |||
| 249 | /** |
||
| 250 | * @param string $path |
||
| 251 | * @param string $visibility |
||
| 252 | * |
||
| 253 | * @return bool |
||
| 254 | */ |
||
| 255 | 1 | public function setVisibility($path, $visibility) |
|
| 264 | |||
| 265 | /** |
||
| 266 | * @param string $path |
||
| 267 | * |
||
| 268 | * @return bool |
||
| 269 | */ |
||
| 270 | 1 | public function has($path) |
|
| 278 | |||
| 279 | /** |
||
| 280 | * @param string $path |
||
| 281 | * |
||
| 282 | * @return array|bool |
||
| 283 | */ |
||
| 284 | public function read($path) |
||
| 290 | |||
| 291 | /** |
||
| 292 | * @param string $path |
||
| 293 | * |
||
| 294 | * @return array|bool |
||
| 295 | */ |
||
| 296 | public function readStream($path) |
||
| 302 | |||
| 303 | /** |
||
| 304 | * @param string $directory |
||
| 305 | * @param bool $recursive |
||
| 306 | * |
||
| 307 | * @return array|bool |
||
| 308 | */ |
||
| 309 | 1 | public function listContents($directory = '', $recursive = false) |
|
| 315 | |||
| 316 | /** |
||
| 317 | * @param string $path |
||
| 318 | * |
||
| 319 | * @return array|bool |
||
| 320 | */ |
||
| 321 | 6 | public function getMetadata($path) |
|
| 327 | |||
| 328 | /** |
||
| 329 | * @param string $path |
||
| 330 | * |
||
| 331 | * @return array|bool |
||
| 332 | */ |
||
| 333 | 1 | public function getSize($path) |
|
| 339 | |||
| 340 | /** |
||
| 341 | * @param string $path |
||
| 342 | * |
||
| 343 | * @return array|bool |
||
| 344 | */ |
||
| 345 | 1 | public function getMimetype($path) |
|
| 352 | |||
| 353 | /** |
||
| 354 | * @param string $path |
||
| 355 | * |
||
| 356 | * @return array|bool |
||
| 357 | */ |
||
| 358 | 1 | public function getTimestamp($path) |
|
| 364 | |||
| 365 | /** |
||
| 366 | * @param string $path |
||
| 367 | * |
||
| 368 | * @return array|bool |
||
| 369 | */ |
||
| 370 | 1 | public function getVisibility($path) |
|
| 384 | |||
| 385 | /** |
||
| 386 | * Creates a temporary file. |
||
| 387 | * |
||
| 388 | * @param string $content |
||
| 389 | * |
||
| 390 | * @throws RuntimeException |
||
| 391 | * |
||
| 392 | * @return string |
||
| 393 | */ |
||
| 394 | protected function createTemporaryFile($content) |
||
| 411 | |||
| 412 | /** |
||
| 413 | * Gets a temporary file path. |
||
| 414 | * |
||
| 415 | * @return bool|string |
||
| 416 | */ |
||
| 417 | protected function getTemporaryPath() |
||
| 421 | |||
| 422 | /** |
||
| 423 | * @param string $path |
||
| 424 | * @param string $content |
||
| 425 | * |
||
| 426 | * @return bool |
||
| 427 | */ |
||
| 428 | protected function setContentType($path, $content) |
||
| 438 | |||
| 439 | /** |
||
| 440 | * @param $response |
||
| 441 | * |
||
| 442 | * @throws RuntimeException |
||
| 443 | * |
||
| 444 | * @return mixed |
||
| 445 | */ |
||
| 446 | 16 | protected function normalizeResponse($response) |
|
| 458 | } |
||
| 459 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.