Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like File 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 File, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
68 | class File extends DataObject implements ShortcodeHandler, AssetContainer { |
||
69 | |||
70 | use ImageManipulation; |
||
71 | |||
72 | private static $default_sort = "\"Name\""; |
||
|
|||
73 | |||
74 | private static $singular_name = "File"; |
||
75 | |||
76 | private static $plural_name = "Files"; |
||
77 | |||
78 | /** |
||
79 | * Permissions necessary to view files outside of the live stage (e.g. archive / draft stage). |
||
80 | * |
||
81 | * @config |
||
82 | * @var array |
||
83 | */ |
||
84 | private static $non_live_permissions = array('CMS_ACCESS_LeftAndMain', 'CMS_ACCESS_AssetAdmin', 'VIEW_DRAFT_CONTENT'); |
||
85 | |||
86 | private static $db = array( |
||
87 | "Name" =>"Varchar(255)", |
||
88 | "Title" =>"Varchar(255)", |
||
89 | "File" =>"DBFile", |
||
90 | // Only applies to files, doesn't inherit for folder |
||
91 | 'ShowInSearch' => 'Boolean(1)', |
||
92 | ); |
||
93 | |||
94 | private static $has_one = array( |
||
95 | "Parent" => "File", |
||
96 | "Owner" => "Member" |
||
97 | ); |
||
98 | |||
99 | private static $defaults = array( |
||
100 | "ShowInSearch" => 1, |
||
101 | ); |
||
102 | |||
103 | private static $extensions = array( |
||
104 | "Hierarchy", |
||
105 | "Versioned" |
||
106 | ); |
||
107 | |||
108 | private static $casting = array( |
||
109 | 'TreeTitle' => 'HTMLText' |
||
110 | ); |
||
111 | |||
112 | /** |
||
113 | * @config |
||
114 | * @var array List of allowed file extensions, enforced through {@link validate()}. |
||
115 | * |
||
116 | * Note: if you modify this, you should also change a configuration file in the assets directory. |
||
117 | * Otherwise, the files will be able to be uploaded but they won't be able to be served by the |
||
118 | * webserver. |
||
119 | * |
||
120 | * - If you are running Apache you will need to change assets/.htaccess |
||
121 | * - If you are running IIS you will need to change assets/web.config |
||
122 | * |
||
123 | * Instructions for the change you need to make are included in a comment in the config file. |
||
124 | */ |
||
125 | private static $allowed_extensions = array( |
||
126 | '', 'ace', 'arc', 'arj', 'asf', 'au', 'avi', 'bmp', 'bz2', 'cab', 'cda', 'css', 'csv', 'dmg', 'doc', |
||
127 | 'docx', 'dotx', 'dotm', 'flv', 'gif', 'gpx', 'gz', 'hqx', 'ico', 'jar', 'jpeg', 'jpg', 'js', 'kml', |
||
128 | 'm4a', 'm4v', 'mid', 'midi', 'mkv', 'mov', 'mp3', 'mp4', 'mpa', 'mpeg', 'mpg', 'ogg', 'ogv', 'pages', |
||
129 | 'pcx', 'pdf', 'png', 'pps', 'ppt', 'pptx', 'potx', 'potm', 'ra', 'ram', 'rm', 'rtf', 'sit', 'sitx', |
||
130 | 'tar', 'tgz', 'tif', 'tiff', 'txt', 'wav', 'webm', 'wma', 'wmv', 'xls', 'xlsx', 'xltx', 'xltm', 'zip', |
||
131 | 'zipx', |
||
132 | ); |
||
133 | |||
134 | /** |
||
135 | * @config |
||
136 | * @var array Category identifiers mapped to commonly used extensions. |
||
137 | */ |
||
138 | private static $app_categories = array( |
||
139 | 'archive' => array( |
||
140 | 'ace', 'arc', 'arj', 'bz', 'bz2', 'cab', 'dmg', 'gz', 'hqx', 'jar', 'rar', 'sit', 'sitx', 'tar', 'tgz', |
||
141 | 'zip', 'zipx', |
||
142 | ), |
||
143 | 'audio' => array( |
||
144 | 'aif', 'aifc', 'aiff', 'apl', 'au', 'avr', 'cda', 'm4a', 'mid', 'midi', 'mp3', 'ogg', 'ra', |
||
145 | 'ram', 'rm', 'snd', 'wav', 'wma', |
||
146 | ), |
||
147 | 'document' => array( |
||
148 | 'css', 'csv', 'doc', 'docx', 'dotm', 'dotx', 'htm', 'html', 'gpx', 'js', 'kml', 'pages', 'pdf', |
||
149 | 'potm', 'potx', 'pps', 'ppt', 'pptx', 'rtf', 'txt', 'xhtml', 'xls', 'xlsx', 'xltm', 'xltx', 'xml', |
||
150 | ), |
||
151 | 'image' => array( |
||
152 | 'alpha', 'als', 'bmp', 'cel', 'gif', 'ico', 'icon', 'jpeg', 'jpg', 'pcx', 'png', 'ps', 'tif', 'tiff', |
||
153 | ), |
||
154 | 'image/supported' => array( |
||
155 | 'gif', 'jpeg', 'jpg', 'png' |
||
156 | ), |
||
157 | 'flash' => array( |
||
158 | 'fla', 'swf' |
||
159 | ), |
||
160 | 'video' => array( |
||
161 | 'asf', 'avi', 'flv', 'ifo', 'm1v', 'm2v', 'm4v', 'mkv', 'mov', 'mp2', 'mp4', 'mpa', 'mpe', 'mpeg', |
||
162 | 'mpg', 'ogv', 'qt', 'vob', 'webm', 'wmv', |
||
163 | ), |
||
164 | ); |
||
165 | |||
166 | /** |
||
167 | * Map of file extensions to class type |
||
168 | * |
||
169 | * @config |
||
170 | * @var |
||
171 | */ |
||
172 | private static $class_for_file_extension = array( |
||
173 | '*' => 'File', |
||
174 | 'jpg' => 'Image', |
||
175 | 'jpeg' => 'Image', |
||
176 | 'png' => 'Image', |
||
177 | 'gif' => 'Image', |
||
178 | ); |
||
179 | |||
180 | /** |
||
181 | * @config |
||
182 | * @var If this is true, then restrictions set in {@link $allowed_max_file_size} and |
||
183 | * {@link $allowed_extensions} will be applied to users with admin privileges as |
||
184 | * well. |
||
185 | */ |
||
186 | private static $apply_restrictions_to_admin = true; |
||
187 | |||
188 | /** |
||
189 | * If enabled, legacy file dataobjects will be automatically imported into the APL |
||
190 | * |
||
191 | * @config |
||
192 | * @var bool |
||
193 | */ |
||
194 | private static $migrate_legacy_file = false; |
||
195 | |||
196 | /** |
||
197 | * @config |
||
198 | * @var boolean |
||
199 | */ |
||
200 | private static $update_filesystem = true; |
||
201 | |||
202 | public static function get_shortcodes() { |
||
205 | |||
206 | /** |
||
207 | * Replace"[file_link id=n]" shortcode with an anchor tag or link to the file. |
||
208 | * |
||
209 | * @param array $arguments Arguments passed to the parser |
||
210 | * @param string $content Raw shortcode |
||
211 | * @param ShortcodeParser $parser Parser |
||
212 | * @param string $shortcode Name of shortcode used to register this handler |
||
213 | * @param array $extra Extra arguments |
||
214 | * @return string Result of the handled shortcode |
||
215 | */ |
||
216 | public static function handle_shortcode($arguments, $content, $parser, $shortcode, $extra = array()) { |
||
245 | |||
246 | /** |
||
247 | * Find the record to use for a given shortcode. |
||
248 | * |
||
249 | * @param array $args Array of input shortcode arguments |
||
250 | * @param int $errorCode If the file is not found, or is inaccessible, this will be assigned to a HTTP error code. |
||
251 | * @return File|null The File DataObject, if it can be found. |
||
252 | */ |
||
253 | public static function find_shortcode_record($args, &$errorCode = null) { |
||
275 | |||
276 | /** |
||
277 | * Given a HTTP Error, find an appropriate substitute File or SiteTree data object instance. |
||
278 | * |
||
279 | * @param int $errorCode HTTP Error value |
||
280 | * @return File|SiteTree File or SiteTree object to use for the given error |
||
281 | */ |
||
282 | protected static function find_error_record($errorCode) { |
||
290 | |||
291 | /** |
||
292 | * A file only exists if the file_exists() and is in the DB as a record |
||
293 | * |
||
294 | * Use $file->isInDB() to only check for a DB record |
||
295 | * Use $file->File->exists() to only check if the asset exists |
||
296 | * |
||
297 | * @return bool |
||
298 | */ |
||
299 | public function exists() { |
||
302 | |||
303 | /** |
||
304 | * Find a File object by the given filename. |
||
305 | * |
||
306 | * @param string $filename Filename to search for, including any custom parent directories. |
||
307 | * @return File |
||
308 | */ |
||
309 | public static function find($filename) { |
||
325 | |||
326 | /** |
||
327 | * Just an alias function to keep a consistent API with SiteTree |
||
328 | * |
||
329 | * @return string The link to the file |
||
330 | */ |
||
331 | public function Link() { |
||
334 | |||
335 | /** |
||
336 | * @deprecated 4.0 |
||
337 | */ |
||
338 | public function RelativeLink() { |
||
342 | |||
343 | /** |
||
344 | * Just an alias function to keep a consistent API with SiteTree |
||
345 | * |
||
346 | * @return string The absolute link to the file |
||
347 | */ |
||
348 | public function AbsoluteLink() { |
||
351 | |||
352 | /** |
||
353 | * @return string |
||
354 | */ |
||
355 | public function getTreeTitle() { |
||
358 | |||
359 | /** |
||
360 | * @param Member $member |
||
361 | * @return bool |
||
362 | */ |
||
363 | View Code Duplication | public function canView($member = null) { |
|
375 | |||
376 | /** |
||
377 | * Check if this file can be modified |
||
378 | * |
||
379 | * @param Member $member |
||
380 | * @return boolean |
||
381 | */ |
||
382 | public function canEdit($member = null) { |
||
394 | |||
395 | /** |
||
396 | * Check if a file can be created |
||
397 | * |
||
398 | * @param Member $member |
||
399 | * @param array $context |
||
400 | * @return boolean |
||
401 | */ |
||
402 | View Code Duplication | public function canCreate($member = null, $context = array()) { |
|
414 | |||
415 | /** |
||
416 | * Check if this file can be deleted |
||
417 | * |
||
418 | * @param Member $member |
||
419 | * @return boolean |
||
420 | */ |
||
421 | View Code Duplication | public function canDelete($member = null) { |
|
433 | |||
434 | /** |
||
435 | * Returns the fields to power the edit screen of files in the CMS. |
||
436 | * You can modify this FieldList by subclassing folder, or by creating a {@link DataExtension} |
||
437 | * and implemeting updateCMSFields(FieldList $fields) on that extension. |
||
438 | * |
||
439 | * @return FieldList |
||
440 | */ |
||
441 | public function getCMSFields() { |
||
484 | |||
485 | /** |
||
486 | * Returns a category based on the file extension. |
||
487 | * This can be useful when grouping files by type, |
||
488 | * showing icons on filelinks, etc. |
||
489 | * Possible group values are:"audio","mov","zip","image". |
||
490 | * |
||
491 | * @param string $ext Extension to check |
||
492 | * @return string |
||
493 | */ |
||
494 | public static function get_app_category($ext) { |
||
501 | |||
502 | /** |
||
503 | * For a category or list of categories, get the list of file extensions |
||
504 | * |
||
505 | * @param array|string $categories List of categories, or single category |
||
506 | * @return array |
||
507 | */ |
||
508 | public static function get_category_extensions($categories) { |
||
536 | |||
537 | /** |
||
538 | * Returns a category based on the file extension. |
||
539 | * |
||
540 | * @return string |
||
541 | */ |
||
542 | public function appCategory() { |
||
545 | |||
546 | |||
547 | /** |
||
548 | * Should be called after the file was uploaded |
||
549 | */ |
||
550 | public function onAfterUpload() { |
||
553 | |||
554 | /** |
||
555 | * Make sure the file has a name |
||
556 | */ |
||
557 | protected function onBeforeWrite() { |
||
573 | |||
574 | /** |
||
575 | * This will check if the parent record and/or name do not match the name on the underlying |
||
576 | * DBFile record, and if so, copy this file to the new location, and update the record to |
||
577 | * point to this new file. |
||
578 | * |
||
579 | * This method will update the File {@see DBFile} field value on success, so it must be called |
||
580 | * before writing to the database |
||
581 | * |
||
582 | * @return bool True if changed |
||
583 | */ |
||
584 | public function updateFilesystem() { |
||
612 | |||
613 | /** |
||
614 | * Collate selected descendants of this page. |
||
615 | * $condition will be evaluated on each descendant, and if it is succeeds, that item will be added |
||
616 | * to the $collator array. |
||
617 | * |
||
618 | * @param string $condition The PHP condition to be evaluated. The page will be called $item |
||
619 | * @param array $collator An array, passed by reference, to collect all of the matching descendants. |
||
620 | * @return true|null |
||
621 | */ |
||
622 | public function collateDescendants($condition, &$collator) { |
||
631 | |||
632 | /** |
||
633 | * Setter function for Name. Automatically sets a default title, |
||
634 | * and removes characters that might be invalid on the filesystem. |
||
635 | * Also adds a suffix to the name if the filename already exists |
||
636 | * on the filesystem, and is associated to a different {@link File} database record |
||
637 | * in the same folder. This means"myfile.jpg" might become"myfile-1.jpg". |
||
638 | * |
||
639 | * Does not change the filesystem itself, please use {@link write()} for this. |
||
640 | * |
||
641 | * @param string $name |
||
642 | * @return $this |
||
643 | */ |
||
644 | public function setName($name) { |
||
689 | |||
690 | /** |
||
691 | * Gets the URL of this file |
||
692 | * |
||
693 | * @return string |
||
694 | */ |
||
695 | public function getAbsoluteURL() { |
||
701 | |||
702 | /** |
||
703 | * Gets the URL of this file |
||
704 | * |
||
705 | * @uses Director::baseURL() |
||
706 | * @param bool $grant Ensures that the url for any protected assets is granted for the current user. |
||
707 | * @return string |
||
708 | */ |
||
709 | public function getURL($grant = true) { |
||
714 | |||
715 | /** |
||
716 | * Get URL, but without resampling. |
||
717 | * |
||
718 | * @param bool $grant Ensures that the url for any protected assets is granted for the current user. |
||
719 | * @return string |
||
720 | */ |
||
721 | public function getSourceURL($grant = true) { |
||
726 | |||
727 | /** |
||
728 | * @todo Coupling with cms module, remove this method. |
||
729 | * |
||
730 | * @return string |
||
731 | */ |
||
732 | public function DeleteLink() { |
||
735 | |||
736 | /** |
||
737 | * Get expected value of Filename tuple value. Will be used to trigger |
||
738 | * a file move on draft stage. |
||
739 | * |
||
740 | * @return string |
||
741 | */ |
||
742 | public function generateFilename() { |
||
750 | |||
751 | /** |
||
752 | * Ensure that parent folders are published before this one is published |
||
753 | * |
||
754 | * @todo Solve this via triggered publishing / ownership in the future |
||
755 | */ |
||
756 | public function onBeforePublish() { |
||
763 | |||
764 | /** |
||
765 | * Update the ParentID and Name for the given filename. |
||
766 | * |
||
767 | * On save, the underlying DBFile record will move the underlying file to this location. |
||
768 | * Thus it will not update the underlying Filename value until this is done. |
||
769 | * |
||
770 | * @param string $filename |
||
771 | * @return $this |
||
772 | */ |
||
773 | public function setFilename($filename) { |
||
796 | |||
797 | /** |
||
798 | * Returns the file extension |
||
799 | * |
||
800 | * @return string |
||
801 | */ |
||
802 | public function getExtension() { |
||
805 | |||
806 | /** |
||
807 | * Gets the extension of a filepath or filename, |
||
808 | * by stripping away everything before the last"dot". |
||
809 | * Caution: Only returns the last extension in"double-barrelled" |
||
810 | * extensions (e.g."gz" for"tar.gz"). |
||
811 | * |
||
812 | * Examples: |
||
813 | * -"myfile" returns"" |
||
814 | * -"myfile.txt" returns"txt" |
||
815 | * -"myfile.tar.gz" returns"gz" |
||
816 | * |
||
817 | * @param string $filename |
||
818 | * @return string |
||
819 | */ |
||
820 | public static function get_file_extension($filename) { |
||
823 | |||
824 | /** |
||
825 | * Given an extension, determine the icon that should be used |
||
826 | * |
||
827 | * @param string $extension |
||
828 | * @return string Icon filename relative to base url |
||
829 | */ |
||
830 | public static function get_icon_for_extension($extension) { |
||
845 | |||
846 | /** |
||
847 | * Return the type of file for the given extension |
||
848 | * on the current file name. |
||
849 | * |
||
850 | * @return string |
||
851 | */ |
||
852 | public function getFileType() { |
||
855 | |||
856 | /** |
||
857 | * Get descriptive type of file based on filename |
||
858 | * |
||
859 | * @param string $filename |
||
860 | * @return string Description of file |
||
861 | */ |
||
862 | public static function get_file_type($filename) { |
||
891 | |||
892 | /** |
||
893 | * Returns the size of the file type in an appropriate format. |
||
894 | * |
||
895 | * @return string|false String value, or false if doesn't exist |
||
896 | */ |
||
897 | public function getSize() { |
||
904 | |||
905 | /** |
||
906 | * Formats a file size (eg: (int)42 becomes string '42 bytes') |
||
907 | * |
||
908 | * @todo unit tests |
||
909 | * |
||
910 | * @param int $size |
||
911 | * @return string |
||
912 | */ |
||
913 | public static function format_size($size) { |
||
931 | |||
932 | /** |
||
933 | * Convert a php.ini value (eg: 512M) to bytes |
||
934 | * |
||
935 | * @todo unit tests |
||
936 | * |
||
937 | * @param string $iniValue |
||
938 | * @return int |
||
939 | */ |
||
940 | public static function ini2bytes($iniValue) { |
||
951 | |||
952 | /** |
||
953 | * Return file size in bytes. |
||
954 | * |
||
955 | * @return int |
||
956 | */ |
||
957 | public function getAbsoluteSize(){ |
||
960 | |||
961 | public function validate() { |
||
967 | |||
968 | /** |
||
969 | * Maps a {@link File} subclass to a specific extension. |
||
970 | * By default, files with common image extensions will be created |
||
971 | * as {@link Image} instead of {@link File} when using |
||
972 | * {@link Folder::constructChild}, {@link Folder::addUploadToFolder}), |
||
973 | * and the {@link Upload} class (either directly or through {@link FileField}). |
||
974 | * For manually instanciated files please use this mapping getter. |
||
975 | * |
||
976 | * Caution: Changes to mapping doesn't apply to existing file records in the database. |
||
977 | * Also doesn't hook into {@link Object::getCustomClass()}. |
||
978 | * |
||
979 | * @param String File extension, without dot prefix. Use an asterisk ('*') |
||
980 | * to specify a generic fallback if no mapping is found for an extension. |
||
981 | * @return String Classname for a subclass of {@link File} |
||
982 | */ |
||
983 | public static function get_class_for_file_extension($ext) { |
||
987 | |||
988 | /** |
||
989 | * See {@link get_class_for_file_extension()}. |
||
990 | * |
||
991 | * @param String|array |
||
992 | * @param String |
||
993 | */ |
||
994 | public static function set_class_for_file_extension($exts, $class) { |
||
1005 | |||
1006 | public function getMetaData() { |
||
1011 | |||
1012 | public function getMimeType() { |
||
1017 | |||
1018 | public function getStream() { |
||
1023 | |||
1024 | public function getString() { |
||
1029 | |||
1030 | View Code Duplication | public function setFromLocalFile($path, $filename = null, $hash = null, $variant = null, $config = array()) { |
|
1039 | |||
1040 | View Code Duplication | public function setFromStream($stream, $filename, $hash = null, $variant = null, $config = array()) { |
|
1049 | |||
1050 | View Code Duplication | public function setFromString($data, $filename, $hash = null, $variant = null, $config = array()) { |
|
1059 | |||
1060 | public function getIsImage() { |
||
1063 | |||
1064 | public function getFilename() { |
||
1067 | |||
1068 | public function getHash() { |
||
1071 | |||
1072 | public function getVariant() { |
||
1075 | |||
1076 | /** |
||
1077 | * Return a html5 tag of the appropriate for this file (normally img or a) |
||
1078 | * |
||
1079 | * @return string |
||
1080 | */ |
||
1081 | public function forTemplate() { |
||
1084 | |||
1085 | /** |
||
1086 | * Return a html5 tag of the appropriate for this file (normally img or a) |
||
1087 | * |
||
1088 | * @return string |
||
1089 | */ |
||
1090 | public function getTag() { |
||
1097 | |||
1098 | public function requireDefaultRecords() { |
||
1111 | |||
1112 | /** |
||
1113 | * Joins one or more segments together to build a Filename identifier. |
||
1114 | * |
||
1115 | * Note that the result will not have a leading slash, and should not be used |
||
1116 | * with local file paths. |
||
1117 | * |
||
1118 | * @param string $part,... Parts |
||
1119 | * @return string |
||
1120 | */ |
||
1121 | public static function join_paths() { |
||
1137 | |||
1138 | public function deleteFile() { |
||
1141 | |||
1142 | public function getVisibility() { |
||
1145 | |||
1146 | public function publishFile() { |
||
1149 | |||
1150 | public function protectFile() { |
||
1153 | |||
1154 | public function grantFile() { |
||
1157 | |||
1158 | public function revokeFile() { |
||
1161 | |||
1162 | public function canViewFile() { |
||
1165 | } |
||
1166 |