Complex classes like ElggFile 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 ElggFile, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 22 | class ElggFile extends \ElggObject { |
||
| 23 | /** Filestore */ |
||
| 24 | private $filestore; |
||
| 25 | |||
| 26 | /** File handle used to identify this file in a filestore. Created by open. */ |
||
| 27 | private $handle; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * Set subtype to 'file'. |
||
| 31 | * |
||
| 32 | * @return void |
||
| 33 | */ |
||
| 34 | protected function initializeAttributes() { |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Loads an \ElggFile entity. |
||
| 42 | * |
||
| 43 | * @param \stdClass $row Database result or null for new \ElggFile |
||
| 44 | */ |
||
| 45 | public function __construct($row = null) { |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Set the filename of this file. |
||
| 54 | * |
||
| 55 | * @param string $name The filename. |
||
| 56 | * |
||
| 57 | * @return void |
||
| 58 | */ |
||
| 59 | public function setFilename($name) { |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Return the filename. |
||
| 65 | * |
||
| 66 | * @return string |
||
| 67 | */ |
||
| 68 | public function getFilename() { |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Return the filename of this file as it is/will be stored on the |
||
| 74 | * filestore, which may be different to the filename. |
||
| 75 | * |
||
| 76 | * @return string |
||
| 77 | */ |
||
| 78 | public function getFilenameOnFilestore() { |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Return the size of the filestore associated with this file |
||
| 84 | * |
||
| 85 | * @param string $prefix Storage prefix |
||
| 86 | * @param int $container_guid The container GUID of the checked filestore |
||
| 87 | * |
||
| 88 | * @return int |
||
| 89 | */ |
||
| 90 | public function getFilestoreSize($prefix = '', $container_guid = 0) { |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Get the mime type of the file. |
||
| 101 | * |
||
| 102 | * @return string |
||
| 103 | */ |
||
| 104 | public function getMimeType() { |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Set the mime type of the file. |
||
| 114 | * |
||
| 115 | * @param string $mimetype The mimetype |
||
| 116 | * |
||
| 117 | * @return bool |
||
| 118 | */ |
||
| 119 | public function setMimeType($mimetype) { |
||
| 122 | |||
| 123 | /** |
||
| 124 | * Detects mime types based on filename or actual file. |
||
| 125 | * |
||
| 126 | * @note This method can be called both dynamically and statically |
||
| 127 | * |
||
| 128 | * @param mixed $file The full path of the file to check. For uploaded files, use tmp_name. |
||
| 129 | * @param mixed $default A default. Useful to pass what the browser thinks it is. |
||
| 130 | * @since 1.7.12 |
||
| 131 | * |
||
| 132 | * @return mixed Detected type on success, false on failure. |
||
| 133 | * @todo Move this out into a utility class |
||
| 134 | */ |
||
| 135 | public function detectMimeType($file = null, $default = null) { |
||
| 168 | |||
| 169 | /** |
||
| 170 | * Set the optional file description. |
||
| 171 | * |
||
| 172 | * @param string $description The description. |
||
| 173 | * |
||
| 174 | * @return bool |
||
| 175 | */ |
||
| 176 | public function setDescription($description) { |
||
| 179 | |||
| 180 | /** |
||
| 181 | * Open the file with the given mode |
||
| 182 | * |
||
| 183 | * @param string $mode Either read/write/append |
||
| 184 | * |
||
| 185 | * @return resource File handler |
||
| 186 | * |
||
| 187 | * @throws IOException|InvalidParameterException |
||
| 188 | */ |
||
| 189 | public function open($mode) { |
||
| 218 | |||
| 219 | /** |
||
| 220 | * Write data. |
||
| 221 | * |
||
| 222 | * @param string $data The data |
||
| 223 | * |
||
| 224 | * @return bool |
||
| 225 | */ |
||
| 226 | public function write($data) { |
||
| 231 | |||
| 232 | /** |
||
| 233 | * Read data. |
||
| 234 | * |
||
| 235 | * @param int $length Amount to read. |
||
| 236 | * @param int $offset The offset to start from. |
||
| 237 | * |
||
| 238 | * @return mixed Data or false |
||
| 239 | */ |
||
| 240 | public function read($length, $offset = 0) { |
||
| 245 | |||
| 246 | /** |
||
| 247 | * Gets the full contents of this file. |
||
| 248 | * |
||
| 249 | * @return mixed The file contents. |
||
| 250 | */ |
||
| 251 | public function grabFile() { |
||
| 255 | |||
| 256 | /** |
||
| 257 | * Close the file and commit changes |
||
| 258 | * |
||
| 259 | * @return bool |
||
| 260 | */ |
||
| 261 | public function close() { |
||
| 272 | |||
| 273 | /** |
||
| 274 | * Delete this file. |
||
| 275 | * |
||
| 276 | * @return bool |
||
| 277 | */ |
||
| 278 | public function delete() { |
||
| 289 | |||
| 290 | /** |
||
| 291 | * Seek a position in the file. |
||
| 292 | * |
||
| 293 | * @param int $position Position in bytes |
||
| 294 | * |
||
| 295 | * @return bool |
||
| 296 | */ |
||
| 297 | public function seek($position) { |
||
| 303 | |||
| 304 | /** |
||
| 305 | * Return the current position of the file. |
||
| 306 | * |
||
| 307 | * @return int The file position |
||
| 308 | */ |
||
| 309 | public function tell() { |
||
| 314 | |||
| 315 | /** |
||
| 316 | * Return the size of the file in bytes. |
||
| 317 | * |
||
| 318 | * @return int |
||
| 319 | * @since 1.9 |
||
| 320 | */ |
||
| 321 | public function getSize() { |
||
| 324 | |||
| 325 | /** |
||
| 326 | * Return the size of the file in bytes. |
||
| 327 | * |
||
| 328 | * @return int |
||
| 329 | * @deprecated 1.8 Use getSize() |
||
| 330 | */ |
||
| 331 | public function size() { |
||
| 335 | |||
| 336 | /** |
||
| 337 | * Return a boolean value whether the file handle is at the end of the file |
||
| 338 | * |
||
| 339 | * @return bool |
||
| 340 | */ |
||
| 341 | public function eof() { |
||
| 346 | |||
| 347 | /** |
||
| 348 | * Returns if the file exists |
||
| 349 | * |
||
| 350 | * @return bool |
||
| 351 | */ |
||
| 352 | public function exists() { |
||
| 357 | |||
| 358 | /** |
||
| 359 | * Set a filestore. |
||
| 360 | * |
||
| 361 | * @param \ElggFilestore $filestore The file store. |
||
| 362 | * |
||
| 363 | * @return void |
||
| 364 | */ |
||
| 365 | public function setFilestore(\ElggFilestore $filestore) { |
||
| 368 | |||
| 369 | /** |
||
| 370 | * Return a filestore suitable for saving this file. |
||
| 371 | * This filestore is either a pre-registered filestore, |
||
| 372 | * a filestore as recorded in metadata or the system default. |
||
| 373 | * |
||
| 374 | * @return \ElggFilestore |
||
| 375 | * |
||
| 376 | * @throws ClassNotFoundException |
||
| 377 | */ |
||
| 378 | protected function getFilestore() { |
||
| 426 | |||
| 427 | /** |
||
| 428 | * Save the file |
||
| 429 | * |
||
| 430 | * Write the file's data to the filestore and save |
||
| 431 | * the corresponding entity. |
||
| 432 | * |
||
| 433 | * @see \ElggObject::save() |
||
| 434 | * |
||
| 435 | * @return bool |
||
| 436 | */ |
||
| 437 | public function save() { |
||
| 453 | |||
| 454 | /** |
||
| 455 | * Get property names to serialize. |
||
| 456 | * |
||
| 457 | * @return string[] |
||
| 458 | */ |
||
| 459 | public function __sleep() { |
||
| 469 | |||
| 470 | /** |
||
| 471 | * Reestablish filestore property |
||
| 472 | * |
||
| 473 | * @return void |
||
| 474 | * @throws ClassNotFoundException |
||
| 475 | */ |
||
| 476 | public function __wakeup() { |
||
| 479 | } |
||
| 480 |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
integervalues, zero is a special case, in particular the following results might be unexpected: