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 Connection 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 Connection, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 20 | class Connection |
||
| 21 | { |
||
| 22 | |||
| 23 | /** |
||
| 24 | * Use Rackspace servicenet to access Cloud Files. |
||
| 25 | * |
||
| 26 | * @var boolean |
||
| 27 | */ |
||
| 28 | protected $useServicenet = false; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * User-Agent for request. |
||
| 32 | * |
||
| 33 | * @var string |
||
| 34 | */ |
||
| 35 | protected $userAgent = 'PHP OpenStackStorage'; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Request timeout. |
||
| 39 | * |
||
| 40 | * @var integer |
||
| 41 | */ |
||
| 42 | protected $timeout = 5; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Authentication object. |
||
| 46 | * |
||
| 47 | * @var \OpenStackStorage\Authentication |
||
| 48 | */ |
||
| 49 | protected $auth = null; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Authentication has already been processed. |
||
| 53 | * |
||
| 54 | * @var boolean |
||
| 55 | */ |
||
| 56 | protected $isAuthenticated = false; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Authentication token. |
||
| 60 | * |
||
| 61 | * @var string |
||
| 62 | */ |
||
| 63 | protected $authToken = null; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Array with information about the connection URI. |
||
| 67 | * |
||
| 68 | * @var array |
||
| 69 | */ |
||
| 70 | protected $connectionUrlInfo = null; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * HTTP-client to work with storage. |
||
| 74 | * |
||
| 75 | * @var \OpenStackStorage\Client |
||
| 76 | */ |
||
| 77 | protected $client = null; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * CDN connection URL. |
||
| 81 | * |
||
| 82 | * @var string |
||
| 83 | */ |
||
| 84 | protected $cdnUrl = null; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Is the access via CDN enabled. |
||
| 88 | * |
||
| 89 | * @var boolean |
||
| 90 | */ |
||
| 91 | protected $cdnEnabled = false; |
||
| 92 | |||
| 93 | /** |
||
| 94 | * HTTP-client to work with storage. |
||
| 95 | * |
||
| 96 | * @var \OpenStackStorage\Client |
||
| 97 | */ |
||
| 98 | protected $cdnClient = null; |
||
| 99 | |||
| 100 | /** |
||
| 101 | * List of parameters that are allowed to be used in the GET-requests to |
||
| 102 | * fetch information about the containers: |
||
| 103 | * — limit For an integer value n, limits the number of results |
||
| 104 | * to n values. |
||
| 105 | * — marker Given a string value x, return container names greater |
||
| 106 | * in value than the specified marker. |
||
| 107 | * — end_marker Given a string value x, return container names less |
||
| 108 | * in value than the specified marker. |
||
| 109 | * — format Response format (json, xml, plain). |
||
| 110 | * |
||
| 111 | * @link http://docs.openstack.org/api/openstack-object-storage/1.0/content/s_listcontainers.html |
||
| 112 | * @var array |
||
| 113 | */ |
||
| 114 | protected static $allowedParameters = array( |
||
| 115 | 'limit', |
||
| 116 | 'marker', |
||
| 117 | 'end_marker', |
||
| 118 | 'format', |
||
| 119 | ); |
||
| 120 | |||
| 121 | /** |
||
| 122 | * Local cache of requests to fetch list of containers. |
||
| 123 | * |
||
| 124 | * @var array |
||
| 125 | */ |
||
| 126 | protected static $listContainersCache = array(); |
||
| 127 | |||
| 128 | /** |
||
| 129 | * The class constructor. |
||
| 130 | * |
||
| 131 | * @param string $username |
||
| 132 | * @param string $apiKey |
||
| 133 | * @param array $options |
||
| 134 | * @param integer $timeout |
||
| 135 | * @throws \InvalidArgumentException |
||
| 136 | */ |
||
| 137 | public function __construct($username, $apiKey, $options = array(), $timeout = 5) |
||
| 162 | |||
| 163 | /** |
||
| 164 | * Return the value of the $authToken property. |
||
| 165 | * |
||
| 166 | * @return string |
||
| 167 | */ |
||
| 168 | public function getAuthToken() |
||
| 174 | |||
| 175 | /** |
||
| 176 | * Return the value of the $cdnEnabled property. |
||
| 177 | * |
||
| 178 | * @return boolean |
||
| 179 | */ |
||
| 180 | public function getCdnEnabled() |
||
| 184 | |||
| 185 | /** |
||
| 186 | * Return the value of the $connection property. |
||
| 187 | * |
||
| 188 | * @return \OpenStackStorage\Client |
||
| 189 | */ |
||
| 190 | public function getClient() |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Return the value of the $timeout property. |
||
| 197 | * |
||
| 198 | * @return integer |
||
| 199 | */ |
||
| 200 | public function getTimeout() |
||
| 204 | |||
| 205 | /** |
||
| 206 | * Return the value of the $userAgent property. |
||
| 207 | * |
||
| 208 | * @return string |
||
| 209 | */ |
||
| 210 | public function getUserAgent() |
||
| 214 | |||
| 215 | /** |
||
| 216 | * Performs an http request to the storage. |
||
| 217 | * |
||
| 218 | * @param string $method name of the method (i.e. GET, PUT, POST, etc) |
||
| 219 | * @param array $path list of tokens that will be added to connection |
||
| 220 | * URI string |
||
| 221 | * @param array $headers additional headers |
||
| 222 | * @param array $parameters additional parameters that will be added to the |
||
| 223 | * query string |
||
| 224 | * @return array |
||
| 225 | */ |
||
| 226 | public function makeRequest($method, array $path = array(), array $headers = array(), $parameters = array()) |
||
| 232 | |||
| 233 | /** |
||
| 234 | * Performs an http request to the CDN. |
||
| 235 | * |
||
| 236 | * @param string $method name of the method (i.e. GET, PUT, POST, etc) |
||
| 237 | * @param array $path list of tokens that will be added to connection |
||
| 238 | * URI string |
||
| 239 | * @param array $headers additional headers |
||
| 240 | * @return array |
||
| 241 | * @throws \OpenStackStorage\Exceptions\CDNNotEnabled |
||
| 242 | */ |
||
| 243 | public function makeCdnRequest($method, array $path = array(), array $headers = array()) |
||
| 253 | |||
| 254 | /** |
||
| 255 | * Return array with number of containers, total bytes in the account and |
||
| 256 | * account metadata. |
||
| 257 | * |
||
| 258 | * @return array |
||
| 259 | */ |
||
| 260 | public function getAccountInfo() |
||
| 285 | |||
| 286 | /** |
||
| 287 | * Update account metadata. |
||
| 288 | * |
||
| 289 | * Example: |
||
| 290 | * <code> |
||
| 291 | * $connection->updateAccountMetadata(array( |
||
| 292 | * 'X-Account-Meta-Foo' => 'bar', |
||
| 293 | * )); |
||
| 294 | * </code> |
||
| 295 | * |
||
| 296 | * @param array $metadata |
||
| 297 | */ |
||
| 298 | public function updateAccountMetadata(array $metadata) |
||
| 302 | |||
| 303 | /** |
||
| 304 | * Create new container. |
||
| 305 | * |
||
| 306 | * If $errorOnExisting is true and container already exists, |
||
| 307 | * throws \OpenStackStorage\Exceptions\ContainerExists. |
||
| 308 | * |
||
| 309 | * @param string $name |
||
| 310 | * @param boolean $errorOnExisting |
||
| 311 | * @return \OpenStackStorage\Container |
||
| 312 | * @throws \OpenStackStorage\Exceptions\ContainerExists |
||
| 313 | */ |
||
| 314 | public function createContainer($name, $errorOnExisting = false) |
||
| 325 | |||
| 326 | /** |
||
| 327 | * Delete container. |
||
| 328 | * |
||
| 329 | * @param \OpenStackStorage\Container|string $container |
||
| 330 | * @throws \OpenStackStorage\Exceptions\NoSuchContainer |
||
| 331 | * @throws \Exception|\OpenStackStorage\Exceptions\ResponseError |
||
| 332 | * @throws \OpenStackStorage\Exceptions\ContainerNotEmpty |
||
| 333 | */ |
||
| 334 | public function deleteContainer($container) |
||
| 370 | |||
| 371 | /** |
||
| 372 | * Return container object. |
||
| 373 | * |
||
| 374 | * @param string $name |
||
| 375 | * @return \OpenStackStorage\Container |
||
| 376 | * @throws \OpenStackStorage\Exceptions\NoSuchContainer |
||
| 377 | * @throws \Exception|\OpenStackStorage\Exceptions\ResponseError |
||
| 378 | */ |
||
| 379 | public function getContainer($name) |
||
| 405 | |||
| 406 | /** |
||
| 407 | * Return array with containers. |
||
| 408 | * |
||
| 409 | * @param array $parameters |
||
| 410 | * @return \OpenStackStorage\Container[] |
||
| 411 | */ |
||
| 412 | public function getContainers(array $parameters = array()) |
||
| 422 | |||
| 423 | /** |
||
| 424 | * Return names of public containers. |
||
| 425 | * |
||
| 426 | * @return array |
||
| 427 | * @throws \OpenStackStorage\Exceptions\CDNNotEnabled |
||
| 428 | */ |
||
| 429 | public function getPublicContainersList() |
||
| 439 | |||
| 440 | /** |
||
| 441 | * Return information about containers. |
||
| 442 | * |
||
| 443 | * @see \OpenStackStorage\Connection::$allowedParameters |
||
| 444 | * @param array $parameters |
||
| 445 | * @return array |
||
| 446 | */ |
||
| 447 | public function getContainersInfo(array $parameters = array()) |
||
| 453 | |||
| 454 | /** |
||
| 455 | * Return names of containers. |
||
| 456 | * |
||
| 457 | * @see \OpenStackStorage\Connection::$allowedParameters |
||
| 458 | * @param array $parameters |
||
| 459 | * @return array |
||
| 460 | */ |
||
| 461 | public function getContainersList(array $parameters = array()) |
||
| 467 | |||
| 468 | /** |
||
| 469 | * Generate path for query string. |
||
| 470 | * |
||
| 471 | * @param array $path |
||
| 472 | * @return string |
||
| 473 | */ |
||
| 474 | protected function getPathFromArray(array $path = array()) |
||
| 488 | |||
| 489 | /** |
||
| 490 | * Authenticate and setup this instance with the values returned. |
||
| 491 | */ |
||
| 492 | protected function authenticate() |
||
| 510 | |||
| 511 | /** |
||
| 512 | * Setup the http connection instance. |
||
| 513 | */ |
||
| 514 | protected function httpConnect() |
||
| 525 | |||
| 526 | /** |
||
| 527 | * Setup the http connection instance for the CDN service. |
||
| 528 | */ |
||
| 529 | protected function cdnConnect() |
||
| 542 | |||
| 543 | /** |
||
| 544 | * Performs the real http request. |
||
| 545 | * |
||
| 546 | * @param \OpenStackStorage\Client $client |
||
| 547 | * @param string $method |
||
| 548 | * @param string $path |
||
| 549 | * @param array $parameters |
||
| 550 | * @param array $headers |
||
| 551 | * @return array |
||
| 552 | * @throws \Exception |
||
| 553 | */ |
||
| 554 | protected function makeRealRequest(Client $client, $method, $path, $parameters = array(), array $headers = array()) |
||
| 560 | |||
| 561 | /** |
||
| 562 | * Validates the container name. |
||
| 563 | * |
||
| 564 | * @param string $name |
||
| 565 | * @throws \OpenStackStorage\Exceptions\InvalidContainerName |
||
| 566 | */ |
||
| 567 | protected function validateContainerName($name) |
||
| 575 | |||
| 576 | /** |
||
| 577 | * Return a raw response string with containers data. |
||
| 578 | * |
||
| 579 | * @see \OpenStackStorage\Connection::$allowedParameters |
||
| 580 | * @param array $parameters |
||
| 581 | * @return string |
||
| 582 | */ |
||
| 583 | protected function getContainersRawData(array $parameters = array()) |
||
| 602 | } |
||
| 603 |