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 |
||
| 16 | class GuzzleAdapter implements AdapterInterface |
||
| 17 | { |
||
| 18 | /** |
||
| 19 | * Whether this endpoint supports HEAD requests. |
||
| 20 | * |
||
| 21 | * @var bool |
||
| 22 | */ |
||
| 23 | protected $supportsHead = true; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * The base URL. |
||
| 27 | * |
||
| 28 | * @var string |
||
| 29 | */ |
||
| 30 | protected $base; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * The Guzzle HTTP client. |
||
| 34 | * |
||
| 35 | * @var \GuzzleHttp\ClientInterface |
||
| 36 | */ |
||
| 37 | protected $client; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * The visibility of this adapter. |
||
| 41 | * |
||
| 42 | * @var string |
||
| 43 | */ |
||
| 44 | protected $visibility = AdapterInterface::VISIBILITY_PUBLIC; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Constructs a GuzzleAdapter object. |
||
| 48 | * |
||
| 49 | * @param string $base The base URL. |
||
| 50 | * @param \GuzzleHttp\ClientInterface $client An optional Guzzle client. |
||
| 51 | */ |
||
| 52 | 3 | public function __construct($base, ClientInterface $client = null) |
|
| 76 | |||
| 77 | /** |
||
| 78 | * Returns the base URL. |
||
| 79 | * |
||
| 80 | * @return string The base URL. |
||
| 81 | */ |
||
| 82 | 3 | public function getBaseUrl() |
|
| 86 | |||
| 87 | /** |
||
| 88 | * @inheritdoc |
||
| 89 | */ |
||
| 90 | 3 | public function copy($path, $newpath) |
|
| 94 | |||
| 95 | /** |
||
| 96 | * @inheritdoc |
||
| 97 | */ |
||
| 98 | 3 | public function createDir($path, Config $config) |
|
| 102 | |||
| 103 | /** |
||
| 104 | * @inheritdoc |
||
| 105 | */ |
||
| 106 | 3 | public function delete($path) |
|
| 110 | |||
| 111 | /** |
||
| 112 | * @inheritdoc |
||
| 113 | */ |
||
| 114 | 3 | public function deleteDir($path) |
|
| 118 | |||
| 119 | /** |
||
| 120 | * @inheritdoc |
||
| 121 | */ |
||
| 122 | 3 | public function getMetadata($path) |
|
| 148 | |||
| 149 | /** |
||
| 150 | * @inheritdoc |
||
| 151 | */ |
||
| 152 | 3 | public function getMimetype($path) |
|
| 156 | |||
| 157 | /** |
||
| 158 | * @inheritdoc |
||
| 159 | */ |
||
| 160 | 3 | public function getSize($path) |
|
| 164 | |||
| 165 | /** |
||
| 166 | * @inheritdoc |
||
| 167 | */ |
||
| 168 | 3 | public function getTimestamp($path) |
|
| 172 | |||
| 173 | /** |
||
| 174 | * @inheritdoc |
||
| 175 | */ |
||
| 176 | 3 | public function getVisibility($path) |
|
| 183 | |||
| 184 | /** |
||
| 185 | * @inheritdoc |
||
| 186 | */ |
||
| 187 | 3 | public function has($path) |
|
| 195 | |||
| 196 | /** |
||
| 197 | * @inheritdoc |
||
| 198 | */ |
||
| 199 | 3 | public function listContents($directory = '', $recursive = false) |
|
| 203 | |||
| 204 | /** |
||
| 205 | * @inheritdoc |
||
| 206 | */ |
||
| 207 | 3 | public function read($path) |
|
| 218 | |||
| 219 | /** |
||
| 220 | * @inheritdoc |
||
| 221 | */ |
||
| 222 | 3 | public function readStream($path) |
|
| 233 | |||
| 234 | /** |
||
| 235 | * @inheritdoc |
||
| 236 | */ |
||
| 237 | 3 | public function rename($path, $newpath) |
|
| 241 | |||
| 242 | /** |
||
| 243 | * @inheritdoc |
||
| 244 | */ |
||
| 245 | 3 | public function setVisibility($path, $visibility) |
|
| 253 | |||
| 254 | /** |
||
| 255 | * @inheritdoc |
||
| 256 | */ |
||
| 257 | 3 | public function update($path, $contents, Config $conf) |
|
| 261 | |||
| 262 | /** |
||
| 263 | * @inheritdoc |
||
| 264 | */ |
||
| 265 | 3 | public function updateStream($path, $resource, Config $config) |
|
| 269 | |||
| 270 | /** |
||
| 271 | * @inheritdoc |
||
| 272 | */ |
||
| 273 | 3 | public function write($path, $contents, Config $config) |
|
| 277 | |||
| 278 | /** |
||
| 279 | * @inheritdoc |
||
| 280 | */ |
||
| 281 | 3 | public function writeStream($path, $resource, Config $config) |
|
| 285 | |||
| 286 | /** |
||
| 287 | * Performs a GET request. |
||
| 288 | * |
||
| 289 | * @param string $path The path to GET. |
||
| 290 | * |
||
| 291 | * @return Response|false The response or false if failed. |
||
| 292 | */ |
||
| 293 | 6 | protected function get($path) |
|
| 307 | |||
| 308 | /** |
||
| 309 | * Performs a HEAD request. |
||
| 310 | * |
||
| 311 | * @param string $path The path to HEAD. |
||
| 312 | * |
||
| 313 | * @return Response|false The response or false if failed. |
||
| 314 | */ |
||
| 315 | 6 | protected function head($path) |
|
| 341 | } |
||
| 342 |
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..