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 DropboxAdapter 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 DropboxAdapter, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 12 | class DropboxAdapter extends AbstractAdapter |
||
| 13 | { |
||
| 14 | use NotSupportingVisibilityTrait; |
||
| 15 | |||
| 16 | /** @var \Spatie\FlysystemDropbox\DropboxClient */ |
||
| 17 | protected $client; |
||
| 18 | |||
| 19 | public function __construct(Client $client, string $prefix = '') |
||
| 20 | { |
||
| 21 | $this->client = $client; |
||
|
|
|||
| 22 | |||
| 23 | $this->setPathPrefix($prefix); |
||
| 24 | } |
||
| 25 | |||
| 26 | /** |
||
| 27 | * {@inheritdoc} |
||
| 28 | */ |
||
| 29 | public function write($path, $contents, Config $config) |
||
| 33 | |||
| 34 | /** |
||
| 35 | * {@inheritdoc} |
||
| 36 | */ |
||
| 37 | public function writeStream($path, $resource, Config $config) |
||
| 41 | |||
| 42 | /** |
||
| 43 | * {@inheritdoc} |
||
| 44 | */ |
||
| 45 | public function update($path, $contents, Config $config) |
||
| 49 | |||
| 50 | /** |
||
| 51 | * {@inheritdoc} |
||
| 52 | */ |
||
| 53 | public function updateStream($path, $resource, Config $config) |
||
| 57 | |||
| 58 | /** |
||
| 59 | * {@inheritdoc} |
||
| 60 | */ |
||
| 61 | public function rename($path, $newPath): bool |
||
| 74 | |||
| 75 | /** |
||
| 76 | * {@inheritdoc} |
||
| 77 | */ |
||
| 78 | View Code Duplication | public function copy($path, $newpath): bool |
|
| 91 | |||
| 92 | /** |
||
| 93 | * {@inheritdoc} |
||
| 94 | */ |
||
| 95 | public function delete($path): bool |
||
| 107 | |||
| 108 | /** |
||
| 109 | * {@inheritdoc} |
||
| 110 | */ |
||
| 111 | public function deleteDir($dirname): bool |
||
| 115 | |||
| 116 | /** |
||
| 117 | * {@inheritdoc} |
||
| 118 | */ |
||
| 119 | View Code Duplication | public function createDir($dirname, Config $config) |
|
| 120 | { |
||
| 121 | $path = $this->applyPathPrefix($dirname); |
||
| 122 | |||
| 123 | try { |
||
| 124 | $object = $this->client->createFolder($path); |
||
| 125 | } catch (BadRequest $e) { |
||
| 126 | return false; |
||
| 127 | } |
||
| 128 | |||
| 129 | return $this->normalizeResponse($object); |
||
| 130 | } |
||
| 131 | |||
| 132 | /** |
||
| 133 | * {@inheritdoc} |
||
| 134 | */ |
||
| 135 | public function has($path) |
||
| 139 | |||
| 140 | /** |
||
| 141 | * {@inheritdoc} |
||
| 142 | */ |
||
| 143 | public function read($path) |
||
| 155 | |||
| 156 | /** |
||
| 157 | * {@inheritdoc} |
||
| 158 | */ |
||
| 159 | View Code Duplication | public function readStream($path) |
|
| 171 | |||
| 172 | /** |
||
| 173 | * {@inheritdoc} |
||
| 174 | */ |
||
| 175 | public function listContents($directory = '', $recursive = false): array |
||
| 219 | |||
| 220 | /** |
||
| 221 | * {@inheritdoc} |
||
| 222 | */ |
||
| 223 | View Code Duplication | public function getMetadata($path) |
|
| 235 | |||
| 236 | /** |
||
| 237 | * {@inheritdoc} |
||
| 238 | */ |
||
| 239 | public function getSize($path) |
||
| 243 | |||
| 244 | /** |
||
| 245 | * {@inheritdoc} |
||
| 246 | */ |
||
| 247 | public function getMimetype($path) |
||
| 251 | |||
| 252 | /** |
||
| 253 | * {@inheritdoc} |
||
| 254 | */ |
||
| 255 | public function getTimestamp($path) |
||
| 259 | |||
| 260 | public function getTemporaryLink(string $path): string |
||
| 264 | |||
| 265 | public function getThumbnail(string $path, string $format = 'jpeg', string $size = 'w64h64') |
||
| 269 | |||
| 270 | /** |
||
| 271 | * {@inheritdoc} |
||
| 272 | */ |
||
| 273 | public function applyPathPrefix($path): string |
||
| 279 | |||
| 280 | public function getClient(): Client |
||
| 284 | |||
| 285 | /** |
||
| 286 | * @param string $path |
||
| 287 | * @param resource|string $contents |
||
| 288 | * @param string $mode |
||
| 289 | * |
||
| 290 | * @return array|false file metadata |
||
| 291 | */ |
||
| 292 | View Code Duplication | protected function upload(string $path, $contents, string $mode) |
|
| 304 | |||
| 305 | protected function normalizeResponse(array $response): array |
||
| 324 | } |
||
| 325 |
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..