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 GuzzleAdapter 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 GuzzleAdapter, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 15 | class GuzzleAdapter implements AdapterInterface |
||
| 16 | { |
||
| 17 | /** |
||
| 18 | * The base URL. |
||
| 19 | * |
||
| 20 | * @var string |
||
| 21 | */ |
||
| 22 | protected $base; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * The Guzzle HTTP client. |
||
| 26 | * |
||
| 27 | * @var \GuzzleHttp\ClientInterface |
||
| 28 | */ |
||
| 29 | protected $client; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * The visibility of this adapter. |
||
| 33 | * |
||
| 34 | * @var string |
||
| 35 | */ |
||
| 36 | protected $visibility = AdapterInterface::VISIBILITY_PUBLIC; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Constructs a GuzzleAdapter object. |
||
| 40 | * |
||
| 41 | * @param string $base The base URL. |
||
| 42 | * @param \GuzzleHttp\ClientInterface $client An optional Guzzle client. |
||
| 43 | */ |
||
| 44 | 3 | public function __construct($base, ClientInterface $client = null) |
|
| 68 | |||
| 69 | /** |
||
| 70 | * Returns the base URL. |
||
| 71 | * |
||
| 72 | * @return string The base URL. |
||
| 73 | */ |
||
| 74 | 3 | public function getBaseUrl() |
|
| 78 | |||
| 79 | /** |
||
| 80 | * @inheritdoc |
||
| 81 | */ |
||
| 82 | 3 | public function copy($path, $newpath) |
|
| 86 | |||
| 87 | /** |
||
| 88 | * @inheritdoc |
||
| 89 | */ |
||
| 90 | 3 | public function createDir($path, Config $config) |
|
| 94 | |||
| 95 | /** |
||
| 96 | * @inheritdoc |
||
| 97 | */ |
||
| 98 | 3 | public function delete($path) |
|
| 102 | |||
| 103 | /** |
||
| 104 | * @inheritdoc |
||
| 105 | */ |
||
| 106 | 3 | public function deleteDir($path) |
|
| 110 | |||
| 111 | /** |
||
| 112 | * @inheritdoc |
||
| 113 | */ |
||
| 114 | 3 | public function getMetadata($path) |
|
| 140 | |||
| 141 | /** |
||
| 142 | * @inheritdoc |
||
| 143 | */ |
||
| 144 | 3 | public function getMimetype($path) |
|
| 148 | |||
| 149 | /** |
||
| 150 | * @inheritdoc |
||
| 151 | */ |
||
| 152 | 3 | public function getSize($path) |
|
| 156 | |||
| 157 | /** |
||
| 158 | * @inheritdoc |
||
| 159 | */ |
||
| 160 | 3 | public function getTimestamp($path) |
|
| 164 | |||
| 165 | /** |
||
| 166 | * @inheritdoc |
||
| 167 | */ |
||
| 168 | 3 | public function getVisibility($path) |
|
| 175 | |||
| 176 | /** |
||
| 177 | * @inheritdoc |
||
| 178 | */ |
||
| 179 | 3 | public function has($path) |
|
| 187 | |||
| 188 | /** |
||
| 189 | * @inheritdoc |
||
| 190 | */ |
||
| 191 | 3 | public function listContents($directory = '', $recursive = false) |
|
| 195 | |||
| 196 | /** |
||
| 197 | * @inheritdoc |
||
| 198 | */ |
||
| 199 | 3 | public function read($path) |
|
| 210 | |||
| 211 | /** |
||
| 212 | * @inheritdoc |
||
| 213 | */ |
||
| 214 | 3 | public function readStream($path) |
|
| 225 | |||
| 226 | /** |
||
| 227 | * @inheritdoc |
||
| 228 | */ |
||
| 229 | 3 | public function rename($path, $newpath) |
|
| 233 | |||
| 234 | /** |
||
| 235 | * @inheritdoc |
||
| 236 | */ |
||
| 237 | 3 | public function setVisibility($path, $visibility) |
|
| 245 | |||
| 246 | /** |
||
| 247 | * @inheritdoc |
||
| 248 | */ |
||
| 249 | 3 | public function update($path, $contents, Config $conf) |
|
| 253 | |||
| 254 | /** |
||
| 255 | * @inheritdoc |
||
| 256 | */ |
||
| 257 | 3 | public function updateStream($path, $resource, Config $config) |
|
| 261 | |||
| 262 | /** |
||
| 263 | * @inheritdoc |
||
| 264 | */ |
||
| 265 | 3 | public function write($path, $contents, Config $config) |
|
| 269 | |||
| 270 | /** |
||
| 271 | * @inheritdoc |
||
| 272 | */ |
||
| 273 | 3 | public function writeStream($path, $resource, Config $config) |
|
| 277 | |||
| 278 | /** |
||
| 279 | * Performs a GET request. |
||
| 280 | * |
||
| 281 | * @param string $path The path to GET. |
||
| 282 | * |
||
| 283 | * @return Response|false The response or false if failed. |
||
| 284 | */ |
||
| 285 | 6 | View Code Duplication | protected function get($path) |
| 299 | |||
| 300 | /** |
||
| 301 | * Performs a HEAD request. |
||
| 302 | * |
||
| 303 | * @param string $path The path to HEAD. |
||
| 304 | * |
||
| 305 | * @return Response|false The response or false if failed. |
||
| 306 | */ |
||
| 307 | 6 | View Code Duplication | protected function head($path) |
| 321 | } |
||
| 322 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..