Complex classes like Container 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 Container, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 15 | class Container implements ContainerContract, Countable, JsonSerializable |
||
| 16 | { |
||
| 17 | /** |
||
| 18 | * @var \ArgentCrusade\Selectel\CloudStorage\Contracts\Api\ApiClientContract $api |
||
| 19 | */ |
||
| 20 | protected $api; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * Container name. |
||
| 24 | * |
||
| 25 | * @var string |
||
| 26 | */ |
||
| 27 | protected $name; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * Container data. |
||
| 31 | * |
||
| 32 | * @var array |
||
| 33 | */ |
||
| 34 | protected $data = []; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Determines if container data was already loaded. |
||
| 38 | * |
||
| 39 | * @var bool |
||
| 40 | */ |
||
| 41 | protected $dataLoaded = false; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @param \ArgentCrusade\Selectel\CloudStorage\Contracts\Api\ApiClientContract $api |
||
| 45 | * @param string $name |
||
| 46 | * @param array $data |
||
| 47 | */ |
||
| 48 | public function __construct(ApiClientContract $api, $name, array $data = []) |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Returns specific container data. |
||
| 58 | * |
||
| 59 | * @param string $key |
||
| 60 | * @param mixed $default = null |
||
| 61 | * |
||
| 62 | * @return mixed |
||
| 63 | */ |
||
| 64 | protected function containerData($key, $default = null) |
||
| 72 | |||
| 73 | /** |
||
| 74 | * Lazy loading for container data. |
||
| 75 | * |
||
| 76 | * @throws \ArgentCrusade\Selectel\CloudStorage\Exceptions\ApiRequestFailedException |
||
| 77 | */ |
||
| 78 | protected function loadContainerData() |
||
| 102 | |||
| 103 | /** |
||
| 104 | * Absolute path to file from storage root. |
||
| 105 | * |
||
| 106 | * @param string $path = '' Relative file path. |
||
| 107 | * |
||
| 108 | * @return string |
||
| 109 | */ |
||
| 110 | protected function absolutePath($path = '') |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Container name. |
||
| 117 | * |
||
| 118 | * @return string |
||
| 119 | */ |
||
| 120 | public function name() |
||
| 124 | |||
| 125 | /** |
||
| 126 | * Container visibility type. |
||
| 127 | * |
||
| 128 | * @return string |
||
| 129 | */ |
||
| 130 | public function type() |
||
| 134 | |||
| 135 | /** |
||
| 136 | * Container files count. |
||
| 137 | * |
||
| 138 | * @return int |
||
| 139 | */ |
||
| 140 | public function filesCount() |
||
| 144 | |||
| 145 | /** |
||
| 146 | * Container files count. |
||
| 147 | * |
||
| 148 | * @return int |
||
| 149 | */ |
||
| 150 | public function count() |
||
| 154 | |||
| 155 | /** |
||
| 156 | * Container size in bytes. |
||
| 157 | * |
||
| 158 | * @return int |
||
| 159 | */ |
||
| 160 | public function size() |
||
| 164 | |||
| 165 | /** |
||
| 166 | * Total uploaded (received) bytes. |
||
| 167 | * |
||
| 168 | * @return int |
||
| 169 | */ |
||
| 170 | public function uploadedBytes() |
||
| 174 | |||
| 175 | /** |
||
| 176 | * Total downloaded (transmitted) bytes. |
||
| 177 | * |
||
| 178 | * @return int |
||
| 179 | */ |
||
| 180 | public function downloadedBytes() |
||
| 184 | |||
| 185 | /** |
||
| 186 | * Returns JSON representation of container. |
||
| 187 | * |
||
| 188 | * @return array |
||
| 189 | */ |
||
| 190 | public function jsonSerialize() |
||
| 201 | |||
| 202 | /** |
||
| 203 | * Determines if container is public. |
||
| 204 | * |
||
| 205 | * @return bool |
||
| 206 | */ |
||
| 207 | public function isPublic() |
||
| 211 | |||
| 212 | /** |
||
| 213 | * Determines if container is private. |
||
| 214 | * |
||
| 215 | * @return bool |
||
| 216 | */ |
||
| 217 | public function isPrivate() |
||
| 221 | |||
| 222 | /** |
||
| 223 | * Retrieves files from current container. |
||
| 224 | * |
||
| 225 | * @param string $directory = null |
||
| 226 | * @param string $prefixOrFullPath = null |
||
| 227 | * @param string $delimiter = null |
||
| 228 | * @param int $limit = 10000 |
||
| 229 | * @param string $marker = '' |
||
| 230 | * |
||
| 231 | * @return \ArgentCrusade\Selectel\CloudStorage\Contracts\Collections\CollectionContract |
||
| 232 | */ |
||
| 233 | public function files($directory = null, $prefixOrFullPath = null, $delimiter = null, $limit = 10000, $marker = '') |
||
| 251 | |||
| 252 | /** |
||
| 253 | * Retrieves file object container. This method does not actually download file, see File::download. |
||
| 254 | * |
||
| 255 | * @param string $path |
||
| 256 | * |
||
| 257 | * @throws \ArgentCrusade\Selectel\CloudStorage\Exceptions\FileNotFoundException |
||
| 258 | * @throws \LogicException |
||
| 259 | * |
||
| 260 | * @return \ArgentCrusade\Selectel\CloudStorage\Contracts\FileContract |
||
| 261 | */ |
||
| 262 | public function getFile($path) |
||
| 276 | |||
| 277 | /** |
||
| 278 | * Creates new directory. |
||
| 279 | * |
||
| 280 | * @param string $name Directory name. |
||
| 281 | * |
||
| 282 | * @throws \ArgentCrusade\Selectel\CloudStorage\Exceptions\ApiRequestFailedException |
||
| 283 | * |
||
| 284 | * @return string |
||
| 285 | */ |
||
| 286 | public function createDir($name) |
||
| 300 | |||
| 301 | /** |
||
| 302 | * Deletes directory. |
||
| 303 | * |
||
| 304 | * @param string $name Directory name. |
||
| 305 | */ |
||
| 306 | public function deleteDir($name) |
||
| 316 | |||
| 317 | /** |
||
| 318 | * Uploads file contents from string. Returns ETag header value if upload was successful. |
||
| 319 | * |
||
| 320 | * @param string $path Remote path. |
||
| 321 | * @param string $contents File contents. |
||
| 322 | * @param array $params = [] Upload params. |
||
| 323 | * @param bool $verifyChecksum = true |
||
| 324 | * |
||
| 325 | * @throws \ArgentCrusade\Selectel\CloudStorage\Exceptions\UploadFailedException |
||
| 326 | * |
||
| 327 | * @return string |
||
| 328 | */ |
||
| 329 | public function uploadFromString($path, $contents, array $params = [], $verifyChecksum = true) |
||
| 333 | |||
| 334 | /** |
||
| 335 | * Uploads file from stream. Returns ETag header value if upload was successful. |
||
| 336 | * |
||
| 337 | * @param string $path Remote path. |
||
| 338 | * @param resource $resource Stream resource. |
||
| 339 | * @param array $params = [] Upload params. |
||
| 340 | * |
||
| 341 | * @throws \ArgentCrusade\Selectel\CloudStorage\Exceptions\UploadFailedException |
||
| 342 | * |
||
| 343 | * @return string |
||
| 344 | */ |
||
| 345 | public function uploadFromStream($path, $resource, array $params = []) |
||
| 349 | |||
| 350 | /** |
||
| 351 | * Upload file from string or stream resource. |
||
| 352 | * |
||
| 353 | * @param string $path Remote path. |
||
| 354 | * @param string | resource $contents File contents. |
||
| 355 | * @param array $params = [] Upload params. |
||
| 356 | * @param bool $verifyChecksum = true |
||
| 357 | * |
||
| 358 | * @throws \ArgentCrusade\Selectel\CloudStorage\Exceptions\UploadFailedException |
||
| 359 | * |
||
| 360 | * @return string |
||
| 361 | */ |
||
| 362 | protected function uploadFrom($path, $contents, array $params = [], $verifyChecksum = true) |
||
| 375 | |||
| 376 | /** |
||
| 377 | * Parses upload parameters and assigns them to appropriate HTTP headers. |
||
| 378 | * |
||
| 379 | * @param string $contents = null |
||
| 380 | * @param array $params = [] |
||
| 381 | * @param bool $verifyChecksum = true |
||
| 382 | * |
||
| 383 | * @return array |
||
| 384 | */ |
||
| 385 | protected function convertUploadParamsToHeaders($contents = null, array $params = [], $verifyChecksum = true) |
||
| 408 | |||
| 409 | /** |
||
| 410 | * Deletes container. Container must be empty in order to perform this operation. |
||
| 411 | * |
||
| 412 | * @throws \ArgentCrusade\Selectel\CloudStorage\Exceptions\ApiRequestFailedException |
||
| 413 | */ |
||
| 414 | public function delete() |
||
| 428 | } |
||
| 429 |