Complex classes like DBFile 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 DBFile, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 23 | class DBFile extends DBComposite implements AssetContainer, Thumbnail |
||
| 24 | { |
||
| 25 | |||
| 26 | use ImageManipulation; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * List of allowed file categories. |
||
| 30 | * |
||
| 31 | * {@see File::$app_categories} |
||
| 32 | * |
||
| 33 | * @var array |
||
| 34 | */ |
||
| 35 | protected $allowedCategories = array(); |
||
| 36 | |||
| 37 | /** |
||
| 38 | * List of image mime types supported by the image manipulations API |
||
| 39 | * |
||
| 40 | * {@see File::app_categories} for matching extensions. |
||
| 41 | * |
||
| 42 | * @config |
||
| 43 | * @var array |
||
| 44 | */ |
||
| 45 | private static $supported_images = array( |
||
| 46 | 'image/jpeg', |
||
| 47 | 'image/gif', |
||
| 48 | 'image/png' |
||
| 49 | ); |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Create a new image manipulation |
||
| 53 | * |
||
| 54 | * @param string $name |
||
| 55 | * @param array|string $allowed List of allowed file categories (not extensions), as per File::$app_categories |
||
| 56 | */ |
||
| 57 | public function __construct($name = null, $allowed = array()) |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Determine if a valid non-empty image exists behind this asset, which is a format |
||
| 65 | * compatible with image manipulations |
||
| 66 | * |
||
| 67 | * @return boolean |
||
| 68 | */ |
||
| 69 | public function getIsImage() |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @return AssetStore |
||
| 78 | */ |
||
| 79 | protected function getStore() |
||
| 83 | |||
| 84 | private static $composite_db = array( |
||
| 85 | "Hash" => "Varchar(255)", // SHA of the base content |
||
| 86 | "Filename" => "Varchar(255)", // Path identifier of the base content |
||
| 87 | "Variant" => "Varchar(255)", // Identifier of the variant to the base, if given |
||
| 88 | ); |
||
| 89 | |||
| 90 | private static $casting = array( |
||
| 91 | 'URL' => 'Varchar', |
||
| 92 | 'AbsoluteURL' => 'Varchar', |
||
| 93 | 'Basename' => 'Varchar', |
||
| 94 | 'Title' => 'Varchar', |
||
| 95 | 'MimeType' => 'Varchar', |
||
| 96 | 'String' => 'Text', |
||
| 97 | 'Tag' => 'HTMLFragment', |
||
| 98 | 'Size' => 'Varchar' |
||
| 99 | ); |
||
| 100 | |||
| 101 | public function scaffoldFormField($title = null, $params = null) |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Return a html5 tag of the appropriate for this file (normally img or a) |
||
| 108 | * |
||
| 109 | * @return string |
||
| 110 | */ |
||
| 111 | public function XML() |
||
| 115 | |||
| 116 | /** |
||
| 117 | * Return a html5 tag of the appropriate for this file (normally img or a) |
||
| 118 | * |
||
| 119 | * @return string |
||
| 120 | */ |
||
| 121 | public function getTag() |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Determine the template to render as on the frontend |
||
| 132 | * |
||
| 133 | * @return string Name of template |
||
| 134 | */ |
||
| 135 | public function getFrontendTemplate() |
||
| 151 | |||
| 152 | /** |
||
| 153 | * Get trailing part of filename |
||
| 154 | * |
||
| 155 | * @return string |
||
| 156 | */ |
||
| 157 | public function getBasename() |
||
| 164 | |||
| 165 | /** |
||
| 166 | * Get file extension |
||
| 167 | * |
||
| 168 | * @return string |
||
| 169 | */ |
||
| 170 | public function getExtension() |
||
| 177 | |||
| 178 | /** |
||
| 179 | * Alt title for this |
||
| 180 | * |
||
| 181 | * @return string |
||
| 182 | */ |
||
| 183 | public function getTitle() |
||
| 192 | |||
| 193 | public function setFromLocalFile($path, $filename = null, $hash = null, $variant = null, $config = array()) |
||
| 205 | |||
| 206 | public function setFromStream($stream, $filename, $hash = null, $variant = null, $config = array()) |
||
| 218 | |||
| 219 | public function setFromString($data, $filename, $hash = null, $variant = null, $config = array()) |
||
| 231 | |||
| 232 | public function getStream() |
||
| 241 | |||
| 242 | public function getString() |
||
| 251 | |||
| 252 | public function getURL($grant = true) |
||
| 262 | |||
| 263 | /** |
||
| 264 | * Get URL, but without resampling. |
||
| 265 | * Note that this will return the url even if the file does not exist. |
||
| 266 | * |
||
| 267 | * @param bool $grant Ensures that the url for any protected assets is granted for the current user. |
||
| 268 | * @return string |
||
| 269 | */ |
||
| 270 | public function getSourceURL($grant = true) |
||
| 276 | |||
| 277 | /** |
||
| 278 | * Get the absolute URL to this resource |
||
| 279 | * |
||
| 280 | * @return string |
||
| 281 | */ |
||
| 282 | public function getAbsoluteURL() |
||
| 289 | |||
| 290 | public function getMetaData() |
||
| 299 | |||
| 300 | public function getMimeType() |
||
| 309 | |||
| 310 | public function getValue() |
||
| 321 | |||
| 322 | public function getVisibility() |
||
| 331 | |||
| 332 | public function exists() |
||
| 341 | |||
| 342 | public function getFilename() |
||
| 346 | |||
| 347 | public function getHash() |
||
| 351 | |||
| 352 | public function getVariant() |
||
| 356 | |||
| 357 | /** |
||
| 358 | * Return file size in bytes. |
||
| 359 | * |
||
| 360 | * @return int |
||
| 361 | */ |
||
| 362 | public function getAbsoluteSize() |
||
| 370 | |||
| 371 | /** |
||
| 372 | * Customise this object with an "original" record for getting other customised fields |
||
| 373 | * |
||
| 374 | * @param AssetContainer $original |
||
| 375 | * @return $this |
||
| 376 | */ |
||
| 377 | public function setOriginal($original) |
||
| 382 | |||
| 383 | /** |
||
| 384 | * Get list of allowed file categories |
||
| 385 | * |
||
| 386 | * @return array |
||
| 387 | */ |
||
| 388 | public function getAllowedCategories() |
||
| 392 | |||
| 393 | /** |
||
| 394 | * Assign allowed categories |
||
| 395 | * |
||
| 396 | * @param array|string $categories |
||
| 397 | * @return $this |
||
| 398 | */ |
||
| 399 | public function setAllowedCategories($categories) |
||
| 407 | |||
| 408 | /** |
||
| 409 | * Gets the list of extensions (if limited) for this field. Empty list |
||
| 410 | * means there is no restriction on allowed types. |
||
| 411 | * |
||
| 412 | * @return array |
||
| 413 | */ |
||
| 414 | protected function getAllowedExtensions() |
||
| 419 | |||
| 420 | /** |
||
| 421 | * Validate that this DBFile accepts this filename as valid |
||
| 422 | * |
||
| 423 | * @param string $filename |
||
| 424 | * @throws ValidationException |
||
| 425 | * @return bool |
||
| 426 | */ |
||
| 427 | protected function isValidFilename($filename) |
||
| 446 | |||
| 447 | /** |
||
| 448 | * Check filename, and raise a ValidationException if invalid |
||
| 449 | * |
||
| 450 | * @param string $filename |
||
| 451 | * @throws ValidationException |
||
| 452 | */ |
||
| 453 | protected function assertFilenameValid($filename) |
||
| 461 | |||
| 462 | |||
| 463 | /** |
||
| 464 | * Hook to validate this record against a validation result |
||
| 465 | * |
||
| 466 | * @param ValidationResult $result |
||
| 467 | * @param string $filename Optional filename to validate. If omitted, the current value is validated. |
||
| 468 | * @return bool Valid flag |
||
| 469 | */ |
||
| 470 | public function validate(ValidationResult $result, $filename = null) |
||
| 483 | |||
| 484 | public function setField($field, $value, $markChanged = true) |
||
| 493 | |||
| 494 | |||
| 495 | /** |
||
| 496 | * Returns the size of the file type in an appropriate format. |
||
| 497 | * |
||
| 498 | * @return string|false String value, or false if doesn't exist |
||
| 499 | */ |
||
| 500 | public function getSize() |
||
| 508 | |||
| 509 | public function deleteFile() |
||
| 519 | |||
| 520 | public function publishFile() |
||
| 528 | |||
| 529 | public function protectFile() |
||
| 537 | |||
| 538 | public function grantFile() |
||
| 546 | |||
| 547 | public function revokeFile() |
||
| 555 | |||
| 556 | public function canViewFile() |
||
| 563 | } |
||
| 564 |
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: