Complex classes like Obj 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 Obj, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 14 | class Obj |
||
| 15 | { |
||
| 16 | |||
| 17 | /** |
||
| 18 | * Container object. |
||
| 19 | * |
||
| 20 | * @var \OpenStackStorage\Container |
||
| 21 | */ |
||
| 22 | protected $container = null; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * Object name. |
||
| 26 | * |
||
| 27 | * @var string |
||
| 28 | */ |
||
| 29 | protected $name = null; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * The object's MIME-type. |
||
| 33 | * |
||
| 34 | * @var string |
||
| 35 | */ |
||
| 36 | protected $contentType = null; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * The object size (in bytes). |
||
| 40 | * |
||
| 41 | * @var integer |
||
| 42 | */ |
||
| 43 | protected $size = null; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Date and time of last file modification. |
||
| 47 | * |
||
| 48 | * @var \DateTime |
||
| 49 | */ |
||
| 50 | protected $lastModified = null; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Object's etag. |
||
| 54 | * |
||
| 55 | * @var string |
||
| 56 | */ |
||
| 57 | protected $etag = null; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Metadata. |
||
| 61 | * |
||
| 62 | * @var array |
||
| 63 | */ |
||
| 64 | protected $metadata = array(); |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Headers. |
||
| 68 | * |
||
| 69 | * @var array |
||
| 70 | */ |
||
| 71 | protected $headers = array(); |
||
| 72 | |||
| 73 | /** |
||
| 74 | * Manifest, used when working with big file. |
||
| 75 | * |
||
| 76 | * @var string |
||
| 77 | */ |
||
| 78 | protected $manifest = null; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * The class constructor. |
||
| 82 | * |
||
| 83 | * Storage objects rarely if ever need to be instantiated directly by |
||
| 84 | * the user. |
||
| 85 | * |
||
| 86 | * Instead, use the \OpenStackStorage\Container object methods: |
||
| 87 | * <code> |
||
| 88 | * $container->createObject('test.txt'); |
||
| 89 | * $container->getObject('test.txt'); |
||
| 90 | * $container->getObjects('test.txt'); |
||
| 91 | * </code> |
||
| 92 | * |
||
| 93 | * @param \OpenStackStorage\Container $container |
||
| 94 | * @param string $name |
||
| 95 | * @param boolean $forceExists |
||
| 96 | * @param array $object |
||
| 97 | * @throws \OpenStackStorage\Exceptions\NoSuchObject |
||
| 98 | */ |
||
| 99 | public function __construct(Container $container, $name = null, $forceExists = false, array &$object = array()) |
||
| 116 | |||
| 117 | /** |
||
| 118 | * Set the value of property $name. |
||
| 119 | * |
||
| 120 | * @param string $name |
||
| 121 | */ |
||
| 122 | public function setName($name) |
||
| 126 | |||
| 127 | /** |
||
| 128 | * Return the value of the $name property. |
||
| 129 | * |
||
| 130 | * @return string |
||
| 131 | */ |
||
| 132 | public function getName() |
||
| 136 | |||
| 137 | /** |
||
| 138 | * Set the value of property $contentType. |
||
| 139 | * |
||
| 140 | * @param string $contentType |
||
| 141 | */ |
||
| 142 | public function setContentType($contentType) |
||
| 146 | |||
| 147 | /** |
||
| 148 | * Return the value of the $contentType property. |
||
| 149 | * |
||
| 150 | * @return string |
||
| 151 | */ |
||
| 152 | public function getContentType() |
||
| 156 | |||
| 157 | /** |
||
| 158 | * Set the value of property $metadata. |
||
| 159 | * |
||
| 160 | * @param array $metadata |
||
| 161 | */ |
||
| 162 | public function setMetadata(array $metadata = array()) |
||
| 166 | |||
| 167 | /** |
||
| 168 | * Return the value of the $metadata property. |
||
| 169 | * |
||
| 170 | * @return array |
||
| 171 | */ |
||
| 172 | public function getMetadata() |
||
| 176 | |||
| 177 | /** |
||
| 178 | * Set the value of property $headers. |
||
| 179 | * |
||
| 180 | * @param array $headers |
||
| 181 | */ |
||
| 182 | public function setHeaders(array $headers = array()) |
||
| 186 | |||
| 187 | /** |
||
| 188 | * Return the value of the $headers property. |
||
| 189 | * |
||
| 190 | * @return array |
||
| 191 | */ |
||
| 192 | public function getHeaders() |
||
| 196 | |||
| 197 | /** |
||
| 198 | * Set the value of property $manifest. |
||
| 199 | * |
||
| 200 | * @param string $manifest |
||
| 201 | */ |
||
| 202 | public function setManifest($manifest) |
||
| 206 | |||
| 207 | /** |
||
| 208 | * Return the value of the $manifest property. |
||
| 209 | * |
||
| 210 | * @return string |
||
| 211 | */ |
||
| 212 | public function getManifest() |
||
| 216 | |||
| 217 | /** |
||
| 218 | * Returns the object size in bytes. |
||
| 219 | * |
||
| 220 | * @return integer |
||
| 221 | */ |
||
| 222 | public function getSize() |
||
| 226 | |||
| 227 | /** |
||
| 228 | * Read the content from the remote storage object. |
||
| 229 | * |
||
| 230 | * By default this method will buffer the response in memory and |
||
| 231 | * return it as a string. However, if a \SplFileObject object is passed |
||
| 232 | * in $buffer, the response will be written to it instead. |
||
| 233 | * |
||
| 234 | * @param integer $size combined with offset, defines the length |
||
| 235 | * of data to be read |
||
| 236 | * @param integer $offset combined with size, defines the start |
||
| 237 | * location to be read |
||
| 238 | * @param array $headers |
||
| 239 | * @param \SplFileObject $buffer |
||
| 240 | * @return null|string |
||
| 241 | */ |
||
| 242 | public function read($size = -1, $offset = 0, array $headers = array(), \SplFileObject $buffer = null) |
||
| 268 | |||
| 269 | /** |
||
| 270 | * Save the contents of the object to filename. |
||
| 271 | * |
||
| 272 | * @param string $filename |
||
| 273 | */ |
||
| 274 | public function saveToFilename($filename) |
||
| 280 | |||
| 281 | /** |
||
| 282 | * Write data to the remote storage system. |
||
| 283 | * The $source may be one the following: |
||
| 284 | * — resource |
||
| 285 | * — \SplFileObject instance |
||
| 286 | * — scalar (string) |
||
| 287 | * — null (can be used to create directories when |
||
| 288 | * $contentType = 'application/directory') |
||
| 289 | * |
||
| 290 | * @param mixed $data |
||
| 291 | * @param string $contentType |
||
| 292 | * @param integer $filesize |
||
| 293 | * @throws \OpenStackStorage\Exceptions\Error |
||
| 294 | */ |
||
| 295 | public function write($data, $contentType = null, $filesize = null) |
||
| 356 | |||
| 357 | /** |
||
| 358 | * Commits the metadata and custom headers to the remote storage system. |
||
| 359 | * |
||
| 360 | * Example: |
||
| 361 | * <code> |
||
| 362 | * $object = $container->getObject('paradise_lost.pdf); |
||
| 363 | * $object->setMetadata(array('author' => 'John Milton')); |
||
| 364 | * $object->setHeaders(array('content-disposition' => 'foo')); |
||
| 365 | * $object->syncMetadata(); |
||
| 366 | * </code> |
||
| 367 | * |
||
| 368 | * @throws \OpenStackStorage\Exceptions\ResponseError |
||
| 369 | */ |
||
| 370 | public function syncMetadata() |
||
| 389 | |||
| 390 | /** |
||
| 391 | * Commits the manifest to the remote storage system. |
||
| 392 | * |
||
| 393 | * Example: |
||
| 394 | * <code> |
||
| 395 | * $object = $container->getObject('paradise_lost.pdf); |
||
| 396 | * $object->setManifest('container/prefix'); |
||
| 397 | * $object->syncManifest(); |
||
| 398 | * </code> |
||
| 399 | */ |
||
| 400 | public function syncManifest() |
||
| 415 | |||
| 416 | /** |
||
| 417 | * Return the URI for this object, if its container is public. |
||
| 418 | * |
||
| 419 | * @return string |
||
| 420 | */ |
||
| 421 | public function getPublicUri() |
||
| 429 | |||
| 430 | /** |
||
| 431 | * Return the SSL URI for this object, if its container is public. |
||
| 432 | * |
||
| 433 | * @return string |
||
| 434 | */ |
||
| 435 | public function getPublicSslUri() |
||
| 443 | |||
| 444 | /** |
||
| 445 | * Return the streaming URI for this object, if its container is public. |
||
| 446 | * |
||
| 447 | * @return string |
||
| 448 | */ |
||
| 449 | public function getPublicStreamingUri() |
||
| 457 | |||
| 458 | /** |
||
| 459 | * Purge Edge cache for this object. |
||
| 460 | * |
||
| 461 | * You will be notified by email if one is provided when the |
||
| 462 | * job completes. |
||
| 463 | * |
||
| 464 | * Example: |
||
| 465 | * <code> |
||
| 466 | * $object1->purgeFromCdn(); |
||
| 467 | * $object2->purgeFromCdn('[email protected]'); |
||
| 468 | * $object3->purgeFromCdn('[email protected],[email protected]); |
||
| 469 | * </code> |
||
| 470 | * |
||
| 471 | * @param string $email |
||
| 472 | * @throws \OpenStackStorage\Exceptions\CDNNotEnabled |
||
| 473 | */ |
||
| 474 | public function purgeFromCdn($email = null) |
||
| 491 | |||
| 492 | /** |
||
| 493 | * Initialize the object with values from the remote service (if any). |
||
| 494 | * |
||
| 495 | * @return boolean |
||
| 496 | * @throws \Exception|\OpenStackStorage\Exceptions\ResponseError |
||
| 497 | */ |
||
| 498 | protected function initialize() |
||
| 531 | |||
| 532 | /** |
||
| 533 | * Validates the object name. |
||
| 534 | * |
||
| 535 | * @param string $name |
||
| 536 | * @throws \OpenStackStorage\Exceptions\InvalidObjectName |
||
| 537 | */ |
||
| 538 | protected function validateName($name = null) |
||
| 548 | |||
| 549 | /** |
||
| 550 | * Returns array representing http headers based on the |
||
| 551 | * respective instance attributes. |
||
| 552 | * |
||
| 553 | * @return array |
||
| 554 | * @throws \OpenStackStorage\Exceptions\InvalidMetaValue |
||
| 555 | * @throws \OpenStackStorage\Exceptions\InvalidMetaName |
||
| 556 | */ |
||
| 557 | protected function getNewHeaders() |
||
| 593 | } |
||
| 594 |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: