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 |
||
| 24 | class DBFile extends DBComposite implements AssetContainer, Thumbnail { |
||
| 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()) { |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Determine if a valid non-empty image exists behind this asset, which is a format |
||
| 64 | * compatible with image manipulations |
||
| 65 | * |
||
| 66 | * @return boolean |
||
| 67 | */ |
||
| 68 | public function getIsImage() { |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @return AssetStore |
||
| 76 | */ |
||
| 77 | protected function getStore() { |
||
| 80 | |||
| 81 | private static $composite_db = array( |
||
| 82 | "Hash" => "Varchar(255)", // SHA of the base content |
||
| 83 | "Filename" => "Varchar(255)", // Path identifier of the base content |
||
| 84 | "Variant" => "Varchar(255)", // Identifier of the variant to the base, if given |
||
| 85 | ); |
||
| 86 | |||
| 87 | private static $casting = array( |
||
| 88 | 'URL' => 'Varchar', |
||
| 89 | 'AbsoluteURL' => 'Varchar', |
||
| 90 | 'Basename' => 'Varchar', |
||
| 91 | 'Title' => 'Varchar', |
||
| 92 | 'MimeType' => 'Varchar', |
||
| 93 | 'String' => 'Text', |
||
| 94 | 'Tag' => 'HTMLFragment', |
||
| 95 | 'Size' => 'Varchar' |
||
| 96 | ); |
||
| 97 | |||
| 98 | public function scaffoldFormField($title = null, $params = null) { |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Return a html5 tag of the appropriate for this file (normally img or a) |
||
| 104 | * |
||
| 105 | * @return string |
||
| 106 | */ |
||
| 107 | public function XML() { |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Return a html5 tag of the appropriate for this file (normally img or a) |
||
| 113 | * |
||
| 114 | * @return string |
||
| 115 | */ |
||
| 116 | public function getTag() { |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Determine the template to render as on the frontend |
||
| 126 | * |
||
| 127 | * @return string Name of template |
||
| 128 | */ |
||
| 129 | public function getFrontendTemplate() { |
||
| 144 | |||
| 145 | /** |
||
| 146 | * Get trailing part of filename |
||
| 147 | * |
||
| 148 | * @return string |
||
| 149 | */ |
||
| 150 | public function getBasename() { |
||
| 156 | |||
| 157 | /** |
||
| 158 | * Get file extension |
||
| 159 | * |
||
| 160 | * @return string |
||
| 161 | */ |
||
| 162 | public function getExtension() { |
||
| 168 | |||
| 169 | /** |
||
| 170 | * Alt title for this |
||
| 171 | * |
||
| 172 | * @return string |
||
| 173 | */ |
||
| 174 | public function getTitle() { |
||
| 182 | |||
| 183 | public function setFromLocalFile($path, $filename = null, $hash = null, $variant = null, $config = array()) { |
||
| 194 | |||
| 195 | public function setFromStream($stream, $filename, $hash = null, $variant = null, $config = array()) { |
||
| 206 | |||
| 207 | public function setFromString($data, $filename, $hash = null, $variant = null, $config = array()) { |
||
| 218 | |||
| 219 | public function getStream() { |
||
| 227 | |||
| 228 | public function getString() { |
||
| 236 | |||
| 237 | public function getURL($grant = true) { |
||
| 246 | |||
| 247 | /** |
||
| 248 | * Get URL, but without resampling. |
||
| 249 | * Note that this will return the url even if the file does not exist. |
||
| 250 | * |
||
| 251 | * @param bool $grant Ensures that the url for any protected assets is granted for the current user. |
||
| 252 | * @return string |
||
| 253 | */ |
||
| 254 | public function getSourceURL($grant = true) { |
||
| 259 | |||
| 260 | /** |
||
| 261 | * Get the absolute URL to this resource |
||
| 262 | * |
||
| 263 | * @return string |
||
| 264 | */ |
||
| 265 | public function getAbsoluteURL() { |
||
| 271 | |||
| 272 | public function getMetaData() { |
||
| 280 | |||
| 281 | public function getMimeType() { |
||
| 289 | |||
| 290 | public function getValue() { |
||
| 300 | |||
| 301 | public function getVisibility() { |
||
| 309 | |||
| 310 | public function exists() { |
||
| 318 | |||
| 319 | public function getFilename() { |
||
| 322 | |||
| 323 | public function getHash() { |
||
| 326 | |||
| 327 | public function getVariant() { |
||
| 330 | |||
| 331 | /** |
||
| 332 | * Return file size in bytes. |
||
| 333 | * |
||
| 334 | * @return int |
||
| 335 | */ |
||
| 336 | public function getAbsoluteSize() { |
||
| 343 | |||
| 344 | /** |
||
| 345 | * Customise this object with an "original" record for getting other customised fields |
||
| 346 | * |
||
| 347 | * @param AssetContainer $original |
||
| 348 | * @return $this |
||
| 349 | */ |
||
| 350 | public function setOriginal($original) { |
||
| 354 | |||
| 355 | /** |
||
| 356 | * Get list of allowed file categories |
||
| 357 | * |
||
| 358 | * @return array |
||
| 359 | */ |
||
| 360 | public function getAllowedCategories() { |
||
| 363 | |||
| 364 | /** |
||
| 365 | * Assign allowed categories |
||
| 366 | * |
||
| 367 | * @param array|string $categories |
||
| 368 | * @return $this |
||
| 369 | */ |
||
| 370 | public function setAllowedCategories($categories) { |
||
| 377 | |||
| 378 | /** |
||
| 379 | * Gets the list of extensions (if limited) for this field. Empty list |
||
| 380 | * means there is no restriction on allowed types. |
||
| 381 | * |
||
| 382 | * @return array |
||
| 383 | */ |
||
| 384 | protected function getAllowedExtensions() { |
||
| 388 | |||
| 389 | /** |
||
| 390 | * Validate that this DBFile accepts this filename as valid |
||
| 391 | * |
||
| 392 | * @param string $filename |
||
| 393 | * @throws ValidationException |
||
| 394 | * @return bool |
||
| 395 | */ |
||
| 396 | protected function isValidFilename($filename) { |
||
| 414 | |||
| 415 | /** |
||
| 416 | * Check filename, and raise a ValidationException if invalid |
||
| 417 | * |
||
| 418 | * @param string $filename |
||
| 419 | * @throws ValidationException |
||
| 420 | */ |
||
| 421 | protected function assertFilenameValid($filename) { |
||
| 428 | |||
| 429 | |||
| 430 | /** |
||
| 431 | * Hook to validate this record against a validation result |
||
| 432 | * |
||
| 433 | * @param ValidationResult $result |
||
| 434 | * @param string $filename Optional filename to validate. If omitted, the current value is validated. |
||
| 435 | * @return bool Valid flag |
||
| 436 | */ |
||
| 437 | public function validate(ValidationResult $result, $filename = null) { |
||
| 460 | |||
| 461 | public function setField($field, $value, $markChanged = true) { |
||
| 469 | |||
| 470 | |||
| 471 | /** |
||
| 472 | * Returns the size of the file type in an appropriate format. |
||
| 473 | * |
||
| 474 | * @return string|false String value, or false if doesn't exist |
||
| 475 | */ |
||
| 476 | public function getSize() { |
||
| 483 | |||
| 484 | public function deleteFile() { |
||
| 493 | |||
| 494 | public function publishFile() { |
||
| 501 | |||
| 502 | public function protectFile() { |
||
| 509 | |||
| 510 | public function grantFile() { |
||
| 517 | |||
| 518 | public function revokeFile() { |
||
| 525 | |||
| 526 | public function canViewFile() { |
||
| 532 | |||
| 533 | /** |
||
| 534 | * Generates the URL for this DBFile preview, this is particularly important for images that |
||
| 535 | * have been manipulated e.g. by {@link ImageManipulation} |
||
| 536 | * Use the 'updatePreviewLink' extension point to customise the link. |
||
| 537 | * |
||
| 538 | * @param null $action |
||
| 539 | * @return bool|string |
||
| 540 | */ |
||
| 541 | public function PreviewLink($action = null) { |
||
| 555 | } |
||
| 556 |
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: