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 |
||
20 | * Represents a file reference stored in a database |
||
21 | * |
||
22 | * @property string $Hash SHA of the file |
||
23 | * @property string $Filename Name of the file, including directory |
||
24 | * @property string $Variant Variant of the file |
||
25 | * |
||
26 | * @package framework |
||
27 | * @subpackage filesystem |
||
28 | */ |
||
29 | class DBFile extends DBComposite implements AssetContainer { |
||
30 | |||
31 | use ImageManipulation; |
||
32 | |||
33 | /** |
||
34 | * List of allowed file categories. |
||
35 | * |
||
36 | * {@see File::$app_categories} |
||
37 | * |
||
38 | * @var array |
||
39 | */ |
||
40 | protected $allowedCategories = array(); |
||
41 | |||
42 | /** |
||
43 | * List of image mime types supported by the image manipulations API |
||
44 | * |
||
45 | * {@see File::app_categories} for matching extensions. |
||
46 | * |
||
47 | * @config |
||
48 | * @var array |
||
49 | */ |
||
50 | private static $supported_images = array( |
||
51 | 'image/jpeg', |
||
52 | 'image/gif', |
||
53 | 'image/png' |
||
54 | ); |
||
55 | |||
56 | /** |
||
57 | * Create a new image manipulation |
||
58 | * |
||
59 | * @param string $name |
||
60 | * @param array|string $allowed List of allowed file categories (not extensions), as per File::$app_categories |
||
61 | */ |
||
62 | public function __construct($name = null, $allowed = array()) { |
||
66 | |||
67 | /** |
||
68 | * Determine if a valid non-empty image exists behind this asset, which is a format |
||
69 | * compatible with image manipulations |
||
70 | * |
||
71 | * @return boolean |
||
72 | */ |
||
73 | public function getIsImage() { |
||
78 | |||
79 | /** |
||
80 | * @return AssetStore |
||
81 | */ |
||
82 | protected function getStore() { |
||
85 | |||
86 | private static $composite_db = array( |
||
87 | "Hash" => "Varchar(255)", // SHA of the base content |
||
88 | "Filename" => "Varchar(255)", // Path identifier of the base content |
||
89 | "Variant" => "Varchar(255)", // Identifier of the variant to the base, if given |
||
90 | ); |
||
91 | |||
92 | private static $casting = array( |
||
93 | 'URL' => 'Varchar', |
||
94 | 'AbsoluteURL' => 'Varchar', |
||
95 | 'Basename' => 'Varchar', |
||
96 | 'Title' => 'Varchar', |
||
97 | 'MimeType' => 'Varchar', |
||
98 | 'String' => 'Text', |
||
99 | 'Tag' => 'HTMLText', |
||
100 | 'Size' => 'Varchar' |
||
101 | ); |
||
102 | |||
103 | public function scaffoldFormField($title = null, $params = null) { |
||
106 | |||
107 | /** |
||
108 | * Return a html5 tag of the appropriate for this file (normally img or a) |
||
109 | * |
||
110 | * @return string |
||
111 | */ |
||
112 | public function forTemplate() { |
||
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() { |
||
128 | |||
129 | /** |
||
130 | * Determine the template to render as on the frontend |
||
131 | * |
||
132 | * @return string Name of template |
||
133 | */ |
||
134 | public function getFrontendTemplate() { |
||
149 | |||
150 | /** |
||
151 | * Get trailing part of filename |
||
152 | * |
||
153 | * @return string |
||
154 | */ |
||
155 | public function getBasename() { |
||
161 | |||
162 | /** |
||
163 | * Get file extension |
||
164 | * |
||
165 | * @return string |
||
166 | */ |
||
167 | public function getExtension() { |
||
173 | |||
174 | /** |
||
175 | * Alt title for this |
||
176 | * |
||
177 | * @return string |
||
178 | */ |
||
179 | public function getTitle() { |
||
187 | |||
188 | public function setFromLocalFile($path, $filename = null, $hash = null, $variant = null, $config = array()) { |
||
199 | |||
200 | public function setFromStream($stream, $filename, $hash = null, $variant = null, $config = array()) { |
||
211 | |||
212 | public function setFromString($data, $filename, $hash = null, $variant = null, $config = array()) { |
||
223 | |||
224 | public function getStream() { |
||
232 | |||
233 | public function getString() { |
||
241 | |||
242 | public function getURL($grant = true) { |
||
251 | |||
252 | /** |
||
253 | * Get URL, but without resampling. |
||
254 | * Note that this will return the url even if the file does not exist. |
||
255 | * |
||
256 | * @param bool $grant Ensures that the url for any protected assets is granted for the current user. |
||
257 | * @return string |
||
258 | */ |
||
259 | public function getSourceURL($grant = true) { |
||
264 | |||
265 | /** |
||
266 | * Get the absolute URL to this resource |
||
267 | * |
||
268 | * @return string |
||
269 | */ |
||
270 | public function getAbsoluteURL() { |
||
276 | |||
277 | public function getMetaData() { |
||
285 | |||
286 | public function getMimeType() { |
||
294 | |||
295 | public function getValue() { |
||
305 | |||
306 | public function getVisibility() { |
||
314 | |||
315 | public function exists() { |
||
323 | |||
324 | public function getFilename() { |
||
327 | |||
328 | public function getHash() { |
||
331 | |||
332 | public function getVariant() { |
||
335 | |||
336 | /** |
||
337 | * Return file size in bytes. |
||
338 | * |
||
339 | * @return int |
||
340 | */ |
||
341 | public function getAbsoluteSize() { |
||
348 | |||
349 | /** |
||
350 | * Customise this object with an "original" record for getting other customised fields |
||
351 | * |
||
352 | * @param AssetContainer $original |
||
353 | * @return $this |
||
354 | */ |
||
355 | public function setOriginal($original) { |
||
359 | |||
360 | /** |
||
361 | * Get list of allowed file categories |
||
362 | * |
||
363 | * @return array |
||
364 | */ |
||
365 | public function getAllowedCategories() { |
||
368 | |||
369 | /** |
||
370 | * Assign allowed categories |
||
371 | * |
||
372 | * @param array|string $categories |
||
373 | * @return $this |
||
374 | */ |
||
375 | public function setAllowedCategories($categories) { |
||
382 | |||
383 | /** |
||
384 | * Gets the list of extensions (if limited) for this field. Empty list |
||
385 | * means there is no restriction on allowed types. |
||
386 | * |
||
387 | * @return array |
||
388 | */ |
||
389 | protected function getAllowedExtensions() { |
||
393 | |||
394 | /** |
||
395 | * Validate that this DBFile accepts this filename as valid |
||
396 | * |
||
397 | * @param string $filename |
||
398 | * @throws ValidationException |
||
399 | * @return bool |
||
400 | */ |
||
401 | protected function isValidFilename($filename) { |
||
419 | |||
420 | /** |
||
421 | * Check filename, and raise a ValidationException if invalid |
||
422 | * |
||
423 | * @param string $filename |
||
424 | * @throws ValidationException |
||
425 | */ |
||
426 | protected function assertFilenameValid($filename) { |
||
433 | |||
434 | |||
435 | /** |
||
436 | * Hook to validate this record against a validation result |
||
437 | * |
||
438 | * @param ValidationResult $result |
||
439 | * @param string $filename Optional filename to validate. If omitted, the current value is validated. |
||
440 | * @return bool Valid flag |
||
441 | */ |
||
442 | public function validate(ValidationResult $result, $filename = null) { |
||
465 | |||
466 | public function setField($field, $value, $markChanged = true) { |
||
474 | |||
475 | |||
476 | /** |
||
477 | * Returns the size of the file type in an appropriate format. |
||
478 | * |
||
479 | * @return string|false String value, or false if doesn't exist |
||
480 | */ |
||
481 | public function getSize() { |
||
488 | |||
489 | public function deleteFile() { |
||
498 | |||
499 | public function publishFile() { |
||
506 | |||
507 | public function protectFile() { |
||
514 | |||
515 | public function grantFile() { |
||
522 | |||
523 | public function revokeFile() { |
||
524 | if($this->Filename) { |
||
525 | $this |
||
526 | ->getStore() |
||
527 | ->revoke($this->Filename, $this->Hash); |
||
528 | } |
||
529 | } |
||
538 |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: